Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add function to make env vars case-insensitive #14390

Merged
merged 4 commits into from
Dec 4, 2024

Conversation

fdncred
Copy link
Collaborator

@fdncred fdncred commented Nov 19, 2024

Description

This PR adds a new function that allows one to get an env var case-insensitively. I did this so we can hopefully stop having problems when Windows has HKLM as path and HKCU as Path.

Instead of just changing every function that used the original one, I chose the ones that I thought were specific to getting the path. I didn"t want to go all in and make every env get case insensitive, but maybe we should? 🤷🏻‍♂️

closes #12676

User-Facing Changes

Tests + Formatting

After Submitting

Copy link
Member

@IanManske IanManske left a comment

Choose a reason for hiding this comment

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

Would you mind using str::eq_ignore_ascii_case instead of str::to_lowercase? This avoids creating a bunch of new strings. Alternatively, you should use str::eq_ignore_case depending on whether Windows is ASCII case insensitive or Unicode case insensitive for environment variables.

@fdncred
Copy link
Collaborator Author

fdncred commented Nov 20, 2024

Would you mind using str::eq_ignore_ascii_case instead of str::to_lowercase? This avoids creating a bunch of new strings. Alternatively, you should use str::eq_ignore_case depending on whether Windows is ASCII case insensitive or Unicode case insensitive for environment variables.

I"m not sure which of these is better. I chose to_lowercase because, from what I read, it works on ascii and unicode. So, I figured the broader application was better. I can look into eq_ignore_case, but I"ve never used it.

@IanManske
Copy link
Member

IanManske commented Nov 20, 2024

I"m not sure which of these is better. I chose to_lowercase because, from what I read, it works on ascii and unicode. So, I figured the broader application was better. I can look into eq_ignore_case, but I"ve never used it.

Yeah, I"m not sure either. In cmd or powershell, do the env vars Å and å overwrite each other / are they treated case insensitively.

@fdncred
Copy link
Collaborator Author

fdncred commented Nov 20, 2024

Yeah, I"m not sure either. In cmd or powershell, do the env vars Å and å overwrite each other / are they treated case insensitively.

I can check but my PR is really about Path PATH path PaTh and I tried to be particular about only changing the functions that dealt with that vs making all env vars case insensitive.

@devyn
Copy link
Contributor

devyn commented Nov 23, 2024

From within Nushell, all reads of env vars are case insensitive now, right? Is there ever any reason to want to check env vars case sensitively from inside the codebase?

@fdncred
Copy link
Collaborator Author

fdncred commented Nov 23, 2024

From within Nushell, all reads of env vars are case insensitive now, right? Is there ever any reason to want to check env vars case sensitively from inside the codebase?

Maybe internally but not at startup apparently, because if you"re on Windows and your HKLM is Path and your HKCU is path things break, or you get half your paths, weirdness ensues. I think this may be only at startup though, which is why I tried to change only those parts. All other internal reading of env vars may be case insensitive. I know I can do $env.path/PATH/PaTh on windows and it reads fine. Seems like some folks don"t want case-insensitive everything env vars but I"m not sure why.

@fdncred
Copy link
Collaborator Author

fdncred commented Dec 4, 2024

We need to dogfood this a bit, let"s land and do that.

@fdncred fdncred merged commit a332712 into nushell:main Dec 4, 2024
13 checks passed
@fdncred fdncred deleted the path_insensitive branch December 4, 2024 02:48
@github-actions github-actions bot added this to the v0.101.0 milestone Dec 4, 2024
fdncred pushed a commit that referenced this pull request Jan 10, 2025
# Description

Fixes multiple issues related to `ENV_CONVERSION` and
path-conversion-to-list.

* #14681 removed some calls to `convert_env_values()`, but we found that
this caused `nu -n` to no longer convert the path properly.
* `ENV_CONVERSIONS` have apparently never preserved case, meaning a
conversion with a key of `foo` would not update `$env.FOO` but rather
create a new environment variable with a different case.
* There was a partial code-path that attempted to solve this for `PATH`,
but it only worked for `PATH` and `Path`.
* `convert_env_values()`, which handled `ENV_CONVERSIONS` was called in
multiple places in the startup depending on flags.

This PR:

* Refactors the startup to handle the conversion in `main()` rather than
in each potential startup path
* Updates `get_env_var_insensitive()` functions added in #14390 to
return the name of the environment variable with its original case. This
allows code that updates environment variables to preserve the case.
* Makes use of the updated function in `ENV_CONVERSIONS` to preserve the
case of any updated environment variables. The `ENV_CONVERSION` key
itself is still case **insensitive**.
* Makes use of the updated function to preserve the case of the `PATH`
environment variable (normally handled separately, regardless of whether
or not there was an `ENV_CONVERSION` for it).

## Before

`env_convert_values` was run:

* Before the user `env.nu` ran, which included `nu -c <commandstring>`
and `nu <script.nu>`
* Before the REPL loaded, which included `nu -n`

## After

`env_convert_values` always runs once in `main()` before any config file
is processed or the REPL is started

# User-Facing Changes

Bug fixes

# Tests + Formatting

- 🟢 `toolkit fmt`
- 🟢 `toolkit clippy`
- 🟢 `toolkit test`
- 🟢 `toolkit test stdlib`

Added additional tests to prevent future regression.

# After Submitting

There is additional cleanup that should probably be done in
`convert_env_values()`. This function previously handled
`ENV_CONVERSIONS`, but there is no longer any need for this since
`convert_env_vars()` runs whenever `$env.ENV_CONVERSIONS` changes now.

This means that the only relevant task in the old `convert_env_values()`
is to convert the `PATH` to a list, and ensure that it is a list of
strings. It"s still calling the `from_string` conversion on every
variable (just once) even though there are no `ENV_CONVERSIONS` at this
point.

Leaving that to another PR though, while we get the core issue fixed
with this one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

nushell is not compatible with lowercase path on windows
4 participants