Skip to content

Commit a547625

Browse files
committed
cmd/gofmt: simplify arg handling
First, we can use flag.Args instead of flag.NArg and flag.Arg. Second, just call filepath.WalkDir directly on each argument. We don't need to check if each argument is a directory or not, since the function will still work on regular files as expected. To continue giving an error in the "gofmt does-not-exist.go" case, we now need to return and handle errors from filepath.WalkDir, too. Arguably, that should have always been the case. While at it, I noticed that the printinf of the "diff" command did not obey the "out" parameter. Fix that. Finally, remove the code to ignore IsNotExist errors. It was added in CL 19301, though it didn't include tests and its reasoning is dubious. Using gofmt on a directory treewhile another program is concurrently editing or removing files is inherently racy. Hiding errors can hide valid problems from the user, and such racy usages aren't supported. Change-Id: I2e74cc04c53eeefb25231d804752b53562b97371 Reviewed-on: https://go-review.googlesource.com/c/go/+/284138 Run-TryBot: Daniel Martí <[email protected]> Trust: Daniel Martí <[email protected]> Reviewed-by: Bryan C. Mills <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 22a56b6 commit a547625

1 file changed

Lines changed: 8 additions & 21 deletions

File tree

src/cmd/gofmt/gofmt.go

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
151151
if err != nil {
152152
return fmt.Errorf("computing diff: %s", err)
153153
}
154-
fmt.Printf("diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename))
154+
fmt.Fprintf(out, "diff -u %s %s\n", filepath.ToSlash(filename+".orig"), filepath.ToSlash(filename))
155155
out.Write(data)
156156
}
157157
}
@@ -164,21 +164,15 @@ func processFile(filename string, in io.Reader, out io.Writer, stdin bool) error
164164
}
165165

166166
func visitFile(path string, f fs.DirEntry, err error) error {
167-
if err == nil && isGoFile(f) {
168-
err = processFile(path, nil, os.Stdout, false)
167+
if err != nil || !isGoFile(f) {
168+
return err
169169
}
170-
// Don't complain if a file was deleted in the meantime (i.e.
171-
// the directory changed concurrently while running gofmt).
172-
if err != nil && !os.IsNotExist(err) {
170+
if err := processFile(path, nil, os.Stdout, false); err != nil {
173171
report(err)
174172
}
175173
return nil
176174
}
177175

178-
func walkDir(path string) {
179-
filepath.WalkDir(path, visitFile)
180-
}
181-
182176
func main() {
183177
// call gofmtMain in a separate function
184178
// so that it can use defer and have them
@@ -206,7 +200,8 @@ func gofmtMain() {
206200
initParserMode()
207201
initRewrite()
208202

209-
if flag.NArg() == 0 {
203+
args := flag.Args()
204+
if len(args) == 0 {
210205
if *write {
211206
fmt.Fprintln(os.Stderr, "error: cannot use -w with standard input")
212207
exitCode = 2
@@ -218,17 +213,9 @@ func gofmtMain() {
218213
return
219214
}
220215

221-
for i := 0; i < flag.NArg(); i++ {
222-
path := flag.Arg(i)
223-
switch dir, err := os.Stat(path); {
224-
case err != nil:
216+
for _, arg := range args {
217+
if err := filepath.WalkDir(arg, visitFile); err != nil {
225218
report(err)
226-
case dir.IsDir():
227-
walkDir(path)
228-
default:
229-
if err := processFile(path, nil, os.Stdout, false); err != nil {
230-
report(err)
231-
}
232219
}
233220
}
234221
}

0 commit comments

Comments
 (0)