Skip to content

feat!: Explicit cell-path case sensitivity syntax - #15692

Merged
Bahex merged 18 commits into
nushell:mainfrom
Bahex:cell-path-sensitivity
May 18, 2025
Merged

Bahex merged 18 commits into
nushell:mainfrom
Bahex:cell-path-sensitivity

Conversation

@Bahex

@Bahex Bahex commented May 4, 2025 •

Copy link
Copy Markdown
Member

Related:

Description

Currently various commands have differing behavior regarding cell-paths

{a: 1, A: 2} | get a A
# => ╭───┬───╮
# => │ 0 │ 2 │
# => │ 1 │ 2 │
# => ╰───┴───╯
{a: 1, A: 2} | select a A
# => ╭───┬───╮
# => │ a │ 1 │
# => │ A │ 2 │
# => ╰───┴───╯
{A: 1} | update a 2
# => Error: nu::shell::column_not_found
# => 
# =>   × Cannot find column 'a'
# =>    ╭─[entry #62:1:1]
# =>  1 │ {A: 1} | update a 2
# =>    · ───┬──          ┬
# =>    ·    │            ╰── cannot find column 'a'
# =>    ·    ╰── value originates here
# =>    ╰────

Proposal: making cell-path access case-sensitive by default and adding new syntax for case-insensitive parts, similar to optional (?) parts.

{FOO: BAR}.foo
# => Error: nu::shell::name_not_found
# => 
# =>   × Name not found
# =>    ╭─[entry #60:1:21]
# =>  1 │ {FOO: BAR}.foo
# =>    ·            ─┬─
# =>    ·             ╰── did you mean 'FOO'?
# =>    ╰────
{FOO: BAR}.foo!
# => BAR

This would solve the problem of case sensitivity for all commands without causing an explosion of flags and make it more granular

Assigning to a field using a case-insensitive path is case-preserving.

mut val = {FOO: "I'm FOO"}; $val
# => ╭─────┬─────────╮
# => │ FOO │ I'm FOO │
# => ╰─────┴─────────╯
$val.foo! = "I'm still FOO"; $val
# => ╭─────┬───────────────╮
# => │ FOO │ I'm still FOO │
# => ╰─────┴───────────────╯

For update, case-insensitive is case-preserving.

{FOO: 1} | update foo! { $in + 1 }
# => ╭─────┬───╮
# => │ FOO │ 2 │
# => ╰─────┴───╯

insert can insert values into nested values so accessing into existing columns is case-insensitive, but creating new columns uses the cell-path as it is.
So insert foo! ... and insert FOO! ... would work exactly as they do without !

{FOO: {quox: 0}}
# => ╭─────┬──────────────╮
# => │     │ ╭──────┬───╮ │
# => │ FOO │ │ quox │ 0 │ │
# => │     │ ╰──────┴───╯ │
# => ╰─────┴──────────────╯
{FOO: {quox: 0}} | insert foo.bar 1
# => ╭─────┬──────────────╮
# => │     │ ╭──────┬───╮ │
# => │ FOO │ │ quox │ 0 │ │
# => │     │ ╰──────┴───╯ │
# => │     │ ╭─────┬───╮  │
# => │ foo │ │ bar │ 1 │  │
# => │     │ ╰─────┴───╯  │
# => ╰─────┴──────────────╯
{FOO: {quox: 0}} | insert foo!.bar 1
# => ╭─────┬──────────────╮
# => │     │ ╭──────┬───╮ │
# => │ FOO │ │ quox │ 0 │ │
# => │     │ │ bar  │ 1 │ │
# => │     │ ╰──────┴───╯ │
# => ╰─────┴──────────────╯

upsert is tricky, depending on the input, the data might end up with different column names in rows. We can either forbid case-insensitive cell-paths for upsert or trust the user to keep their data in a sensible shape.

This would be a breaking change as it would make existing cell-path accesses case-sensitive, however the case-sensitivity is already inconsistent and any attempt at making it consistent would be a breaking change.

What about $env?

  1. Initially special case it so it keeps its current behavior.
  2. Accessing environment variables with non-matching paths gives a deprecation warning urging users to either use exact casing or use the new explicit case-sensitivity syntax
  3. Eventuall remove $env's special case, making $env accesses case-sensitive by default as well.

$env.ENV_CONVERSIONS?

In addition to from_string and to_string add an optional field to opt into case insensitive/preserving behavior.

User-Facing Changes

  • get, where and other previously case-insensitive commands are now case-sensitive by default.
  • get's --sensitive flag removed, similar to --ignore-errors there is now an --ignore-case flag that treats all parts of the cell-path as case-insensitive. --sensitive is now a no-op and gives a deprecation warning when used. A flag that that makes get treat all members of the cell-path as case-insensitive will likely be added later.
  • Users can explicitly choose the case case-sensitivity of cell-path accesses or commands.

