Repository navigation
Path relative to walkup - #19089
Path relative to walkup#19089
Conversation
Replaces strip_prefix and the separate case-insensitive fallback (try_case_insensitive_strip_prefix) with one walk over the components, whose comparison folds case on Windows and macOS. Output is unchanged; the existing tests cover both the exact and the case-folded match. Split out of the `--walk-up` change so reviewers can check this step for equivalence on its own: the next commit only extends this walk. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes nushell#13969. Revives the idea of nushell#14055, which was closed unmerged after going stale. As the maintainers asked there, walking up is opt-in: without the flag, the error still works as a lexical "is descendant" check (the same reason Neovim's vim.fs.relpath returns nil). With the flag, the output matches Python's relative_to(walk_up=True). The flag is kebab-case, like most multi-word flags in nu-command. pathdiff::diff_paths is not reused: it compares case-sensitively and does not reject a `..` left in the base or a different Windows drive. With the flag, the result is refused when a leftover base component is `.` or `..` (it cannot be undone without the filesystem), or when the rest of the input starts at a root, a Windows prefix or a `.`: pushing a root onto the `..` chain would replace it, and PathBuf::push drops a `.`, so `./a` relative to `b` would silently become `../a`. Normalizing both paths lexically first would remove these refusals, but it changes results, so it was not done. The help text states that the result is lexical, so a symlink in the base can make it point elsewhere (checked on disk). Co-Authored-By: Claude Opus 5.5 <[email protected]>
| if !relative.as_os_str().is_empty() | ||
| && matches!( | ||
| path_rest.peek(), | ||
| Some(Component::RootDir | Component::Prefix(_) | Component::CurDir) |
There was a problem hiding this comment.
A leading . on only one side makes --walk-up fail, even though both paths are relative to the same cwd:
'./a' | path relative-to 'b' --walk-up # error: different roots
'a' | path relative-to './b' --walk-up # error, via the CurDir -> None mapping at line 208
'./a' | path relative-to './b' --walk-up # ../aexpand_to_real_path keeps the leading ./, so this will show up with paths people type. Skipping a leading CurDir on both sides before the shared-prefix loop would make all three return ../a. The walk_up("./a", "b") == None assertion in the tests would need to change with it.
| /// Compares two path components, ignoring case of names on case-insensitive filesystems. | ||
| fn components_eq(a: &Component, b: &Component) -> bool { | ||
| match (a, b) { | ||
| (Component::Normal(a), Component::Normal(b)) if is_case_insensitive_filesystem() => { |
There was a problem hiding this comment.
is_case_insensitive_filesystem() guesses from the target OS. It doesn't look at the actual volume. The guess was already here before this PR, and it could already give a wrong answer. With --walk-up it also decides how many .. components come out, so a wrong guess now builds a .. chain that lands in the wrong directory.
On a case-sensitive APFS volume, where /v/Foo and /v/foo are different directories:
'/v/Foo' | path relative-to '/v/foo/bar' --walk-up # returns `..`, which is /v/foo, not /v/FooLinux with a case-insensitive vfat/ntfs mount has the opposite problem.
One option is to compare names exactly when walking up. '/v/Foo' relative to '/v/foo/bar' would then give ../../Foo, which resolves correctly on both kinds of filesystem. It's just not the shortest answer on a case-insensitive one. The existing case-insensitive prefix match without --walk-up could stay as it is.
There was a problem hiding this comment.
Done in 2a2538e: names are compared exactly when walking up, so '/v/Foo' | path relative-to '/v/foo/bar' --walk-up gives ../../Foo. addb442 then tries the case-insensitive match first, and compares names exactly only when .. has to be added. That way the flag never changes an answer the plain command already gives.
| fn components_eq(a: &Component, b: &Component) -> bool { | ||
| match (a, b) { | ||
| (Component::Normal(a), Component::Normal(b)) if is_case_insensitive_filesystem() => { | ||
| a.to_string_lossy().to_lowercase() == b.to_string_lossy().to_lowercase() |
There was a problem hiding this comment.
Two smaller things on this line.
On macOS and Windows this now does two lossy conversions and two to_lowercase() calls for every Normal component pair, even when the names are byte-for-byte equal. The old code tried a zero-allocation strip_prefix first and only lowercased on a miss. Checking a == b || before the lowercase comparison gets most of that back, which matters for something like ls **/* | get name | path relative-to $root.
Also, to_string_lossy() turns invalid UTF-8, or unpaired surrogates on Windows, into U+FFFD, so two different non-UTF-8 names compare equal. The old fallback had the same issue, but it's now on the main path and feeds the .. count.
There was a problem hiding this comment.
Done in 75c3416, as you suggested: a == b first, then the same to_lowercase comparison as before. Names that are not valid UTF-8 are compared exactly, so two different ones no longer both become U+FFFD.
| None if args.walk_up => Value::error( | ||
| GenericError::new( | ||
| String::from("Cannot walk up from the argument path to the input path."), | ||
| "the paths have different roots, or the argument path has `.` or `..` after the shared part", |
There was a problem hiding this comment.
This message also fires when the problem is on the input side. './a' | path relative-to 'b' --walk-up says the argument path has . or .., but b has neither.
Both errors here also label the input value's span. Since this one is about the argument, args.path.span would point at the thing the user needs to change.
| "prefix not found", | ||
| span, | ||
| ) | ||
| .with_help("Use --walk-up to allow `..` components in the result.") |
There was a problem hiding this comment.
This hint shows up in cases where --walk-up fails too, for example '/a/b' | path relative-to 'a', 'a/b' | path relative-to 'a/../c', or different drive letters on Windows. Adding the flag then just gives a different error. Since this is the error path, calling relative_path(&lhs, &rhs, true) and only adding the hint when it returns Some would be cheap.
`expand_to_real_path` keeps a leading `./`, so `'./a' | path relative-to 'b' --walk-up` failed as if the paths had different roots, while both are relative to the same current directory. Skip a leading `CurDir` on both sides before the shared-part walk. `components()` yields `.` only first, so the `CurDir` refusal after the walk is no longer needed. Addresses review comment: nushell#19089 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
`is_case_insensitive_filesystem()` guesses from the target OS, not the actual volume. With `--walk-up` a wrong guess changes how many `..` are emitted: on case-sensitive APFS, `'/v/Foo' | path relative-to '/v/foo/bar' --walk-up` gave `..`, which is `/v/foo`, not `/v/Foo`. Compare names exactly when walking up. The result is then correct on any filesystem, at worst longer (`/etc` relative to `/Etc` is `../etc`). Without the flag the case folding stays as before. Addresses review comment: nushell#19089 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
`components_eq` did two `to_string_lossy()` and two `to_lowercase()` for every pair of names, even identical ones, and the lossy conversion turned different non-UTF-8 names into the same U+FFFD, so they compared equal. Check exact equality first, as the review suggested, and fold case only for names that are valid UTF-8. The fold stays main's whole-string `to_lowercase`. A per-character, allocation-free fold was tried and changed results for the Greek final sigma and for `ß`, so it is not worth the saving on the mismatch path. Addresses review comment: nushell#19089 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
The walk-up error said "the argument path has `.` or `..`" also when the problem was on the input side, and it always labelled the input value. Make `relative_path` return the reason it refused: the paths have different roots, or the argument has a `..` after the shared part. Each reason gets its own message and help, and the `..` one labels the argument path, which is what the user needs to change. Addresses review comment: nushell#19089 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
The "Use --walk-up" hint also appeared for pairs that `--walk-up` refuses too, such as `'/a/b' | path relative-to 'a'` or an argument with `..`, so following it only led to another error. On this error path, try the walk up and add the hint only when it succeeds. Addresses review comment: nushell#19089 (comment) Co-Authored-By: Claude Opus 5.5 <[email protected]>
Comparing names exactly whenever the flag was set made the flag change results the plain command already gives: on macOS and Windows, `'/Etc/x' | path relative-to '/etc'` returned `x`, but with `--walk-up` it returned `../Etc/x`. The flag is meant to only add results where the plain command fails. So try the plain, case-folded match first, and compare names exactly only when `..` has to be added; that still fixes the `/v/Foo` case from the review. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`'/a' | path relative-to '.'` returned `/a`, with or without --walk-up, while main refused it. Skipping the leading `.` left the base with no components, and the root/prefix guard only ran when `..` had been emitted, so the absolute rest of the input was pushed as the "relative" result. The guard now runs always: a root or prefix left at the start of the input means the base had none to match it. `'/a' | path relative-to ''` is now refused too; main returned `/a` there, which contradicted "both absolute or both relative". Since the plain command can now reach this refusal, its title no longer speaks of walking up. Found by a multi-agent correctness review of the branch. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The help said to run `path expand` on the argument path only. With a
relative input that leads straight into the next error:
`'a/b' | path relative-to ('a/../c' | path expand) --walk-up` refuses
because one path is absolute and the other relative. Expanding both
paths gives `../a/b`.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
The root check ran on the rest of the input path only, and after the `NotParent` return. So a mixed pair got an answer that depended on the flag and the base, not on the paths: `'/a/b' | path relative-to 'a'` said "not a parent" (a false claim, with no help), the same pair with --walk-up said "different roots", and `'/a' | path relative-to '.'` said "different roots" without the flag. `C:\a` against `D:\a` without the flag also said "not a parent". With --walk-up, `'/a/b'` against `'a/../c'` blamed the `..`, though the two paths share nothing. Now a root or a Windows prefix at the start of either leftover is refused as different roots, first, in one place. What is left after it is only names and `..`, so the `..` map no longer carries a roots arm of its own. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks |
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-upflag topath relative-to: when the argument path is not a parent of the input, its leftover components become...relative_to(walk_up=True). Without the flag, the error still works as an "is inside" check (Neovim keptvim.fs.relpathparent-only for this reason, feat: add vim.fs.relpath neovim/neovim#31790). Spelled--walk-uprather than the proposed--walkup, to match nu-command's kebab-case flags.const; the help text warns that a symlink in the argument path can make..point elsewhere.--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.strip_prefixplus the case-insensitive fallback from Fix path relative-to for case-insensitive filesystems #16310, so case folding applies to--walk-uptoo.--walk-uponly 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-tohas a new--walk-upflag 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-upreturns../../b/c.Additional notes
Windows-only tests (the example output and the different-drive refusal) were not run locally.