Skip to content

Change environment variables to be case-preserving - #12701

Merged
fdncred merged 8 commits into
nushell:mainfrom
yizhepku:case-sensitive-env
May 1, 2024
Merged

fdncred merged 8 commits into
nushell:mainfrom
yizhepku:case-sensitive-env

Conversation

@yizhepku

@yizhepku yizhepku commented Apr 29, 2024 •

Copy link
Copy Markdown
Contributor

This PR changes $env to 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.PATH and $env.Path can 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 get command extracts data using a cell path in a case-insensitive way (!), but accepts a --sensitive flag. (I think this should be flipped around?)

@fdncred

fdncred commented Apr 29, 2024

Copy link
Copy Markdown
Contributor

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?

@kubouch

kubouch commented Apr 29, 2024

Copy link
Copy Markdown
Contributor

I'll mention this PR here since it implemented the same thing but hasn't been finished: #11432

@yizhepku

Copy link
Copy Markdown
Contributor Author

My only question is whether all supported operating systems should be allowed case-insensitive access to env vars?

We already have get and get --sensitive if the user wants to explictly choose between case-sensitive and case-insensitive. Do you mean case-insensitive by default on all systems? Is there a good reason for that? I feel like it would be surprising for non-Windows users.

I'll mention this PR here since it implemented the same thing but hasn't been finished: #11432

I was aware of that. The author of that PR has abandoned it for quite a while now.

@yizhepku

Copy link
Copy Markdown
Contributor Author

I just noticed that $env.0 = "foo" doesn't work, even though the original implementation intended it. This is probably a bug. It is unrelated to this PR though, it already breaks without this PR.

@devyn

devyn commented Apr 30, 2024

Copy link
Copy Markdown
Contributor

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. $env.path to get the PATH var, which would work for them but not elsewhere.

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 get and make it explicit.

@yizhepku

Copy link
Copy Markdown
Contributor Author

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. $env.path to get the PATH var, which would work for them but not elsewhere.

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 foo.txt and then reads from FOO.txt behaves differently depending on the platform. I wouldn't try to make filenames case-insensitive on every platform just so that the behavior is the same.

This PR makes it so that $env.PATH works on all platforms. Of course there are ways to write things that don't work on all platforms, but I think that's part of the responsibility of the script author.

That said, it would be nice to improve documentation and/or issue a warning when $env.path is used instead of $env.PATH.

@fdncred

fdncred commented Apr 30, 2024 •

Copy link
Copy Markdown
Contributor

I think "cross-platform support" doesn't necessary means "the same script always has the same behavior on every OS".

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.

@fdncred
fdncred marked this pull request as draft April 30, 2024 11:52
@yizhepku

Copy link
Copy Markdown
Contributor Author

OK, that's fine with me.

@yizhepku yizhepku changed the title Change environment variables to be case-preserving on Windows Change environment variables to be case-preserving Apr 30, 2024
@yizhepku
yizhepku marked this pull request as ready for review April 30, 2024 12:38
@yizhepku

Copy link
Copy Markdown
Contributor Author

I've opened a PR on the book that explains the new behavior.

@fdncred

fdncred commented May 1, 2024

Copy link
Copy Markdown
Contributor

Thanks. Excited to use this!

@IanManske

IanManske commented May 3, 2024 •

Copy link
Copy Markdown
Member

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:

  1. with-env and the env shorthand env_var=value command. E.g., in FOO=bar foo=bar command which case is put into the environment, or are both FOO and foo put into $env?
  2. load-env, same spiel: load-env { FOO: bar, foo: bar }.
  3. loading the initial environment on startup. Though unlikely, two different environment variables that only differ in case are possible on unix, so how are these two variables translated into the initial $env?

Tagging you as well @sholderbach, since you worked on the recent environment changes.

@fdncred

fdncred commented May 3, 2024

Copy link
Copy Markdown
Contributor

@IanManske My vote is for those commands to be case preserving. If a user wants foo instead of FOO it would be nice to allow that.

