Repository navigation
Change environment variables to be case-preserving - #12701
Conversation
|
Hopefully this will close this issue once and for all. My only question is whether all supported operating systems should be allowed case-insensitive access to env vars? |
|
I'll mention this PR here since it implemented the same thing but hasn't been finished: #11432 |
We already have
I was aware of that. The author of that PR has abandoned it for quite a while now. |
|
I just noticed that |
|
I think the challenging behaviour here is that we usually try to make sure that the same Nushell scripts can run on all operating systems, as long as they're not written in a very OS-specific way. So having this just be the case for Windows would make it possible for a Windows-based developer to write a script that won't work on other OSes, because they assume they can just do e.g. I think if we're going to do it for Windows we probably need to do it for everyone. It's probably not the worst thing though - even though environment variables are typically case sensitive on Unixes, I can't think of single case where that actually matters to someone trying to read an environment variable. Worst case, they can use |
I think "cross-platform support" doesn't necessary means "the same script always has the same behavior on every OS". For example, filenames on Windows are case-insensitive, so a script that writes to This PR makes it so that That said, it would be nice to improve documentation and/or issue a warning when |
Correct, it probably doesn't necessarily mean that but that's what we aim for with nushell. We want everything possible to work across MacOS, Linux, and Windows. Some things aren't possible, but I think this one is. I agree with devyn and would prefer this to work the same across platforms. We'd appreciate it if you can please make that one additional change. I think we'd be happy to land this if that happens because many people have been "bitten" by this issue where the path env var doesn't work the same across platforms. |
|
OK, that's fine with me. |
|
I've opened a PR on the book that explains the new behavior. |
|
Thanks. Excited to use this! |
|
Thanks for this PR, I think this is great addition. One question/problem that I'll submit to everyone is: how should the case preservation interact with the other methods of setting environment variables:
Tagging you as well @sholderbach, since you worked on the recent environment changes. |
|
@IanManske My vote is for those commands to be case preserving. If a user wants |
|
I was thinking that as well, but the issue is after, for example, |
|
hmmm, seems like case preserving would allow both FOO and foo, but I wonder if that would be confusing? |
|
Personally I think it would be most cross platform to have it use the case of an existing var if there is one. Otherwise |
I think it's supposed to work this way now. I can also do |
|
I think we should make it an invariant that "$env cannot contains keys like
This does mean that Nushell cannot read both |
|
As we currently reject duplicate env vars passed to Is the change of implementing the Windows-based case-preserving behavior on the level of Nushell causing problems for folks trying to interact with edge cases on Unix? |
|
I do worry that being overly strict with it would cause issues for someone trying to do something that is possible on Unix. I don't think it should be impossible to add both to env but I think it should have to be very intentional |
While we do have |
|
Well, Record cell path updates are normally sensitive I think, but I haven't confirmed that |
|
Has any thought been put into how this update interacts with nushell/crates/nu-utils/src/sample_config/default_env.nu Lines 65 to 74 in c54d223 nushell/crates/nu-engine/src/env.rs Lines 341 to 347 in c54d223 Which one gets called, maybe both can if it's case-preserving, but I'd hope that record can also benefit from insensitive lookups. |
Currently it doesn't affact ENV_CONVERSIONS at all. We still use case-sensitive comparision to convert to/from the environment. I wonder if that's OK? |
|
In my perfect world, no one would have to ever know or have to remember what case path needs to be in, whether it is PATH or Path or PaTh or path, all would work everywhere equally. Nushell would know and update the environment appropriately. |
Related: - #15683 - #14551 - #849 - #12701 - #11527 # Description Currently various commands have differing behavior regarding cell-paths ```nushell {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. ```nushell {FOO: BAR}.foo # => Error: nu:🐚: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. ```nushell 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. ```nushell {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 `!` ```nushell {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. - 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 - Update the website to include the new syntax - Update [tree-sitter-nu](https://github.com/nushell/tree-sitter-nu) --------- Co-authored-by: Bahex <[email protected]>
This PR changes
$envto be case-preserving instead of case-sensitive. That is, it preserves the case of the environment variable when it is first assigned, but subsequent retrieval and update ignores the case.Notably, both
$env.PATHand$env.Pathcan now be used to read or set the environment variable, but child processes will always see the correct case based on the platform.Fixes #11268.
This feature was surprising simple to implement, because most of the infrastructure to support case-insensitive cell path access already exists. The
getcommand extracts data using a cell path in a case-insensitive way (!), but accepts a--sensitiveflag. (I think this should be flipped around?)