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

How to run on windows? #343

Open
johhansantana opened this Issue Feb 27, 2018 · 14 comments

Comments

Projects
None yet
10 participants
@johhansantana
Copy link

johhansantana commented Feb 27, 2018

I'm trying to setup direnv to run on windows.

I'm running git-bash as my terminal.

I tried running both:
https://github.com/direnv/direnv/releases/download/v2.15.2/direnv.windows-386.exe
and
https://github.com/direnv/direnv/releases/download/v2.15.2/direnv.windows-amd64.exe

A small window appears but then disappears almost instantly.

I tried adding to ~/.bashrc eval "$(direnv hook bash)" but it keeps saying direnv command not found

How can I get direnv to work with windows.

@Oppodelldog

This comment has been minimized.

Copy link

Oppodelldog commented Feb 28, 2018

Same issue here, I couldn't figure out how to use direnv under windows.
Please support some documentation.

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Mar 1, 2018

@johhansantana direnv command not found means that direnv isn't found in the $PATH. Try putting the executable in one of the folder listed when you run echo $PATH.

@markwest1

This comment has been minimized.

Copy link

markwest1 commented Apr 3, 2018

Since I use direnv exclusively in git-bash, I copy the executable to the /usr/local/bin folder (absolute folder is C:\Program Files\Git\usr\local\bin) and add two lines to my .bashrc.

$ grep direnv ~/.bashrc
alias direnv="/usr/local/bin/direnv.windows-amd64.exe"
# direnv
eval "$(direnv hook bash)"

If you want to use direnv in both cmd.exe and git-bash shells, it may be more complicated than this.

direnv

@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Apr 3, 2018

nice! I'm surprised that it's working at all

@markwest1

This comment has been minimized.

Copy link

markwest1 commented Apr 4, 2018

However, while the GOPATH environment variable can be successfully set within .envrc, unfortunately, the PATH variable still gets mangled (see demonstration below). I think the mangling happens as a result of this code in main.expandPath() (in the file cmd_expand_path.go):

return filepath.Clean(filepath.Join(relTo, path))

So far, I haven't come up with a good work-around, but I'm still noodling on it. I'll submit a PR if I can come up something that's not too hack-ish for bash in mingw.

BTW, @zimbatm, than you for the time and effort you spent to create direnv, it does make my life easier, despite the PATH mangling in mingw. I use it at home on my linux machine, and it works perfectly. Unfortunately, I'm stuck with Windows at work.

Here's a demonstration of how PATH get's mangled when it's modified within .envrc (in mingw):

$ cat /d/code/otr/direnv/.envrc
export GOPATH=$(pwd)

$ cd /d/code/otr/direnv
direnv: loading D:\code\otr\direnv\.envrc
direnv: export +GOPATH

$ ls
bin/  pkg/  src/

$ echo $GOPATH
D:/code/otr/direnv

$ echo "layout go" >> .envrc
direnv: error D:\code\otr\direnv\.envrc is blocked. Run `direnv allow` to approve its content.

$ cat .envrc
export GOPATH=$(pwd)
layout go
direnv: error D:\code\otr\direnv\.envrc is blocked. Run `direnv allow` to approve its content.

$ direnv allow
direnv: loading D:\code\otr\direnv\.envrc
direnv: export +GOPATH ~PATH

$ ls
bash: ls: command not found
direnv: error Can't find bash: "exec: \"bash\": executable file not found in %PATH%"

$ echo $PATH
D;D:\code\otr\direnv\bin;C:\Users\218024862\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\local\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\usr\bin;C:\Program Files\Git\mingw64\bin;C:\Program Files\Git\usr\bin;C:\Users\218024862\bin;C:\Program Files\Microsoft MPI\Bin;C:\Program Files (x86)\M1 Licensing;C:\Windows ... [truncated for brevity]
direnv: error Can't find bash: "exec: \"bash\": executable file not found in %PATH%"

$
direnv: error Can't find bash: "exec: \"bash\": executable file not found in %PATH%"
@zimbatm

This comment has been minimized.

Copy link
Member

zimbatm commented Apr 6, 2018

Thanks @markwest1!

I think that I have seen that particular issue already. The issue is with go's builtins "path" package that has some heuristics hard-coded for Windows. When the compiler targets Windows it's assuming that you willl be using Windows-style paths everywhere which is obviously not true.

Basically we need the same package but that can be switched between Unix or Window paths at runtime instead. And then a way to setup direnv to tell him if it should be running in CMD or mingw mode.

@DmitryH3

This comment has been minimized.

Copy link

DmitryH3 commented Jun 11, 2018

I'm seeing the same issue, when trying to append to PATH. Basically, it goes something like:
$echo $PATH
/usr/bin:/working path # All working

Now say I want to add this to my .envrc:
export PATH="/test path":$PATH

after switching to the directory, PATH is converted to:
C:\msys2-64\test path;C:\msys2-64\usr\bin;C:\msys2-64\working path

And at this point things totally don't work.

@ibraimgm

This comment has been minimized.

Copy link

ibraimgm commented Jun 20, 2018

I also have the path mangling problem on Windows on git-bash. I managed to work around the problem with a little hack:

# my .envrc
export GOPATH="$PWD"
export PATH=$GOPATH/bin:$PATH

Now, I created a small script fo "fix" the path mangling:

# ~/fix.sh : Hackish way to 'fix' path mangling
export USR_BIN=/c/Git/usr/bin
export SED=$USR_BIN/sed.exe
export ECHO=$USR_BIN/echo.exe
export PATH=$($ECHO $PATH | $SED -E 's/C:/\/c/g' | $SED -E 's/\\/\//g' | $SED -E 's/;/:/g')

Then, with the help of Bash-preexec, I just call the "fix" script before a command is executed:

# ~/.profile
eval $(direnv hook bash)
source ~/.bash-preexec.sh
precmd() { source ~/fix.sh ; }

This way, before any command is executed the (possible) mangling on path is undone. Not the best (or most beautiful) solution, but so far I had no problems with this hack.

@markwest1

This comment has been minimized.

Copy link

markwest1 commented Jun 20, 2018

Thank you @ibraimgm! I will give this a try.

@ivan-m

This comment has been minimized.

Copy link

ivan-m commented Jul 11, 2018

Using Cygwin (installed to the non-stand location of C:\workspace\ext\cygwin), even if my ~/.bash_profile contains just the following (having downloaded the latest AMD64 executable and saved it to the C:\workspace\ext\cygwin\bin\):

export PATH="/usr/bin"

alias direnv="/usr/bin/direnv.windows-amd64.exe"

eval "$(direnv hook bash)"

then I still get this on every prompt:

bash: C:\workspace\ext\cygwin\bin\direnv.windows-amd64.exe: command not found

Yet this succeeds:

machine ~
$ direnv version
2.17.0
-bash: C:\workspace\ext\cygwin\bin\direnv.windows-amd64.exe: command not found

machine ~
$ echo $?
0
-bash: C:\workspace\ext\cygwin\bin\direnv.windows-amd64.exe: command not found

I tried using bash-preexec as suggested by @ibraimgm but that didn't work either.

@vshih

This comment has been minimized.

Copy link
Contributor

vshih commented Dec 17, 2018

Any progress on this? I'm trying to run in cygwin (looked through those related issues too) but no luck - I get

-bash: C:\cygwin64\usr\local\bin\direnv.windows-amd64.exe: command not found

on every bash prompt.

@vshih

This comment has been minimized.

Copy link
Contributor

vshih commented Dec 23, 2018

I submitted #431.

@Shamus03

This comment has been minimized.

Copy link

Shamus03 commented Dec 24, 2018

I'm having the path-mangling issue as well, even with an empty .envrc. I haven't found a use case for changing PATH using direnv, so I just added PATH to the IGNORED_KEYS list as a workaround. Hoping to see a fix soon.

@mcauto

This comment has been minimized.

Copy link

mcauto commented Feb 14, 2019

  1. Install direnv
  2. move program
$ mv direnv.windows-amd64.exe ${GOPATH}/bin/direnv
$ direnv
direnv v2.19.2
Usage: direnv COMMAND [...ARGS]

Available commands
------------------
allow [PATH_TO_RC]:
  Grants direnv to load the given .envrc
deny [PATH_TO_RC]:
  Revokes the authorization of a given .envrc
edit [PATH_TO_RC]:
  Opens PATH_TO_RC or the current .envrc into an $EDITOR and allow
  the file to be loaded afterwards.
exec [DIR] COMMAND [...ARGS]:
  Executes a command after loading the first .envrc found in DIR
help [SHOW_PRIVATE]:
  shows this help
hook SHELL:
  Used to setup the shell hook
prune:
  removes old allowed files
reload:
  triggers an env reload
status:
  prints some debug status information
stdlib:
  Displays the stdlib available in the .envrc execution context
version:
  prints the version
watch [SHELL] PATH:
  Adds a path to the list that direnv watches for changes
  1. apply direnv in git-bash
$ vim ~/.bashrc
export EDITOR=vim
eval "$(direnv hook bash)"
$ source ~/.bashrc
  1. Use it.
$ direnv edit .
export HELLO=world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment