Просмотр исходного кода

fix(entities): reload a file when its dangling symlink resolves again

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 4 дней назад
Родитель
Сommit
e706b5496a

+ 11 - 3
service/internal/filehelper/file_change_notify.go

@@ -62,6 +62,10 @@ type watchContext struct {
 	// different directory to watchedPath, so in-place edits of a symlink's
 	// target are seen.
 	resolvedDir string
+	// dangling is set while watchedPath cannot be resolved. Edits made in the
+	// meantime may not have been seen, so the file is reloaded once it
+	// resolves again, even if it points to the same target as before.
+	dangling bool
 
 	interestedEvent fsnotify.Op
 }
@@ -348,22 +352,26 @@ func refreshResolvedPath(ctx *watchContext) bool {
 }
 
 // resolveWatchedPath updates where the watched file points. It reports
-// whether the target changed, and separately whether the file resolved at
-// all.
+// whether the target changed, or resolved again after dangling, and
+// separately whether the file resolved at all.
 func resolveWatchedPath(ctx *watchContext) (changed bool, resolved bool) {
 	target, err := filepath.EvalSymlinks(ctx.watchedPath)
 	if err != nil {
 		log.Debugf("fsnotify could not resolve %v: %v", ctx.watchedPath, err)
 		watchDanglingTargetDir(ctx)
+		ctx.dangling = true
 		return false, false
 	}
 
+	recovered := ctx.dangling
+	ctx.dangling = false
+
 	// The target's directory is watched again even when the target has not
 	// changed, because a dangling symlink in between may have moved the extra
 	// watch to the directory it pointed into.
 	watchResolvedDir(ctx, filepath.Dir(target))
 
-	if target == ctx.resolvedPath {
+	if target == ctx.resolvedPath && !recovered {
 		return false, true
 	}
 

+ 28 - 0
service/internal/filehelper/file_watch_symlink_test.go

@@ -237,6 +237,34 @@ func TestWatchFileWriteKeepsWatchingTargetAfterDanglingLinkIsUndone(t *testing.T
 	recorder.expectOneCall(t)
 }
 
+// The link points away to a missing file, the old target is edited while the
+// link dangles, then the missing file appears as a link back to the old
+// target. The resolved path is the same as before, but the content changed.
+func TestWatchFileWriteReloadsWhenDanglingLinkResolvesToSameTarget(t *testing.T) {
+	skipWithoutSymlinks(t)
+
+	linkDir, firstDir, secondDir := t.TempDir(), t.TempDir(), t.TempDir()
+	first := filepath.Join(firstDir, "entities.yaml")
+	writeFile(t, first, "a: 1\n")
+
+	path := filepath.Join(linkDir, "entities.yaml")
+	require.NoError(t, os.Symlink(first, path))
+	recorder := watchFileForTest(t, path)
+
+	second := filepath.Join(secondDir, "entities.yaml")
+	temporary := filepath.Join(linkDir, "entities.yaml.tmp")
+	require.NoError(t, os.Symlink(second, temporary))
+	require.NoError(t, os.Rename(temporary, path))
+	recorder.expectNone(t)
+
+	writeFile(t, first, "a: 2\n")
+	recorder.expectNone(t)
+
+	require.NoError(t, os.Symlink(first, second))
+	recorder.expectOneCall(t)
+	assert.Equal(t, "a: 2\n", readFile(t, path))
+}
+
 func TestWatchFileWriteReloadsRenameSave(t *testing.T) {
 	path := filepath.Join(t.TempDir(), "entities.yaml")
 	writeFile(t, path, "a: 1\n")