Skip to content

Commit

Permalink
cmd/shfmt: handle errors properly in filepath.Walk
Browse files Browse the repository at this point in the history
If the provided error is non-nil, info might be nil. We should check the
error first.

In practice, this only triggered with weird corner cases such as running
'shfmt -f /' inside a container, since some special files would give
weird I/O errors. As such, it's hard to write a small test for this.

The new code seems logical, and it's unlikely we'll break it again, so
don't add a test for now.

Fixes #466.
  • Loading branch information
mvdan committed Jan 3, 2020
1 parent 25e5333 commit 23f86c8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/shfmt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ func walk(path string, onError func(error)) {
return
}
filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && vcsDir.MatchString(info.Name()) {
return filepath.SkipDir
}
if err != nil {
onError(err)
return nil
}
if info.IsDir() && vcsDir.MatchString(info.Name()) {
return filepath.SkipDir
}
conf := fileutil.CouldBeScript(info)
if conf == fileutil.ConfNotScript {
return nil
Expand Down

0 comments on commit 23f86c8

Please sign in to comment.