Skip to content

Make env var names case insensitive on Windows - #11432

Closed
raggi wants to merge 1 commit into
nushell:mainfrom
raggi:windows-env
Closed

raggi wants to merge 1 commit into
nushell:mainfrom
raggi:windows-env

Conversation

@raggi

@raggi raggi commented Dec 28, 2023

Copy link
Copy Markdown

The Windows getenv/putenv functions are case-insensitive, so when built for Windows nushell should also act this way to avoid many complicated conflicts. A full description of the problem is in #11154.

Fixes #11154

User-Facing Changes

On Windows systems, environment variable names are now case-insensitive. Reading from / writing to PATH and Path and path are the same.

Tests + Formatting

Old special case implementation switcheroo from Path and PATH is removed, but the test cases are not and still pass.

New tests should likely be added, but sending the PR as-is for initial evaluation.

After Submitting

  • Doc updates are indeed required.
  • Example config needs updating to remove the vague language around Path.

The Windows getenv/putenv functions are case-insensitive, so when
built for Windows nushell should also act this way to avoid many
complicated conflicts.

Fixes nushell#11154
@kubouch

kubouch commented Dec 28, 2023

Copy link
Copy Markdown
Contributor

I kind of like this idea. You could still make cross-platform scripts by always using upper-case, and it would still work on Windows. The only concern is that it is a breaking change, and makes Nushell behave differently on different platforms. But one principle of Nushell is to make it feel native on each platform, which doesn't necessarily mean uniform. So, for me, having case-insensitive env vars on Windows and case-sensitive on Unix if fine.

Wasn't there a case on Windows that you could have either Path or PATH (or both) defined somehow? I wonder if that would break now.

@amtoine amtoine added the platform:windows Issues which are specific to Windows label Dec 28, 2023
@fdncred

fdncred commented Dec 28, 2023 •

Copy link
Copy Markdown
Contributor

I added this one #11268 a while back and forgot about it. It makes $env.PATH case insensitive on all platforms.

@fdncred

fdncred commented Dec 28, 2023

Copy link
Copy Markdown
Contributor

I'm not a fan of this PR because it doesn't allow cross-platform case insensitivity. We could have $env.path in Windows scripts and, if I read this PR correctly, that would puke in Mac/Linux. The point of my PR was that it would work cross platform, so you no longer have to worry about the spelling case of path. This PR doesn't fix that.

@fdncred
fdncred marked this pull request as draft December 28, 2023 12:32
@kubouch

kubouch commented Dec 28, 2023

Copy link
Copy Markdown
Contributor

On Unix, everything is expected case-sensitive. I wouldn't expect $env.Path to work on Linux. With this PR, to get cross-platform path, you'd use $env.PATH everywhere (which is a default expected spelling for environment variables in general across OSes). The scope of this PR also includes all env vars, not just PATH, again matching the expected Windows case-insensitivity.

But clearly, this is something we need to discuss in the core team.

@fdncred

fdncred commented Dec 28, 2023

Copy link
Copy Markdown
Contributor

which is a default expected spelling for environment variables in general across OSes

Only across *nix OSes. We know PATH is not valid on Windows. Although I would agree that generally speaking, uppercase env vars is the default with the one exception, path. Why does Windows have to be so stupid. It's really aggravating sometimes.

@raggi

raggi commented Dec 29, 2023

Copy link
Copy Markdown
Author

@fdncred it's important to look at the bug this fixes; I think you're over-focused on PATH here, but there are far more side effects of this problem.

An excerpt from the reproduction in #11154:

Perform the following tests with an arbitrary program, nvim might be a reasonable choice:

Set $env.Foo = 1 and $env.FOO = 2, spawn a program & process explorer shows Foo = 2
Set $env.FoO = 3 spawn a program & process explorer shows FOO = 3
Update $env.Foo = 4 then a spawn a program, observe FOO = 3

