Skip to content

Commit 157fc45

Browse files
ianlancetaylorbradfitz
authored andcommitted
path/filepath: don't return SkipDir at top
If the walker function called on a top-level file returns SkipDir, then (before this change) Walk would return SkipDir, which the documentation implies will not happen. Fixes #16280. Change-Id: I37d63bdcef7af4b56e342b624cf0d4b42e65c297 Reviewed-on: https://go-review.googlesource.com/24780 Run-TryBot: Ian Lance Taylor <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 307de65 commit 157fc45

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

src/path/filepath/path.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -393,9 +393,14 @@ func walk(path string, info os.FileInfo, walkFn WalkFunc) error {
393393
func Walk(root string, walkFn WalkFunc) error {
394394
info, err := os.Lstat(root)
395395
if err != nil {
396-
return walkFn(root, nil, err)
396+
err = walkFn(root, nil, err)
397+
} else {
398+
err = walk(root, info, walkFn)
397399
}
398-
return walk(root, info, walkFn)
400+
if err == SkipDir {
401+
return nil
402+
}
403+
return err
399404
}
400405

401406
// readDirNames reads the directory named by dirname and returns

src/path/filepath/path_test.go

+18-3
Original file line numberDiff line numberDiff line change
@@ -528,16 +528,28 @@ func TestWalkSkipDirOnFile(t *testing.T) {
528528
touch(t, filepath.Join(td, "dir/foo2"))
529529

530530
sawFoo2 := false
531-
filepath.Walk(td, func(path string, info os.FileInfo, err error) error {
531+
walker := func(path string, info os.FileInfo, err error) error {
532532
if strings.HasSuffix(path, "foo2") {
533533
sawFoo2 = true
534534
}
535535
if strings.HasSuffix(path, "foo1") {
536536
return filepath.SkipDir
537537
}
538538
return nil
539-
})
539+
}
540540

541+
err = filepath.Walk(td, walker)
542+
if err != nil {
543+
t.Fatal(err)
544+
}
545+
if sawFoo2 {
546+
t.Errorf("SkipDir on file foo1 did not block processing of foo2")
547+
}
548+
549+
err = filepath.Walk(filepath.Join(td, "dir"), walker)
550+
if err != nil {
551+
t.Fatal(err)
552+
}
541553
if sawFoo2 {
542554
t.Errorf("SkipDir on file foo1 did not block processing of foo2")
543555
}
@@ -1203,7 +1215,7 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
12031215
ken := filepath.Join(root, "ken")
12041216
seenBugs := false
12051217
seenKen := false
1206-
filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
1218+
err = filepath.Walk(root, func(pth string, info os.FileInfo, err error) error {
12071219
if err != nil {
12081220
t.Fatal(err)
12091221
}
@@ -1220,6 +1232,9 @@ func TestBug3486(t *testing.T) { // https://golang.org/issue/3486
12201232
}
12211233
return nil
12221234
})
1235+
if err != nil {
1236+
t.Fatal(err)
1237+
}
12231238
if !seenKen {
12241239
t.Fatalf("%q not seen", ken)
12251240
}

0 commit comments

Comments
 (0)