purcell / exec-path-from-shell

Make Emacs use the $PATH set up by the user's shell
Other
1.41k stars 81 forks source link

It does not understand env var in `if-else` branch #63

Closed longkai closed 7 years ago

longkai commented 7 years ago

My env configuration is based on different os version checking, e.g.,

# shared
export A="a"
export B="b"
# ....
if [[ `uname` == 'Linux' ]]; then
  export C="c"
  export D="d"
  # ....
else
  export E="e"
  export F="f"
  # ....
fi

But, it does not contain all of the env in the if-else block, only global(A, B) is accepted. It works well in shells.

Tested on macOS 10.12.3 and spacemacs 0.200.7@25.1.1

purcell commented 7 years ago

That's nothing to do with this package: the elisp code simply executes your shell. I imagine your shell is not executing the code as you expect. Set exec-path-from-shell-debug to t and then run exec-path-from-shell-initialize: the command used by exec-path-from-shell will be displayed in the *Messages* buffer and you can test that from the command line to debug your startup scripts.

longkai commented 7 years ago

Thanks for the reply. I am sorry for my fault. I did't read the document carefully. I thought it would load all my env vars.

From the output on bottom, it seems the package does load the PATH fragment in the if-else block. And only load the env var with ends PATH? I see MANPATH PATH and GOPATH.

If you want your other vars, call (exec-path-from-shell-copy-env "name").

Thanks.

Invoking shell /bin/zsh with args ("-l" "-i" "-c" "/usr/local/opt/coreutils/libexec/gnubin/printf '__RESULT\\000%s\\000%s' \"${PATH-ce48314cacce40761f825e2bc42915b6}\" \"${MANPATH-ce48314cacce40761f825e2bc42915b6}\"")                                                                                                                                                                                                                                                                                                                                                                                                                                 
Shell printed: "__RESULT�/Users/longkai/bin:/usr/local/opt/go/libexec/bin:/usr/local/opt/coreutils/libexec/gnubin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Users/longkai/bin:/usr/local/opt/go/libexec/bin:/usr/local/opt/coreutils/libexec/gnubin:/Applications/Emacs.app/Contents/MacOS/bin-x86_64-10_9:/Applications/Emacs.app/Contents/MacOS/libexec-x86_64-10_9:/Users/longkai/pkg/gem/bin:/Users/longkai/pkg/gem/bin�/usr/local/opt/coreutils/libexec/gnuman:/usr/share/man:/usr/local/share/man:/Applications/Wireshark.app/Contents/Resources/share/man:/usr/local/opt/coreutils/libexec/gnuman"
Invoking shell /bin/zsh with args ("-l" "-c" "/usr/local/opt/coreutils/libexec/gnubin/printf '__RESULT\\000%s\\000%s' \"${MANPATH-15b3fb476c4638a11bdd2a921d5680b1}\" \"${PATH-15b3fb476c4638a11bdd2a921d5680b1}\"")                                                                                                                                                                                                                                                                                                                                                                                                                                      
Shell printed: "__RESULT�/usr/share/man:/usr/local/share/man:/Applications/Wireshark.app/Contents/Resources/share/man:/usr/local/opt/coreutils/libexec/gnuman�/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Wireshark.app/Contents/MacOS:/Users/longkai/bin:/usr/local/opt/go/libexec/bin:/usr/local/opt/coreutils/libexec/gnubin:/Applications/Emacs.app/Contents/MacOS/bin-x86_64-10_9:/Applications/Emacs.app/Contents/MacOS/libexec-x86_64-10_9:/Users/longkai/pkg/gem/bin"                                                                                                                                                            
You appear to be setting environment variables ("PATH" "MANPATH") in your .bashrc or .zshrc: those files are only read by interactive shells, so you should instead set environment variables in startup files like .profile, .bash_profile or .zshenv.  Refer to your shell’s man page for more info.  Customize ‘exec-path-from-shell-arguments’ to remove "-i" when done, or disable ‘exec-path-from-shell-check-startup-files’ to disable this message.                                                                                                                                                                                               
purcell commented 7 years ago

Yep. Note that you can modify exec-path-from-shell-variables before you call exec-path-from-shell-initialize so that several env vars are collected in a single shell execution -- it can be slow to call exec-path-from-shell-copy-env multiple times.

longkai commented 7 years ago

Thank you. I exec the function with M-x and it works.

But after putting (exec-path-from-shell-copy-env "GOROOT") into ~/.spacemacs user-init, emacs warns (Spacemacs) Error in dotspacemacs/user-init: Symbol’s function definition is void: exec-path-from-shell-copy-env.

BTW, exec-path-from-shell-variables, it seems not appear in M-x result list.

I am new to emacs and sorry in advance.

purcell commented 7 years ago

That's a spacemacs question, really. I think you should probably put something like this in .spacemacs instead:

(setq exec-path-from-shell-variables '("PATH" "MANPATH" "GOROOT"))
longkai commented 7 years ago

It works like a charm. Thank you very much!

purcell commented 7 years ago

Happy to help. :-)