These results are unpredictable, spurious and problematic. There's no portable solution to this problem in the general case - we have to make the behavior predictable in some manner otherwise it's just broken. Again, this isn't just about PATH. The unpredictability here comes from the HashDOS resistant HashMap backing these variables, and it means the behavior isn't even consistent across repeated runs.

@raggi

raggi commented Dec 29, 2023

Copy link
Copy Markdown
Author

@kubouch

Wasn't there a case on Windows that you could have either Path or PATH (or both) defined somehow? I wonder if that would break now.

Windows 11 defaults have a variable named Path set in the global environment settings, which is the default for a number of versions back (back to XP, IIRC). There are actually two of these defined, one for the global system, one for the user, and they're merged in a user session to produce the final path.

In a managed environment it's not uncommon to see the user ones replaced with a value defined as PATH, as that's typically how most scripts/management systems spell it, and for safety reasons they tend to remove the existing setting and create a new one.

Similarly if you run nu.exe from underneath nvim.exe or gvim.exe or some other similarly behaving program you'll likely find that it has modified the environment with getenv/putenv, and the outcome is that env contains only PATH, not Path. It was precisely the nvim case of this that lead me to filing the original bug. On the original bug I have also referenced a number of other bugs I found that seem very likely also a side effect of this issue - those include failure to hide variables in scopes and so on.

@raggi

raggi commented Dec 29, 2023

Copy link
Copy Markdown
Author

I'll hold off fixing CI here until there's a decision on how to proceed.

I'd run it locally inside WSL but didn't spot those issues, they look trivial to fix anyway.

@kubouch

kubouch commented Dec 29, 2023

Copy link
Copy Markdown
Contributor

which is a default expected spelling for environment variables in general across OSes

Only across *nix OSes. We know PATH is not valid on Windows. Although I would agree that generally speaking, uppercase env vars is the default with the one exception, path. Why does Windows have to be so stupid. It's really aggravating sometimes.

Yes, I meant env vars in general, not PATH, poorly worded.

Maybe in addition to handling env vars on Windows case-insensitively, we could also special-case PATH/Path on Windows to ensure that only Path exists, i.e., Path is always passed to external programs spawned from Nushell even if you set $env.PATH = ....

@fdncred

fdncred commented Dec 29, 2023

Copy link
Copy Markdown
Contributor

My goal with all of this is to allow the user to not care about case sensitivity for env vars. I just started with path because it's the biggest problem. I'd really like to get away from Windows users having to know how their registry is configured for Path. I've seen many times that Windows isn't case-sensitive but case-preserving. So, if it's called PaTh in the two registry entries, that's what it'll be on your system, unfortunately. So, I'm up for whatever makes it easiest for all users across all platforms and I think that solution is to not care about the spelling case of env vars, and path in particular.

@raggi

raggi commented Jan 1, 2024

Copy link
Copy Markdown
Author

In both of the last two comments there appears to be a suggestion that somehow nu will produce a canonical casing for Path as literally Path, but this isn't particularly useful/valuable. As described in earlier comments / bugs I filed:

  • Administration scripts often delete these keys and install new ones with PATH, so you won't always find PATH, nor is there a strong expectation of software to find Path.
  • Many software packages also do similar behavior to the above, either constructing entirely new environments or removing and setting environments containing PATH, for example you find this in:
    • nvim.exe terminals are run with PATH
    • go.exe subcommands are run with PATH
    • git.exe -> preserves case (uses getenv/putenv to add libexec)
    • msys -> canonicalizes to PATH for compatibility
    • mingw64 -> case preserving

The point is that canonicalization is not particularly important, but may get in the way if a user is launching a program with specific (bad) expectations. Problems will not arise with general windows software using the Windows CRT, but problems do come up at times with non-native runtimes (python, ruby, node, go, etc). Most of those runtimes canonicalize to PATH as well, e.g. python 3.12 from the MS app store:

>>> [k for k in os.environ if k.lower() == 'path']
['PATH']

The only common canonicalization is PATH, even though the most common default on Windows is Path.

My recommendation is:

  • Implement case-indifference (that's what this change does)
  • Most of the time users can use PATH, consistent with the broadest ecosystem of software.
  • When a user hits a rare special case of software that spuriously needs canonical casing, they can unset, and set the variable with the required casing, and that will enable them to produce the desired result.

@kubouch

kubouch commented Jan 3, 2024

Copy link
Copy Markdown
Contributor

So we're on board with it. Just to make it clear, the env vars on Windows should be case-preserving, meaning that $env.SpAm = 'spam'; nu -c '$env | columns' should contain SpAm, but let foo = $env.SPAM would still work because the matches are case-indifferent. I guess this is how UniCase works?

This makes me think: What would happen on Windows if you set $env.SpAm = 'spam1' and $env.SPAM = 'spam2'? I guess it would be overwritten, so you'd have only SPAM at the end? I'd be OK with it, though it would need to be documented.

It would be great if you could add tests covering these cases (case-preserving and case-indiference on Windows).

@ChrisDenton

Copy link
Copy Markdown
Contributor

This makes me think: What would happen on Windows if you set $env.SpAm = 'spam1' and $env.SPAM = 'spam2'? I guess it would be overwritten, so you'd have only SPAM at the end? I'd be OK with it, though it would need to be documented.

Windows environment variables are "case insensitive but case preserving". The above hits the "case preserving" part. If you start with SpAm then it'll always be SpAm even if you set SPAM (unless you delete SpAm first). Nushell could act differently but I think it should match the native way instead of introducing a third way to handle things.

@kubouch

kubouch commented Jan 4, 2024

Copy link
Copy Markdown
Contributor

Ah, yes, that makes sense. I'm fine with it, but as you can see, it's not immediately obvious, we should definitely put this into our documentation.

@raggi

raggi commented Jan 4, 2024

Copy link
Copy Markdown
Author

Yup, as @ChrisDenton described.

Thanks for taking the time to review the proposal @kubouch.

I see there's a wait-until-after-release label - makes sense. When is the next release scheduled? (no urgency here, I'm just curious)

I will get the PR updated ensuring that the bits CI caught are covered too.

I'll look in devdocs/ and see if there's a sensible place to add some additional docs in this repo as well.

@fdncred

fdncred commented Jan 4, 2024

Copy link
Copy Markdown
Contributor

When is the next release scheduled?

9th of January - Just a few days from now

@fdncred

fdncred commented Jan 20, 2024

Copy link
Copy Markdown
Contributor

Where are we at on this PR?

@fdncred

fdncred commented Jan 30, 2024

Copy link
Copy Markdown
Contributor

@raggi did you run out of free time to help with this?

@fdncred

fdncred commented Feb 26, 2024

Copy link
Copy Markdown
Contributor

any updates here?

@fdncred

fdncred commented Mar 18, 2024

Copy link
Copy Markdown
Contributor

@raggi are you able to get back to this and fix it up as suggested above?

@raggi

raggi commented Mar 18, 2024

Copy link
Copy Markdown
Author

Sorry @fdncred. I worked around the case that was continually causing me problems and inadvertently deprioritized coming back to this as a result - my bad.

I can try to pick this up again one evening this week, or if someone else wants to do so I'm not in any way precious about my incomplete patch - I'd love to have this working.

@fdncred

fdncred commented Mar 18, 2024

Copy link
Copy Markdown
Contributor

No worries at all @raggi. We understand that life happens. I just personally wanted something like this too so I'm hoping someone can push it over the finish line. I think it would be a great QoL add for nushell.

@fdncred

fdncred commented May 1, 2024

Copy link
Copy Markdown
Contributor

closed in favor of #12701

@fdncred fdncred closed this May 1, 2024
@raggi

raggi commented May 25, 2024

Copy link
Copy Markdown
Author

Sorry for not making it, but thank you for finishing off this bug, very welcome change!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

notes:breaking-changes Noted in Breaking Changes section platform:windows Issues which are specific to Windows status:wait-until-after-nushell-release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Environment variables on Windows need deterministic and documented behavior

5 participants