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
Not working on Windows using Powershell #1214
Comments
I suspect it has something to do with WSL2:
|
I've seen that @patrickhuber had the same suspicion in #1207 . Did you find a way around it yet, Patrick? |
No, but I found out where it is happening. When direnv creates a sub shell, it wraps the commands in a call to bash. I would think it should use the shell for the export command instead of always using bash. https://github.com/direnv/direnv/blob/master/internal%2Fcmd%2Frc.go#L273 |
I wonder if using the mount path will work https://learn.microsoft.com/en-us/windows/wsl/filesystems |
My problem is when using PowerShell inside Windows File System! If i'm using MSYS2 (bash or zsh) it works fine (same binary that I'm using on PowerShell). Thanks. |
Direnv is calling into bash to run its stdlib shell scripts. Wsl is invoked when you call bash.exe from powershell. Because it is invoked from windows, it is using a windows path to point to the direnv stdlib. When you use the Linux binary from Linux you don't cross the wsl boundary. The issue is (I believe) the wsl runtime has a different file system than windows because it is a virtual machine. The error is saying "hey, you called bash with this file. I can't see that file because the path c:...\whatever is a windows path and I only understand Linux paths" When wsl runs it mounts the root fs drive under /mnt/c/... . Mysys doesn't, it runs from windows and supports windows paths https://www.msys2.org/docs/filesystem-paths/ |
I dug through the code a bit more and this seems like the best place for a fix https://github.com/direnv/direnv/blob/master/internal%2Fcmd%2Fconfig.go#L102 It appears paths are already being manipulated here for mysys. Some code could be invoked that runs In the wsl case, the path prefix can be changed to /mnt/c (or the respective drive letter at the beginning of the SelfPath). There may be a better way to detect wsl. This was what I found after googling "detect wsl installed". Another option may be to check for the existence of the |
The ideal should be using the bash.exe installed by MSYS2 (Or Cygwin), and not the one on System32 folder! Thanks. |
That may fix it in your particular circumstance. The bug will still exist for users who do not have mysys installed. I think a better workaround for you may be to use the DIRENV_BASH environment variable. That way order of the path variables doesn't matter. |
I implemented the wsl check here patrickhuber@9fff44d I may need something similar for the target as I had to change that by hand during shell testing. It looks like I'm still getting an error from the direnv/internal/cmd/cmd_dotenv.go Line 42 in 9afbc87
ls -la /mnt/c/Users/patri/source/github.com/patrickhuber/direnv/.env
-rwxrwxrwx 1 patrick patrick 0 Jan 17 16:19 /mnt/c/Users/patri/source/github.com/patrickhuber/direnv/.env
/mnt/c/Users/patri/source/github.com/patrickhuber/direnv/direnv.exe dotenv bash /mnt/c/Users/patri/source/github.com/patrickhuber/direnv/.env
direnv: error open /mnt/c/Users/patri/source/github.com/patrickhuber/direnv/.env: The system cannot find the path specified. I wonder if os.ReadFile has a bug under wsl when reading over a mount. I did find this issue golang/go#42184, though it may be only related to symlinks. I probably need an isolated binary to test. A go program compiled to windows x64 that just tries to open a file with os.ReadFile. |
So looks like when you invoke a windows executable from wsl and you specify a file, the windows executable expects windows paths. So the example above changed to : bash -c 'eval $(/mnt/c/Users/patri/source/github.com/patrickhuber/direnv/direnv.exe stdlib) && __main__ dotenv $"c:/Users/patri/source/github.com/patrickhuber/direnv/.env"'
direnv: .env at c:/Users/patri/source/github.com/patrickhuber/direnv/.env not found This is coming from the stdlib.sh There are also a few env vars that can be checked, looking at my system I see: WSLENV, WSL2_GUI_APPS_ENABLED, WSL_DISTRO_NAME and WSL_INTEROP Another option is to use the $ wslpath c:/Users/patri/source/github.com/patrickhuber/direnv/.env
/mnt/c/Users/patri/source/github.com/patrickhuber/direnv/.env |
I settup DIRENV_BASH to my MSYS2 bash.exe, and now I'm getting this error on PowerShell 7:
Thanks. |
could there be a check when running bash.exe if you startup in WSL? WSL got the wslpath helper function where you could throw a Windows Path against and it gives you the Linux variant |
Would it be possible to query for more specific ENV_Vars that won't clash with other XDG "settings" on windows? https://gist.github.com/nwisemanII/b3d75336b7dffd2fc77ba520eb0adb57 |
Describe the bug
I download (https://github.com/direnv/direnv/releases/download/v2.33.0/direnv.windows-amd64.exe) and enable (
Invoke-Expression "$(direnv hook pwsh)"
) the app on Windows with Powershell, but it's not working!To Reproduce
Invoke-Expression "$(direnv hook pwsh)"
)Expected behavior
The variable should be defined without error!
Environment
Additional context
The same binary works fine with Bash on MSYS2!
Thanks
The text was updated successfully, but these errors were encountered: