From e74265d921b9b12bf89882e791743758b42f5f3d Mon Sep 17 00:00:00 2001 From: kolaente Date: Thu, 19 Mar 2026 10:06:07 +0100 Subject: [PATCH] fix: make mage fmt skip gitignored files Use git ls-files instead of filepath.Walk to collect Go files, so that gitignored files are no longer formatted. --- magefile.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/magefile.go b/magefile.go index e3aeda4bd..1ff62fae5 100644 --- a/magefile.go +++ b/magefile.go @@ -27,7 +27,6 @@ import ( "errors" "fmt" "io" - "io/fs" "math" "net" "net/http" @@ -385,18 +384,13 @@ func waitForHTTP(ctx context.Context, url string, timeout time.Duration) error { // Fmt formats the code using go fmt func Fmt(ctx context.Context) error { mg.Deps(initVars) - var goFiles []string - err := filepath.Walk(".", func(path string, info fs.FileInfo, err error) error { - if err != nil { - return err - } - if !info.IsDir() && filepath.Ext(path) == ".go" { - goFiles = append(goFiles, path) - } - return nil - }) + out, err := exec.CommandContext(ctx, "git", "ls-files", "--cached", "--others", "--exclude-standard", "*.go").Output() if err != nil { - return err + return fmt.Errorf("failed to list go files from git: %w", err) + } + goFiles := strings.Split(strings.TrimSpace(string(out)), "\n") + if len(goFiles) == 0 || (len(goFiles) == 1 && goFiles[0] == "") { + return nil } args := append([]string{"-s", "-w"}, goFiles...) return runAndStreamOutput(ctx, "gofmt", args...)