From 44e716d2f1b36c64a9ef77850edfa7afe0ac7616 Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Fri, 20 Jun 2025 20:58:56 +1000 Subject: [PATCH] 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 --- main.go | 16 ++++++++-------- watch.go | 8 ++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index af94141d..f5d805ab 100644 --- a/main.go +++ b/main.go @@ -298,6 +298,14 @@ func main() { gSocketProt = gDefaultSocketProt gSocketPath = gDefaultSocketPath + if gLogPath != "" { + path, err := filepath.Abs(gLogPath) + if err != nil { + log.Fatalf("getting log path: %s", err) + } + gLogPath = path + } + if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { @@ -319,14 +327,6 @@ func main() { log.Fatalf("remote command: %s", err) } 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) serve() default: diff --git a/watch.go b/watch.go index 0ac17b89..9aa7768d 100644 --- a/watch.go +++ b/watch.go @@ -96,6 +96,14 @@ func (watch *watch) loop() { } 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) for _, path := range watch.getSameDirs(dir) { watch.addUpdate(filepath.Join(path, file))