Skip to content

feat(fs): introduce canonicalize option to WalkOptions #3679

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

Merged
merged 7 commits into from
Oct 16, 2023

Conversation

powdream
Copy link
Contributor

@powdream powdream commented Sep 27, 2023

Introduces a new option canonicalize to WalkOptions.

Currently, when followSymlinks is enabled, walk() function emits the canonical paths of the symlinks, which is transformed with Deno.realPath(). However, if the false is given for canonicalize option, the symlinks would be kept without canonicalizing.

To keep the existing behavior of walk API, canonicalize defaults to true.

Example

root
  + a
     - 1
     - 2
  + b
    - a -> ../a

As-is

walk(b, { followSymlinks: true })

=> ["../a", "../a/1", "../a/2"]

To-be

walk(b, { followSymlinks: true })

=> ["../a", "../a/1", "../a/1"]
walk(b, { followSymlinks: true, canonicalize: false })

=> ["a", "a/1", "a/2"]

@powdream powdream requested a review from kt3k as a code owner September 27, 2023 06:49
@github-actions github-actions bot added the fs label Sep 27, 2023
@powdream powdream changed the title feat(walk): introduce useRealPath option to WalkOptions feat(fs): introduce useRealPath option to WalkOptions Sep 27, 2023
@timreichen
Copy link
Contributor

I think useRealPath as a name does not clearly express the functionality.
Maybe it should be renamed to something like resolveSymlinksToRealPaths or resolveSymlinks.

@powdream
Copy link
Contributor Author

powdream commented Sep 27, 2023

I think useRealPath as a name does not clearly express the functionality. Maybe it should be renamed to something like resolveSymlinksToRealPaths or resolveSymlinks.

Thanks for the suggestion.
I'd like to vote for resolveSymlinksToRealPaths even if it is longer than resolveSymlinks, since resolveSymlinks sounds a bit confused because of followSymlinks.
Then, can I go with resolveSymlinksToRealPaths?

@powdream powdream changed the title feat(fs): introduce useRealPath option to WalkOptions feat(fs): introduce resolveSymlinksToRealPaths option to WalkOptions Sep 27, 2023
@timreichen
Copy link
Contributor

Another one that comes into mind is resolveSymlinkPaths. realPath kinda suggests symlink paths not to be real. I think we should make a collection of possible names and do a poll on what would work best.

@powdream
Copy link
Contributor Author

powdream commented Sep 27, 2023

Then, let me list up possible candidates here:

  1. useRealPath: Not clear
  2. resolveSymlinksToRealPaths
  3. resolveSymlinks: Sounds similar to followSymlinks
  4. resolveSymlinkPaths
  5. resolvePaths
  6. replaceSymlinksWithRealPaths
  7. resolveSymlinksToCanonicalPaths
  8. canonicalize

@jollytoad
Copy link
Contributor

jollytoad commented Sep 29, 2023

resolveToCanonicalPaths?
or just canonical or canonicalize?

A canonical path is the general term used when a path has been normalized and all symlinks folllowed

@powdream
Copy link
Contributor Author

powdream commented Oct 1, 2023

I have no idea whether we can call the result of Deno.realPath() a canonical path. What do you think?

@jollytoad
Copy link
Contributor

It's a canonical path...
Deno.realPath would most probably be based on the underlying posix fn... realpath which states:

return the canonicalized absolute pathname

GNU even provides an alternative... canonicalize_file_name, as the posix realpath has a design flaw.

I agree with @timreichen that realpath is a bad name, but it's just an unfortunate history carried over from posix, many other languages and operating systems (including Windows) use the term canonical. Tbh, I'm surprised that Deno carried this legacy over and didn't seek to use a more accurate term like canonicalPath.

I think the choice should be down to either base the name on the correct term canonical, or on the name of the underlying fn call, ie. realPath ... throwing the terms follow or symlinks don't really help.

Just my humble opinion.

@powdream
Copy link
Contributor Author

powdream commented Oct 3, 2023

@jollytoad
Thanks for your opinion for this topic. It sounds reasonable. I wasn't aware of whether there is historical reason for the name. If we can use the term "canonical" path in this case, "resolveSymlinksToCanonicalPath" sounds very fair enough.

@jollytoad
Copy link
Contributor

tbh, i'd suggest canonicalize is probably enough, all other words are redundant.

@powdream
Copy link
Contributor Author

powdream commented Oct 5, 2023

I'd like to vote for the word normalize instead of canonicalize for consistency with other path-related APIs.
In path module, normalize function already exists but not canonicalize.

How does the following look to other guys?

walk(b, { followSymlinks: true, normalizeSymlinks: false })

@jollytoad
Copy link
Contributor

normalize means a different thing, it's to resolve '..' and '.' segments in the path, where-as canonicalize also does this AND resolves symlinks until the path references a concrete file. Deno has unfortunately used the questionable term realPath (based on the historical usage in the posix API) rather than the correct term canonicalPath. If it's a question of consistency with the existing API, which is perfectly valid, then the most sensible option is to use realPath.

normalize is not the right term here, the correct term is canonicalize, but a reasonable name could also be based on realPath as it's the underlying API used (but that is difficult to turn into a verb). Anyway, that's enough bike-shedding from me.

@powdream
Copy link
Contributor Author

powdream commented Oct 5, 2023

I'm persuaded. Then, let me address it.

@powdream powdream changed the title feat(fs): introduce resolveSymlinksToRealPaths option to WalkOptions feat(fs): introduce canonicalize option to WalkOptions Oct 5, 2023
Copy link
Member

@kt3k kt3k left a comment

Choose a reason for hiding this comment

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

Sorry for the delay in review.

LGTM. Thanks for your contribution!

@kt3k kt3k merged commit a62c908 into denoland:main Oct 16, 2023
@powdream powdream deleted the option-to-use-non-real-path branch October 16, 2023 21:32
@powdream powdream mentioned this pull request Oct 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants