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

direnv mangles environment variables on windows #1274

Open
sprig opened this issue Apr 23, 2024 · 0 comments
Open

direnv mangles environment variables on windows #1274

sprig opened this issue Apr 23, 2024 · 0 comments
Labels

Comments

@sprig
Copy link

sprig commented Apr 23, 2024

Describe the bug
On windows, direnv (even with an empty .envrc) attempts to upcase environment variables that it shouldn't be touching since they are not modified in the config;

T:\projects\test> direnv allow                                                                               
T:\projects\test> direnv export pwsh                                                                   
direnv: loading /t/projects/test/.envrc
direnv: export +COMMONPROGRAMFILES +COMSPEC +PATH +PROGRAMFILES +SYSTEMDRIVE +SYSTEMROOT +WINDIR -ComSpec -CommonProgramFiles -Path -ProgramFiles -SystemDrive -SystemRoot -windir

Furthetmore, it appears that the expression that direnv produces for powershell is malformed. Note the double single quotes:

Remove-Item -Path 'env:/'SystemRoot'';Remove-Item -Path 'env:/'SystemDrive'';$env:PROGRAMFILES='C:\Program Files';$env:COMMONPROGRAMFILES='C:\Program Files\Common Files';$env:COMSPEC='C:\Windows\system32\cmd.exe';$env:SYSTEMDRIVE='C:';Remove-Item -Path 'env:/'ComSpec'';Remove-Item -Path 'env:/'windir'';Remove-Item -Path 'env:/'CommonProgramFiles'';$env:WINDIR='C:\Windows';
...

And it produces the following errors, as in #1214, either when hooking into the shell or trying to source the output of direnv export pwsh:

Remove-Item: A positional parameter cannot be found that accepts argument 'SystemDrive'.
Remove-Item: A positional parameter cannot be found that accepts argument 'Path'.
Remove-Item: A positional parameter cannot be found that accepts argument 'ProgramFiles'.
Remove-Item: A positional parameter cannot be found that accepts argument 'CommonProgramFiles'.
Remove-Item: A positional parameter cannot be found that accepts argument 'ComSpec'.
Remove-Item: A positional parameter cannot be found that accepts argument 'SystemRoot'.
Remove-Item: A positional parameter cannot be found that accepts argument 'windir'

Isolating just the produced expression for the comspec variable, we get:

❯ cat .\comspec
Remove-Item -Path 'env:/'ComSpec''

T:\projects\test
❯ cat .\comspec|Invoke-Expression
Remove-Item: A positional parameter cannot be found that accepts argument 'ComSpec'.

To Reproduce
Create en empty directory with an empty .envrc. direnv allow on it and run direnv export pwsh (same happens if the hook is added to the profile startup script).

Expected behavior
I would expect environment variables that are not modified via .envrc (or .env if .envrc loads that) to not be touched.

Environment

  • OS: Windows 11
  • Shell: powershell 7.4.2
  • Direnv version: 2.34.0
  • Bash version 5.2.15(1)

Additional context
direnv.toml has bash set to C:\Program Files\Git\usr\bin\bash.exe, which is installed by git to avoid using the wsl bash and the error from #1214.

I haven't looked in the code yet but I suspect this is somehow related to the interaction with mingw64 bash. likely a diff between the environment as shown by env in bash (that has environment variables upcased) and the environment as seen by powershell produces this effect. No clue what causes the malformed expressions themselves (i.e. that produce an error).

@sprig sprig added the Bug label Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

1 participant