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

How do I set up $GOPATH properly for Emacs? #261

Closed
arianitu opened this issue Dec 10, 2014 · 7 comments

Comments

@arianitu
Copy link

@arianitu arianitu commented Dec 10, 2014

It always takes me an hour to get gocode to work and usually it's random. It also tends to break at random as well.

Where does gocode look for $GOPATH?

Here are some of the things I've tried.. in my emacs:

(setenv "GOPATH" "/Users/auka/Development/personal/go")
(setq exec-path (append exec-path '("/usr/local/Cellar/go/1.3.3/bin")))
(setq exec-path (append exec-path '("/Users/auka/Development/personal/go/bin")))
(setenv "PATH"
        (concat
     "/usr/local/Cellar/go/1.3.3/bin" ":"
     (getenv "PATH")
     )
    )

in my .zshrc:

export GOPATH=$HOME/Development/personal/go

Is it using the one in .zshrc, or the one in .emacs, or neither?

I use http://emacsformacosx.com/, so I'm not sure where all the paths are coming from, it's extremely frustrating.

If I run this go env in my zsh terminal I get:

GOARCH="amd64"
GOBIN=""
GOCHAR="6"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/auka/Development/personal/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.3.3/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.3.3/libexec/pkg/tool/darwin_amd64"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fno-common"
CXX="clang++"
CGO_ENABLED="1"

Is gocode suppose to run from the zsh terminal, or is the emacs plugin suppose to launch it for you? Are the paths different if you launch it your self?

Is there an easy way to debug what's happening under the covers instead of me hitting tab and nothing happening?

@arianitu arianitu changed the title How do I set this up $GOPATH properly for Emacs? How do I set up $GOPATH properly for Emacs? Dec 10, 2014
@arianitu

This comment has been minimized.

Copy link
Author

@arianitu arianitu commented Dec 10, 2014

As a note, I'm launching Emacs.app from spotlight. Perhaps I need to launch it from my console to get the right environment variables?

@nsf

This comment has been minimized.

Copy link
Owner

@nsf nsf commented Dec 10, 2014

GOPATH is a just an env var. Unfortunatelly I'm not familiar at all with how MacOSX does things. I can only tell how things work on linux more or less.

On linux environment variables of all the parent processes are inherited to child processes by default, so if a process starts another process it gets its env vars. Using that concept it's possible to set variables in many places: logic shell init scripts (such as /etc/profile), ~/.xinitrc for graphical environment, interactive shell init scripts (such as ~/.zshrc or ~/.bashrc), etc.

Same goes for editors, if you start an editor from bash/zsh, it uses environment variables defined for that shell session. If you start an editor from graphical environment, it will use whatever environment variables are defined on the process responsible for it.

As for GOPATH specifically, I set it in /.bashrc, but my per-user logic shell script (/.bash_profile) runs ~/.bashrc as well, so it becomes system-wide. Here I must mention that "login shell" is a valid term if you do the login using the shell, there are also "graphical logins", which use different mechanisms.

For further info you can read this debian's article, it describes everything nicely: https://wiki.debian.org/EnvironmentVariables

And here answers to your direct questions:

Is gocode suppose to run from the zsh terminal, or is the emacs plugin suppose to launch it for you?

Normally gocode should be started by the plugin itself.

Are the paths different if you launch it your self?

Gocode client passes all necessary environment variables on a per-request basis, so it doesn't matter under which environment gocode daemon was started.

Is there an easy way to debug what's happening under the covers instead of me hitting tab and nothing happening?

You can run gocode in a so-called "debug mode" manually, it prints a lot of info on a per-request basis, you can do it that way in a separate terminal window:

gocode close
gocode -s -debug

Just google for how to set environment vars for programs in MacOSX, I'm sure you'll find your answer.

I hope it helps.

@arianitu

This comment has been minimized.

Copy link
Author

@arianitu arianitu commented Dec 10, 2014

The way I fixed this was by installing https://github.com/purcell/exec-path-from-shell and removing any set paths from my .emacs file. The plugin makes sure to use the correct paths (the paths I set in .zshrc) even when launching from spotlight (which was the actual issue, it was inheriting the wrong paths when launching the .app)

I installed that from MELPA and everything seems to work again. Perhaps a recommendation in the README.md might help other people? I'm not sure if this is a common problem.. but I know a lot of people on OSX use http://emacsformacosx.com/ and would run into this exact issue if they launched from spotlight.

Also feel free to close this issue.

@nsf

This comment has been minimized.

Copy link
Owner

@nsf nsf commented Dec 10, 2014

Yes, I'll add a link to this script. In fact now you mentioned it, I remember there was a similar issue with a similar outcome (a person ended up using exec-path-from-shell). I'll add a note in a moment.

@nsf nsf closed this in 643c064 Dec 10, 2014
@arianitu

This comment has been minimized.

Copy link
Author

@arianitu arianitu commented Dec 10, 2014

@nsf If you wouldn't mind, could you also add this under that suggestion section..

Add this to your .emacs if you use exec-path-from-shell:

(when (memq window-system '(mac ns))
  (exec-path-from-shell-initialize))
(exec-path-from-shell-copy-env "GOPATH")

People might forget to add (exec-path-from-shell-copy-env "GOPATH") which copies $GOPATH from your .bashrc, or .zshrc

Thanks so much, gocode -s -debug definitely nailed where the issue was coming from.

@nsf

This comment has been minimized.

Copy link
Owner

@nsf nsf commented Dec 10, 2014

Done.

@sunlightlj

This comment has been minimized.

Copy link

@sunlightlj sunlightlj commented Sep 3, 2016

@nsf @arianitu
This issue could be solved by following step
#echo "export GOPATH="YOUR-GO-PATH" >> ~/.profile

More info:
http://stackoverflow.com/questions/18773051/how-to-make-os-x-to-read-bash-profile-not-profile-file

gojun077 pushed a commit to gojun077/jun-dotfiles that referenced this issue Jan 24, 2018
…heck

I think it has to do with the PATH being incorrectly set for Emacs when it
is started on MacOS X from the Launchbar.

I therefore applied a workaround using the pkg "exec-path-from-shell"

References:

http://codewinds.com/blog/2015-04-02-emacs-flycheck-eslint-jsx.html

nsf/gocode#261
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.