Skip to content

path/filepath: pass clean path to WalkDirFunc and WalkFunc #50630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/path/filepath/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,11 +462,12 @@ func walk(path string, info fs.FileInfo, walkFn WalkFunc) error {
//
// WalkDir does not follow symbolic links.
func WalkDir(root string, fn fs.WalkDirFunc) error {
info, err := os.Lstat(root)
cleanRootPath := Clean(root)
info, err := os.Lstat(cleanRootPath)
if err != nil {
err = fn(root, nil, err)
err = fn(cleanRootPath, nil, err)
} else {
err = walkDir(root, &statDirEntry{info}, fn)
err = walkDir(cleanRootPath, &statDirEntry{info}, fn)
}
if err == SkipDir {
return nil
Expand Down Expand Up @@ -498,11 +499,12 @@ func (d *statDirEntry) Info() (fs.FileInfo, error) { return d.info, nil }
// Walk is less efficient than WalkDir, introduced in Go 1.16,
// which avoids calling os.Lstat on every visited file or directory.
func Walk(root string, fn WalkFunc) error {
info, err := os.Lstat(root)
cleanRootPath := Clean(root)
info, err := os.Lstat(cleanRootPath)
if err != nil {
err = fn(root, nil, err)
err = fn(cleanRootPath, nil, err)
} else {
err = walk(root, info, fn)
err = walk(cleanRootPath, info, fn)
}
if err == SkipDir {
return nil
Expand Down