@IanManske

IanManske commented May 3, 2024 •

Copy link
Copy Markdown
Member

I was thinking that as well, but the issue is after, for example, load-env { FOO: bar, foo: bar }, what does $env contain? Does it contain FOO, foo, or both? (Sorry if I wasn't clear before.) Currently, load-env and with-env do not allow duplicate keys/columns/env variables.

@fdncred

fdncred commented May 3, 2024

Copy link
Copy Markdown
Contributor

hmmm, seems like case preserving would allow both FOO and foo, but I wonder if that would be confusing?

@devyn

devyn commented May 3, 2024

Copy link
Copy Markdown
Contributor

Personally I think it would be most cross platform to have it use the case of an existing var if there is one. Otherwise $env.path = would work on windows but not unixes

@fdncred

fdncred commented May 3, 2024 •

Copy link
Copy Markdown
Contributor

Personally I think it would be most cross platform to have it use the case of an existing var if there is one. Otherwise $env.path = would work on windows but not unixes

I think it's supposed to work this way now. $env.path works on macos. $env.foo = "test" works fine on macos too and it has a lowercase foo but if I then do $env.FOO = "test2" it reassigns foo to test2. I think that's right too because all env vars are case preserving now. So, now that I think more about it, we shouldn't be able to have FOO and foo at the same time.

I can also do $env.path | append foo and it updates $env.PATH on macos, which is case-preserving and working the way I think it should.

@yizhepku

yizhepku commented May 4, 2024

Copy link
Copy Markdown
Contributor Author

I think we should make it an invariant that "$env cannot contains keys like FOO and foo at the same time". This would be the simplest option.

with-env and load-env should put only one of the variables into $env. When loading the initial environment on Unix, only one of the variables should be loaded.

This does mean that Nushell cannot read both FOO and foo from the environment, but such cases are rare. I think it's worth it.

@sholderbach

Copy link
Copy Markdown
Member

As we currently reject duplicate env vars passed to with-env and load-env with the unique key semantics of record they would have to be changed to error out when the passed record violates whatever semantics we end up with here.

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?

@devyn

devyn commented May 5, 2024

Copy link
Copy Markdown
Contributor

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

@yizhepku

yizhepku commented May 5, 2024

Copy link
Copy Markdown
Contributor Author

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 get --sensitive for case-sensitive read, AFAIK there's no case-sensitive write in Nushell yet. Without that, it wouldn't be very useful to have both cases in $env.

@devyn

devyn commented May 5, 2024

Copy link
Copy Markdown
Contributor

Well, $env is kind of special. Perhaps with-env can take --sensitive

Record cell path updates are normally sensitive I think, but I haven't confirmed that

@Dorumin

Dorumin commented May 7, 2024

Copy link
Copy Markdown
Contributor

Has any thought been put into how this update interacts with ENV_CONVERSIONS?

$env.ENV_CONVERSIONS = {
"PATH": {
from_string: { |s| $s | split row (char esep) | path expand --no-symlink }
to_string: { |v| $v | path expand --no-symlink | str join (char esep) }
}
"Path": {
from_string: { |s| $s | split row (char esep) | path expand --no-symlink }
to_string: { |v| $v | path expand --no-symlink | str join (char esep) }
}
}

let conversions = stack.get_env_var(engine_state, ENV_CONVERSIONS);
let conversion = conversions
.as_ref()
.and_then(|val| val.as_record().ok())
.and_then(|record| record.get(name))
.and_then(|val| val.as_record().ok())
.and_then(|record| record.get(direction));

Which one gets called, maybe both can if it's case-preserving, but I'd hope that record can also benefit from insensitive lookups.

@yizhepku

yizhepku commented May 7, 2024

Copy link
Copy Markdown
Contributor Author

Has any thought been put into how this update interacts with ENV_CONVERSIONS?

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?

@fdncred

fdncred commented May 7, 2024

Copy link
Copy Markdown
Contributor

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.

Bahex added a commit that referenced this pull request May 18, 2025
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants