From 1d4e684949684852c9e20ef52a364b2abf1b73a1 Mon Sep 17 00:00:00 2001 From: XG Date: Thu, 16 Oct 2025 16:20:24 +0800 Subject: [PATCH] refactor(cmd/gf): Optimize run command to reload only on file write events (#4476) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化run命令使得只在文件有写入事件时才触发reload: gf run 的文件监控逻辑之前会对所有文件系统事件做出响应,包括非内容修改的事件(如文件access time变化),这会导致开发过程中不必要的频繁重载。本次修改在文件监控回调中增加了event.IsWrite()的判断,确保只有在文件内容被实际写入时才触发重载逻辑,优化了开发体验。 --- cmd/gf/internal/cmd/cmd_run.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/cmd/gf/internal/cmd/cmd_run.go b/cmd/gf/internal/cmd/cmd_run.go index 64223d98a..224972d89 100644 --- a/cmd/gf/internal/cmd/cmd_run.go +++ b/cmd/gf/internal/cmd/cmd_run.go @@ -27,9 +27,7 @@ import ( "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" ) -var ( - Run = cRun{} -) +var Run = cRun{} type cRun struct { g.Meta `name:"run" usage:"{cRunUsage}" brief:"{cRunBrief}" eg:"{cRunEg}" dc:"{cRunDc}"` @@ -63,9 +61,7 @@ which compiles and runs the go codes asynchronously when codes change. cRunWatchPathsBrief = `watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"` ) -var ( - process *gproc.Process -) +var process *gproc.Process func init() { gtag.Sets(g.MapStrStr{ @@ -119,8 +115,12 @@ func (c cRun) Index(ctx context.Context, in cRunInput) (out *cRunOutput, err err } dirty := gtype.NewBool() - var outputPath = app.genOutputPath() + outputPath := app.genOutputPath() callbackFunc := func(event *gfsnotify.Event) { + if !event.IsWrite() && !event.IsCreate() && !event.IsRemove() && !event.IsRename() { + return + } + if gfile.ExtName(event.Path) != "go" { return }