Selaa lähdekoodia

fix: watch the target's directory again when a dangling symlink is undone

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 3 päivää sitten
vanhempi
sitoutus
4ee5d2862e

+ 5 - 1
service/internal/filehelper/file_change_notify.go

@@ -358,12 +358,16 @@ func resolveWatchedPath(ctx *watchContext) (changed bool, resolved bool) {
 		return false, 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 {
 		return false, true
 	}
 
 	ctx.resolvedPath = target
-	watchResolvedDir(ctx, filepath.Dir(target))
 	return true, true
 }
 

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

@@ -210,6 +210,33 @@ func TestWatchFileWriteWaitsForDanglingTargetInAnotherDirectory(t *testing.T) {
 	recorder.expectOneCall(t)
 }
 
+func TestWatchFileWriteKeepsWatchingTargetAfterDanglingLinkIsUndone(t *testing.T) {
+	skipWithoutSymlinks(t)
+
+	linkDir, targetDir, otherDir := t.TempDir(), t.TempDir(), t.TempDir()
+	target := filepath.Join(targetDir, "real.yaml")
+	writeFile(t, target, "a: 1\n")
+
+	path := filepath.Join(linkDir, "entities.yaml")
+	require.NoError(t, os.Symlink(target, path))
+	recorder := watchFileForTest(t, path)
+
+	replaceLink := func(destination string) {
+		temporary := filepath.Join(linkDir, "entities.yaml.tmp")
+		require.NoError(t, os.Symlink(destination, temporary))
+		require.NoError(t, os.Rename(temporary, path))
+	}
+
+	replaceLink(filepath.Join(otherDir, "missing.yaml"))
+	recorder.expectNone(t)
+
+	replaceLink(target)
+	recorder.expectOneCall(t)
+
+	writeFile(t, target, "a: 2\n")
+	recorder.expectOneCall(t)
+}
+
 func TestWatchFileWriteReloadsRenameSave(t *testing.T) {
 	path := filepath.Join(t.TempDir(), "entities.yaml")
 	writeFile(t, path, "a: 1\n")