Skip to content
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

Replace walk with walkdir #34251

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion config/tests/lint/config_lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func Test_ForbidYmlExtension(t *testing.T) {
for _, path := range exemptPaths {
exempt[filepath.Join(configPath, path)] = true
}
err := filepath.Walk(configPath, func(path string, info os.FileInfo, err error) error {
err := filepath.WalkDir(configPath, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}
if _, ok := exempt[path]; ok {
return filepath.SkipDir
}
Expand Down
5 changes: 4 additions & 1 deletion experiment/bumpmonitoring/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,10 @@ func (c *client) findConfigToUpdate() error {
}

// No error is expected
filepath.Walk(fullPath, func(leafPath string, info os.FileInfo, err error) error {
filepath.WalkDir(fullPath, func(leafPath string, info os.DirEntry, err error) error {
if err != nil {
return err
}
if !re.MatchString(leafPath) {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion kubetest/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func Test_logDumperNode_dump(t *testing.T) {
}

actual := []string{}
err = filepath.Walk(tmpdir, func(path string, info os.FileInfo, err error) error {
err = filepath.WalkDir(tmpdir, func(path string, info os.DirEntry, err error) error {
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion kubetest/e2e/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (t *GinkgoTester) findBinary(name string) (string, error) {
}

if bazelBinExists {
err := filepath.Walk(bazelBin, func(path string, info os.FileInfo, err error) error {
err := filepath.WalkDir(bazelBin, func(path string, info os.DirEntry, err error) error {
Copy link
Member

@BenTheElder BenTheElder Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tool is deprecated but at the same time breaking it would be a problem.

Do we really need to make this change?

Go does not remove or stop supporting deprecated methods in the standard library (this would break the Go1 promise), they just discourage new usage versus better designed utilities.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aim was to remove overhead calls to os.stat since filepath.WalkDir allows us to interact with directory entries directly via os.DirEntry without needing to gather full file metadata (size, permissions, etc) , which avoids the overhead of making an additional system call (os.Stat) for each file.

But if this would break the Go1 promise, Do you want me to close this PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The aim was to remove overhead calls to os.stat since filepath.WalkDir allows us to interact with directory entries directly via os.DirEntry without needing to gather full file metadata (size, permissions, etc) , which avoids the overhead of making an additional system call (os.Stat) for each file.

This overhead is negligible though, isn't it? This isn't a high performance operation (also this particular codepath is effectively dead unless developing very old kubernetes releases)

But if this would break the Go1 promise, Do you want me to close this PR?

Er no, Go cannot remove the old method, so continuing to use it is safe. Us switching to a different API just requires not introducing our own bugs.

if err != nil {
return fmt.Errorf("error from walk: %w", err)
}
Expand Down