Repository navigation
Fix path relative-to for case-insensitive filesystems - #16310
Conversation
WindSoilder
left a comment
There was a problem hiding this comment.
Thank you for the fix! Just left some ideas on try_case_insensitive_strip_prefix
|
For reference what Microsoft does in .NET for getting relative paths: https://github.com/dotnet/runtime/blob/3f25a276772b8d0e54452321e7525b7a176aa4a7/src/libraries/System.Private.CoreLib/src/System/IO/Path.cs#L859 |
WindSoilder
left a comment
There was a problem hiding this comment.
Thanks, LGTM!
I found out MS handles something like C:\ and relative path(like ../) at the same time, I don't think we need such complexity for now
| } | ||
|
|
||
| /// Check if the current filesystem is typically case-insensitive | ||
| fn is_case_insensitive_filesystem() -> bool { |
There was a problem hiding this comment.
I'm not sure if hardcoding this based on the platform is a good idea. You can access NTFS or APFS from Linux, isn't there a better way to check the path belonging to a case insensitive fs?
There was a problem hiding this comment.
It might be better to make this configurable in some way. On Windows at least, case-sensitivity can be set per-directory. But I would not expect relative-to to need to stat each directory in the path (e.g. a path may not (yet) exist or it might be inaccessible).
There was a problem hiding this comment.
I did consider this issue earlier, but opted for this approach as it's the simplest and has minimal performance impact. If you have better ideas, feel free to refine it further
Disclosure: the code was written by an AI agent (Claude) under my direction; I can't review Rust line-by-line myself. ## Description In this PR, I added a `--walk-up` flag to `path relative-to`: when the argument path is not a parent of the input, its leftover components become `..`. ```nu > '/a/b/c' | path relative-to '/a/d/e' --walk-up ../../b/c ``` - **Opt-in**, as @WindSoilder asked in #14055, following Python's `relative_to(walk_up=True)`. Without the flag, the error still works as an "is inside" check (Neovim kept `vim.fs.relpath` parent-only for this reason, neovim/neovim#31790). Spelled `--walk-up` rather than the proposed `--walkup`, to match nu-command's kebab-case flags. - **Symlinks** (@sholderbach's question): the command stays lexical and `const`; the help text warns that a symlink in the argument path can make `..` point elsewhere. - **Refused with `--walk-up`:** absolute mixed with relative, different Windows drives, and `.` or `..` in the argument after the shared part — none of these can be answered from the text alone. - One component walk replaces `strip_prefix` plus the case-insensitive fallback from #16310, so case folding applies to `--walk-up` too. - Output without the flag changes in three cases: a leading ./ is ignored ('./a/b' | path relative-to 'a' returns b instead of an error); an absolute input against an empty base is refused ('/a' | path relative-to '' returned /a); and a pair with different roots is refused as "different roots" with a hint, instead of "not a parent". The error hints at `--walk-up` only when the flag would give a result. Easier to review commit by commit: the first is a refactor with no behaviour change, the second adds the flag on top of it. ## User-facing changes (Release notes) `path relative-to` has a new `--walk-up` flag that returns a path with `..` when the argument path is not a parent of the input: `'/a/b/c' | path relative-to '/a/d/e' --walk-up` returns `../../b/c`. ## Additional notes - closes #13969 - supersedes #14055 (closed unmerged after going stale) - related #16310, neovim/neovim#31790 Windows-only tests (the example output and the different-drive refusal) were not run locally. --------- Co-authored-by: Claude <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
fixes #16205
Description
Before:
After:
For Windows and macOS:
Release notes summary
path relative-toworks better for case-insentitive filesystems, this works onWindowesormacOS:"/etc" | path relative-to "/Etc"