Ignore updates for log file when watch is enabled (#2015)

* Ignore updates for log file when `watch` is enabled

* Move check earlier

* Ignore write event only

* fix message
This commit is contained in:
Joe Lim authored and GitHub committed 2025-06-20 20:58:56 +10:00
1 parent c3b184fc2e
commit 44e716d2f1
2 files changed
+16 -8

No files matched your search

+8 -8
View File
@@ -298,6 +298,14 @@ func main() {
gSocketProt = gDefaultSocketProt gSocketProt = gDefaultSocketProt
gSocketPath = gDefaultSocketPath gSocketPath = gDefaultSocketPath
if gLogPath != "" {
path, err := filepath.Abs(gLogPath)
if err != nil {
log.Fatalf("getting log path: %s", err)
}
gLogPath = path
}
if *cpuprofile != "" { if *cpuprofile != "" {
f, err := os.Create(*cpuprofile) f, err := os.Create(*cpuprofile)
if err != nil { if err != nil {
@@ -319,14 +327,6 @@ func main() {
log.Fatalf("remote command: %s", err) log.Fatalf("remote command: %s", err)
} }
case *serverMode: case *serverMode:
if gLogPath != "" && !filepath.IsAbs(gLogPath) {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("getting current directory: %s", err)
} else {
gLogPath = filepath.Join(wd, gLogPath)
}
}
os.Chdir(gUser.HomeDir) os.Chdir(gUser.HomeDir)
serve() serve()
default: default:
+8
View File
@@ -96,6 +96,14 @@ func (watch *watch) loop() {
} }
if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) { if ev.Has(fsnotify.Write) || ev.Has(fsnotify.Chmod) {
// skip write updates for the log file, otherwise it is possible
// to have an infinite loop where writing to the log file causes
// it to be reloaded, which in turn triggers more events that
// are then logged
if ev.Name == gLogPath && ev.Has(fsnotify.Write) {
continue
}
dir, file := filepath.Split(ev.Name) dir, file := filepath.Split(ev.Name)
for _, path := range watch.getSameDirs(dir) { for _, path := range watch.getSameDirs(dir) {
watch.addUpdate(filepath.Join(path, file)) watch.addUpdate(filepath.Join(path, file))