Przeglądaj źródła

fix: watch the directory a dangling entity file symlink points into

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
jamesread 1 dzień temu
rodzic
commit
948cb11cd3

+ 23 - 0
service/internal/filehelper/file_change_notify.go

@@ -354,6 +354,7 @@ 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)
 		return false, false
 	}
 
@@ -366,6 +367,28 @@ func resolveWatchedPath(ctx *watchContext) (changed bool, resolved bool) {
 	return true, true
 }
 
+// watchDanglingTargetDir watches the directory a dangling symlink points into.
+// Directory watches are not recursive, so when that directory is not already
+// watched, creating the missing target there would give no event, and the file
+// would not be reloaded once it can be read.
+func watchDanglingTargetDir(ctx *watchContext) {
+	target, err := os.Readlink(ctx.watchedPath)
+	if err != nil {
+		return
+	}
+
+	if !filepath.IsAbs(target) {
+		target = filepath.Join(filepath.Dir(ctx.watchedPath), target)
+	}
+
+	dir := filepath.Dir(target)
+	if _, err := os.Stat(dir); err != nil {
+		return
+	}
+
+	watchResolvedDir(ctx, dir)
+}
+
 func watchResolvedDir(ctx *watchContext, dir string) {
 	if dir == ctx.resolvedDir {
 		return

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

@@ -192,6 +192,24 @@ func TestWatchFileWriteWaitsForDanglingReplacement(t *testing.T) {
 	recorder.expectOneCall(t)
 }
 
+func TestWatchFileWriteWaitsForDanglingTargetInAnotherDirectory(t *testing.T) {
+	skipWithoutSymlinks(t)
+
+	linkDir, targetDir := t.TempDir(), t.TempDir()
+	path := filepath.Join(linkDir, "entities.yaml")
+	writeFile(t, path, "a: 1\n")
+	recorder := watchFileForTest(t, path)
+
+	missing := filepath.Join(targetDir, "entities.yaml")
+	temporary := filepath.Join(linkDir, "entities.yaml.tmp")
+	require.NoError(t, os.Symlink(missing, temporary))
+	require.NoError(t, os.Rename(temporary, path))
+	recorder.expectNone(t)
+
+	writeFile(t, missing, "a: 2\n")
+	recorder.expectOneCall(t)
+}
+
 func TestWatchFileWriteReloadsRenameSave(t *testing.T) {
 	path := filepath.Join(t.TempDir(), "entities.yaml")
 	writeFile(t, path, "a: 1\n")