Skip to content

proposal: path/filepath: Deprecate Walk #69604

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
earthboundkid opened this issue Sep 24, 2024 · 12 comments
Closed

proposal: path/filepath: Deprecate Walk #69604

earthboundkid opened this issue Sep 24, 2024 · 12 comments
Labels
Milestone

Comments

@earthboundkid
Copy link
Contributor

Proposal Details

filepath.WalkDir was added in Go 1.16. It's past time to go ahead and deprecate filepath.Walk, which is less efficient.

@gabyhelp
Copy link

@gopherbot gopherbot added this to the Proposal milestone Sep 24, 2024
@robpike
Copy link
Contributor

robpike commented Sep 24, 2024

Define 'deprecate'.

@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Sep 24, 2024
@ianlancetaylor
Copy link
Contributor

I'm not sure it's useful to deprecate a function that doesn't have an exact equivalent that people can simply switch to. The docs for filepath.Walk already say "Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling os.Lstat on every visited file or directory."

@earthboundkid
Copy link
Contributor Author

Define 'deprecate'.

Add // Deprecated: Use [WalkDir]. to the doc string, so my editor puts a squiggly line under accidental uses of Walk. I am aware that WalkDir exists and is more efficient but accidentally used Walk instead last week.

@earthboundkid
Copy link
Contributor Author

earthboundkid commented Sep 24, 2024

I'm not sure it's useful to deprecate a function that doesn't have an exact equivalent that people can simply switch to. The docs for filepath.Walk already say "Walk is less efficient than WalkDir, introduced in Go 1.16, which avoids calling os.Lstat on every visited file or directory."

In most cases, the DirEntry is sufficient. If it’s not, you can just do a .FileInfo() call yourself at no loss of efficiency. Or just keep using Walk because it’s not going to be removed.

@ianlancetaylor
Copy link
Contributor

Yes, you can update, but the update can't be easily done mechanically, and I don't think the performance advantage here is sufficient reason to push people to modify existing working code.

I think what you are looking for is something more like "hint when writing new code that a different choice would be better". Our current uses of deprecation apply to existing code as well. As far as I know we don't have that kind of hint at present.

@earthboundkid
Copy link
Contributor Author

I don't think the expectation is that there's always a trivial find-and-replace for deprecations. For example, zip.FileHeader.ModifiedTime is a uint16 that is deprecated in favor of a time.Time Modified field. Presumably fixing that requires ripping out any code that uses the uint16 as a number.

My expectation is that a large percentage of the changes from Walk to WalkDir would just need to change from fs.FileInfo to fs.DirEntry in the callback, but the ones that need more than that are fairly easy to catch and fix when the type check fails. I think it actually could be done mechanically with a type aware rewrite tool.

@ianlancetaylor
Copy link
Contributor

I'm not sure it's quite analogous. zip.FileHeader.ModifiedTime is deprecated because it doesn't have the same precision. Switching to Modified changes from 2-second precision to 1-second precision. So Modified is really better.

Walk and WalkDir are nearly the same. There isn't much of a reason to change existing code.

To be clear I certainly agree that new code should use WalkDir. I just don't think that deprecation as implemented today is necessarily the best path to make that happen.

@apocelipes
Copy link
Contributor

Rather than marking a function as deprecated, I'd like "go vet" or gopls to give me some hints like "WalkDir performs better, you should use this" or something like that. And if you add this hint, the same thing should be applied to os.(*File).Readdir too.

@robpike
Copy link
Contributor

robpike commented Sep 25, 2024

The comments already say that. Go vet's guildelines (see the README) have no scope to do suggest faster code. Whether gopls's job includes repeating all the information in the comments is up to its authors. I don't believe it does, though.

@apparentlymart
Copy link

apparentlymart commented Oct 7, 2024

Reading the discussion so far, I agree that it seems like the most ideal answer here would be a new classification that is softer than "deprecated" but still machine-readable so that tools can react to it. However, I also have a feeling that discussing such a thing here is taking the issue off-topic. 🤔

At the risk of straying further off-topic 😬 ...

The idea of this being a gopls thing actually resonates with me because its code completion hints are very much relevant only when writing new code, and not when reading existing code. Is this issue describing a use-case for a way to declare that a particular symbol should be excluded from (or deprioritized in) code completion?

Of course that's not an answer for everyone, but I will admit that when I'm writing new code I do tend to "follow my nose" by selecting the most likely candidate from the code completion list, rather than carefully reviewing the documentation for each function. Having Walk either not appear at all, or always appear after WalkDir in the choice list, would make me less likely to select it.

@seankhliao
Copy link
Member

I think as above, Deprecation is too strong for filepath.Walk.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

8 participants