cmd/gofmt: simplify logic to process arguments

Rather than stat-ing each argument and taking different code paths
depending on whether it's a directory or not, we can leverage
the fact that filepath.WalkDir works on regular files and already
has to figure out whether each file it walks is a directory or not.

We can then implement "always format non-directory arguments"
by looking at whether the path we are walking is the original argument,
meaning we are walking the top file.

For full clarity, we expand the skipping logic with a switch,
as before it was a bit confusing how we could `return err`
on directories and other non-Go files.

Given that we discard directories separately now,
simplify isGoFile to just be about filenames.

While here, also note that we called AddReport inside WalkDir;
this is unnecessary, as we can return the error for the same effect.

Change-Id: I50ab94710143f19bd8dd95a69e01a3dd228e397e
Reviewed-on: https://go-review.googlesource.com/c/go/+/700115
Reviewed-by: Sean Liao <sean@liao.dev>
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Daniel Martí 2025-08-30 18:54:43 +01:00
parent 925a3cdcd1
commit 4c4cefc19a
2 changed files with 26 additions and 32 deletions

View file

@ -87,10 +87,8 @@ func initParserMode() {
} }
} }
func isGoFile(f fs.DirEntry) bool { func isGoFilename(name string) bool {
// ignore non-Go files return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go")
name := f.Name()
return !strings.HasPrefix(name, ".") && strings.HasSuffix(name, ".go") && !f.IsDir()
} }
// A sequencer performs concurrent tasks that may write output, but emits that // A sequencer performs concurrent tasks that may write output, but emits that
@ -411,34 +409,30 @@ func gofmtMain(s *sequencer) {
} }
for _, arg := range args { for _, arg := range args {
switch info, err := os.Stat(arg); { // Walk each given argument as a directory tree.
case err != nil: // If the argument is not a directory, it's always formatted as a Go file.
s.AddReport(err) // If the argument is a directory, we walk it, ignoring non-Go files.
case !info.IsDir(): if err := filepath.WalkDir(arg, func(path string, d fs.DirEntry, err error) error {
// Non-directory arguments are always formatted. switch {
arg := arg case err != nil:
s.Add(fileWeight(arg, info), func(r *reporter) error { return err
return processFile(arg, info, nil, r) case d.IsDir():
}) return nil // simply recurse into directories
default: case path == arg:
// Directories are walked, ignoring non-Go files. // non-directories given as explicit arguments are always formatted
err := filepath.WalkDir(arg, func(path string, f fs.DirEntry, err error) error { case !isGoFilename(d.Name()):
if err != nil || !isGoFile(f) { return nil // skip walked non-Go files
return err
}
info, err := f.Info()
if err != nil {
s.AddReport(err)
return nil
}
s.Add(fileWeight(path, info), func(r *reporter) error {
return processFile(path, info, nil, r)
})
return nil
})
if err != nil {
s.AddReport(err)
} }
info, err := d.Info()
if err != nil {
return err
}
s.Add(fileWeight(path, info), func(r *reporter) error {
return processFile(path, info, nil, r)
})
return nil
}); err != nil {
s.AddReport(err)
} }
} }
} }

View file

@ -115,7 +115,7 @@ func genFilenames(t *testing.T, filenames chan<- string) {
return nil return nil
} }
// don't descend into testdata directories // don't descend into testdata directories
if isGoFile(d) && !strings.Contains(filepath.ToSlash(filename), "/testdata/") { if !d.IsDir() && isGoFilename(d.Name()) && !strings.Contains(filepath.ToSlash(filename), "/testdata/") {
filenames <- filename filenames <- filename
nfiles++ nfiles++
} }