Skip to content

Fix path relative-to for case-insensitive filesystems - #16310

Merged
WindSoilder merged 5 commits into
nushell:mainfrom
hustcer:feature/path-relativeTo
Aug 4, 2025
Merged

WindSoilder merged 5 commits into
nushell:mainfrom
hustcer:feature/path-relativeTo

Conversation

@hustcer

@hustcer hustcer commented Jul 31, 2025 •

Copy link
Copy Markdown
Contributor

fixes #16205

Description

  1. Adds fallback: On case-insensitive filesystems (Windows, macOS), falls back to case-insensitive comparison when the standard comparison fails
  2. Maintains filesystem semantics: Only uses case-insensitive comparison on platforms where it's appropriate

Before:

$> "/etc" | path relative-to "/Etc"
Error: nu::shell::cant_convert

  × Can't convert to prefix not found.
   ╭─[entry #33:1:1]
 1 │ "/etc" | path relative-to "/Etc"
   · ───┬──
   ·    ╰── can't convert string to prefix not found
   ╰────

After:

For Windows and macOS:

$> "/etc" | path relative-to "/Etc" | debug -v
""

Release notes summary

path relative-to works better for case-insentitive filesystems, this works on Windowes or macOS: "/etc" | path relative-to "/Etc"

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

Thank you for the fix! Just left some ideas on try_case_insensitive_strip_prefix

@hustcer hustcer added notes:breaking-changes Noted in Breaking Changes section notes:fixes Noted in "Bug fixes" section labels Aug 3, 2025
@sholderbach

Copy link
Copy Markdown
Member

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

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

@WindSoilder
WindSoilder merged commit 9f4c3a1 into nushell:main Aug 4, 2025
@github-actions github-actions Bot added this to the v0.107.0 milestone Aug 4, 2025
@hustcer
hustcer deleted the feature/path-relativeTo branch August 4, 2025 14:51
}

/// Check if the current filesystem is typically case-insensitive
fn is_case_insensitive_filesystem() -> bool {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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?

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.

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

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.

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

@Bahex Bahex added notes:ready Indicates Ready for Release notes and removed notes:breaking-changes Noted in Breaking Changes section labels Aug 30, 2025
fdncred pushed a commit that referenced this pull request Oct 9, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:fixes Noted in "Bug fixes" section notes:ready Indicates Ready for Release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

path relative-to does not work as expected with case-insensitive paths on case-insensitive filesystems

6 participants