Skip to content

Path relative to walkup - #19089

Merged
fdncred merged 11 commits into
nushell:mainfrom
maxim-uvarov:path-relative-to-walkup
Oct 9, 2026
Merged

fdncred merged 11 commits into
nushell:mainfrom
maxim-uvarov:path-relative-to-walkup

Conversation

@maxim-uvarov

@maxim-uvarov maxim-uvarov commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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 ...

> '/a/b/c' | path relative-to '/a/d/e' --walk-up
../../b/c
  • Opt-in, as @WindSoilder asked in Allow path relative-to to use parent directory symbols #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, feat: add vim.fs.relpath 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 Fix path relative-to for case-insensitive filesystems #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

Windows-only tests (the example output and the different-drive refusal) were not run locally.

claude and others added 2 commits September 25, 2026 22:23
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]>
@maxim-uvarov
maxim-uvarov marked this pull request as ready for review September 25, 2026 22:29
@fdncred
fdncred requested a balanced review from Copilot September 26, 2026 01:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@fdncred fdncred left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Notes from my clanker

if !relative.as_os_str().is_empty()
&& matches!(
path_rest.peek(),
Some(Component::RootDir | Component::Prefix(_) | Component::CurDir)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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  # ../a

expand_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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 1a8ebe8: a leading . is skipped on both sides, so all three examples return ../a. Side effect: without the flag, './a/b' | path relative-to 'a' now returns b instead of an error. f10d161 keeps '/a' | path relative-to '.' refused after that change.

/// 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() => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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/Foo

Linux 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in 4326b2d: the refusal now says which reason applies, and the .. error labels args.path.span. 11a3791 also checks different roots first, so '/a/b' | path relative-to 'a/../c' --walk-up names the roots instead of the ...

"prefix not found",
span,
)
.with_help("Use --walk-up to allow `..` components in the result.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done in ea739d3: on the error path it tries the walk-up and adds the hint only when that succeeds. 23472ff makes the path expand hint say "both paths", since expanding only the argument led to a second error.

claude and others added 9 commits September 26, 2026 04:15
`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]>
@fdncred
fdncred merged commit 8ca2234 into nushell:main Oct 9, 2026
14 checks passed
@fdncred

fdncred commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Thanks

@github-actions github-actions Bot added this to the 1.0 milestone Oct 9, 2026
@fdncred fdncred added notes:ready Indicates Ready for Release notes notes:additions Noted in "Additions" section labels Oct 10, 2026
@hustcer hustcer modified the milestones: 1.0, v0.117.0 Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:additions Noted in "Additions" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove "argument path needs to be a parent" limitation from "path relative-to"

5 participants