`direnv export pwsh` removes the existing `PATH` environment variable on PowerShell 7
direnv export pwsh removes the existing PATH environment variable on PowerShell 7
Description
On Windows with PowerShell 7.6.2, using the PowerShell hook causes the existing PATH environment variable to become empty when entering a directory containing an .envrc.
The same behavior can be reproduced without using the hook by running direnv export pwsh directly and evaluating its output.
Environment
- OS: Windows
- PowerShell: 7.6.2
- direnv: 2.37.1
Reproduction
Create an .envrc:
export TEST=testThen run:
direnv allow
Invoke-Expression "$(direnv hook pwsh)"
cd C:\tools\direnv-testAfter entering the directory:
$env:PATHis empty.
The issue can also be reproduced directly from the output of direnv export pwsh.
Before evaluating the output, $env:PATH contains the expected value.
Run:
$export = & direnv export pwsh
Invoke-Expression ($export -join [Environment]::NewLine)Afterwards:
$env:PATHis empty.
Observed direnv export pwsh output
The output contains commands such as:
Remove-Item -LiteralPath 'env:/Path'and also contains:
${env:PATH}='C:\Program Files\Git\mingw64\bin;...'There are similar entries for other environment variables, for example:
Remove-Item -LiteralPath 'env:/SystemDrive'
${env:SYSTEMDRIVE}='C:'
Remove-Item -LiteralPath 'env:/SystemRoot'
${env:SYSTEMROOT}='C:\WINDOWS'
Remove-Item -LiteralPath 'env:/ProgramFiles'
${env:PROGRAMFILES}='C:\Program Files'
Remove-Item -LiteralPath 'env:/ComSpec'
${env:COMSPEC}='C:\WINDOWS\system32\cmd.exe'Expected behavior
Evaluating the output of:
direnv export pwshshould not cause an existing environment variable such as PATH to disappear when the exported environment still contains a value for that variable.
In particular, entering the directory should preserve a usable PATH:
$env:PATHshould continue to contain the existing PATH entries, together with any changes introduced by .envrc.
Notes
I am not sure what the root cause is.
The generated output contains both Remove-Item operations and assignments for environment variables whose names differ in casing, but I do not want to assume that this is the cause of the problem.
The issue is reproducible from the raw output of direnv export pwsh itself, so it does not appear to be specific to the PowerShell hook implementation.
I would appreciate guidance on whether this is expected behavior and, if not, where the generated Remove-Item operations are coming from.
Source: direnv/direnv