Tests + Formatting

Existing tests required minimal modification. However, new tests are not yet added.

  • 🟢 toolkit fmt
  • 🟢 toolkit clippy
  • 🟢 toolkit test
  • 🟢 toolkit test stdlib

After Submitting

Bahex added 10 commits May 4, 2025 16:37
- Add `CellPath::String::insensitive: bool`, represented with an exclamation
  mark in `Display`
- Update `into cell-path` and `split cell-path` to take case
  insensitivity into account while constructing and splitting cell-paths
- remove `insensitive` parameter, case sensitivity is determined by the
  cell-path itself
- update call sites
can round trip `$. | to nuon | from nuon`
- remove `--sensitive(-s)`
- add `--ignore-case(-I)`
…tivity

Make case sensitivity explicit in various places.
@github-actions github-actions Bot added A:parser Issues related to parsing deprecated:pr-plugins Deprecated: use the A:plugins label instead A:LSP Language Server Protocol (nu-lsp) labels May 4, 2025
@Bahex Bahex added category:enhancement New feature or request category:inconsistent-behavior Behavior between different commands or types inconsistent/unexpected notes:breaking-changes Noted in Breaking Changes section A:environment Related to the management of environment variables/process state A:syntax Changes to the grammar or syntax beyond parser bugfixes deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way deprecated:pr-language language changes A:cell-path-semantics All around the cell path type and its semantics for access and removed deprecated:pr-plugins Deprecated: use the A:plugins label instead A:LSP Language Server Protocol (nu-lsp) labels May 4, 2025
@NotTheDr01ds

NotTheDr01ds commented May 5, 2025 •

Copy link
Copy Markdown
Contributor

I really like the current get (and its cell-path access shorthand) case-insensitive behavior, at least for $env (and possible others). If I want to check the path, I like that I can:

$env.path
# or
$env | get path

... and it works on all platforms. I think this is good shell behavior. Let's not deprecate or change that behavior, please? The user can opt-in to case-sensitivity already using get --sensitive.

I've got to run for a bit, but I'm looking through the other commands trying to see - Are there others besides get and its equivalent cell-path access that aren't case sensitive?

@Bahex

Bahex commented May 5, 2025

Copy link
Copy Markdown
Member Author

@NotTheDr01ds This PR makes all cell-path accesses case sensitive by default (with the exception of $env).

# this is special cased and will continue to work
$env.path

# unfortunately I couldn't special case this one as once `$env` is piped as a value,
# it is just another record
$env | get path

The user can opt-in to case-sensitivity already using get --sensitive.

Yes, but there wasn't any way to specify case-sensitivity for other commands or direct cell-path accesses.
Also, the other commands were arbitrary in whether or not they were case-sensitive.

The new behavior is consistent across board and allows both case-sensitivity and case-insensitivity in places they couldn't use before.

@NotTheDr01ds

NotTheDr01ds commented May 5, 2025 •

Copy link
Copy Markdown
Contributor

This PR makes all cell-path accesses case sensitive by default (with the exception of $env).

I'm concerned, especially, about this statement, though:

  • Accessing environment variables with non-matching paths gives a deprecation warning urging users to either use exact casing or use the new explicit case-sensitivity syntax
  • Eventuall remove $env's special case, making $env accesses case-sensitive by default as well.

I'm not sure I like removing cell-path and get case-insensitivity in general, though. Having those work differently was definitely an intentional feature. I might could live with it if it were only for $env going forward (with no warning message), but I'm still noodling on that part.

If we want to strive for consistency, I'd propose making all commands case-insensitive by default and opt-in to the strict behavior with a --match-case, --case, or --sensitive flag. This allows the user to have the "simple" option as the most convenient in the shell, and yet still opt-in to the stricter behavior when needed for robustness in scripts.

Also, the other commands were arbitrary in whether or not they were case-sensitive.

Which ones were case-insensitive other than get and cell-path access? I'd probably be okay with changing those.

I'd probably be okay with cell-path and get being case-insensitive by default and all others being case-sensitive. I kind of thought that's what we had now?

@132ikl

132ikl commented May 5, 2025 •

Copy link
Copy Markdown
Member

I'm not sure about making $env accesses case sensitive even in the long term, but that whole topic is a can of worms which we still don't seem to really have any clear path forward with yet.

I see where @NotTheDr01ds is coming from with the case-insensitive cell path accesses being nice (I've written scripts which take advantage of this myself). Maybe we should flip it around, so that cell path accesses are case-insensitive by default, but adding the ! makes it case-sensitive? This would also fit with the "case-preserving" environment accesses, so no special-casing needed. Then again, making it insensitive by default might be a surprising "gotcha" users have to be on the lookout for, whereas sensitive by default is least-surprise but less convenient. Data with case-sensitive keys is kind of an edge case, which I think favors insensitive-by-default, but I also really don't like introducing potential footguns into the language. (so, in summary, I am not sure which direction we should go 😄)

@Bahex

Bahex commented May 5, 2025

Copy link
Copy Markdown
Member Author

I consider it very similar to optional cell-path syntax.

Trying to access a non existent field throws an error, if we want to relax it we just add a ?.
I've never found myself frustrated or confused because of that, If I mistyped something I fix it or if the data is missing in some places I just add the question mark and continue with what I'm doing.

To me, it's very similar with case sensitivity. If I mistyped something the error suggests the correct column name and I can fix it or if I don't want to deal with case sensitivity I can just add !.

We can very similarly suggest that "trying to access non existent fields or columns with holes should just return null" for convenience, and I think that would be similarly wrong.

Currently some commands that only read data like get and where are case insensitive, commands that modify data like insert, update are case sensitive.

select and reject are case sensitive.

Adding flags to all relevant commands for turning sensitivity on/off is both a lot of clutter, and not granular enough. Whether the default is sensitive or not, there should be way to specify case sensitivity for each column.

The result of the current approach of insensitive when reading (sometimes), and sensitive when writing is this:

mut value = {}
$value.foo = "Hello"
$value
# => ╭─────┬───────╮
# => │ foo │ Hello │
# => ╰─────┴───────╯

$value.FOO += " World"
$value
# => ╭─────┬─────────────╮
# => │ foo │ Hello       │
# => │ FOO │ Hello World │
# => ╰─────┴─────────────╯

Making case (in)sensitivity a part of the cell-path itself rather than something the operations choose fixes a lot of inconsistencies at once.

@cptpiepmatz

Copy link
Copy Markdown
Member

I like the idea of making all cell paths consistent and dropping most of the case sensitivity flags. I never used them myself, so I don't mind seeing them go.

On the question of whether we want insensitivity or sensitivity by default, I'd go with sensitivity. Kinda like mut in Rust, you only add it when needed. It keeps things more strict by default. I see this suffix operator working the same way: loosen it up only when necessary.

Not totally sure if ! is the best pick though. I only know it from TypeScript, where it means "not nullish", which feels a bit different. No idea how other languages handle stuff like this. Purely looking at math symbols ~ would represent more a "like" or similar.

@fdncred

fdncred commented May 12, 2025

Copy link
Copy Markdown
Contributor

We decided to proceed with this PR in last week's meeting after these changes had taken place
https://hackmd.io/GKPfIh0ySoGM6g-KzM7fpw#cell-path-access

@github-actions github-actions Bot added deprecated:pr-plugins Deprecated: use the A:plugins label instead A:LSP Language Server Protocol (nu-lsp) labels May 13, 2025
@cptpiepmatz
cptpiepmatz self-requested a review May 15, 2025 15:47
Comment thread crates/nu-protocol/src/ast/cell_path.rs Resolved
Comment thread crates/nu-protocol/src/value/record.rs Resolved
@Bahex
Bahex requested a review from cptpiepmatz May 18, 2025 06:22
@azarmadr

Copy link
Copy Markdown

Could have added a deprecation warning whenever the old behavior was expected, would have given users some chance to make changes before submerged in errors. 👍

koffydrop added a commit to koffydrop/home-manager that referenced this pull request Jun 29, 2025
After nushell/nushell#15692, this integration breaks
$PATH conversions again as cell-paths are now case-sensitive.
koffydrop added a commit to koffydrop/home-manager that referenced this pull request Jun 30, 2025
After nushell/nushell#15692, this integration breaks
$PATH conversions again as cell-paths are now case-sensitive.
khaneliman pushed a commit to nix-community/home-manager that referenced this pull request Jun 30, 2025
After nushell/nushell#15692, this integration breaks
$PATH conversions again as cell-paths are now case-sensitive.
dwb pushed a commit to dwb/home-manager that referenced this pull request Sep 24, 2025
After nushell/nushell#15692, this integration breaks
$PATH conversions again as cell-paths are now case-sensitive.
@Bahex
Bahex deleted the cell-path-sensitivity branch March 22, 2026 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A:cell-path-semantics All around the cell path type and its semantics for access A:environment Related to the management of environment variables/process state A:LSP Language Server Protocol (nu-lsp) A:parser Issues related to parsing A:syntax Changes to the grammar or syntax beyond parser bugfixes category:enhancement New feature or request category:inconsistent-behavior Behavior between different commands or types inconsistent/unexpected deprecated:pr-commands (deprecated: too vague) This PR changes our commands in some way deprecated:pr-language language changes deprecated:pr-plugins Deprecated: use the A:plugins label instead notes:breaking-changes Noted in Breaking Changes section

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants