6

I keep getting this error within Sublime Text, after installing the gofmt package:

Traceback (most recent call last):
  File "/Users/abrahma/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 257, in run_formatter
    formatter = Formatter(view)
  File "/Users/abrahma/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 166, in __init__
    self.cmds = [Command(cmd, self.view, self.window) for cmd in cmds]
  File "/Users/abrahma/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 166, in <listcomp>
    self.cmds = [Command(cmd, self.view, self.window) for cmd in cmds]
  File "/Users/abrahma/Library/Application Support/Sublime Text 3/Installed Packages/Gofmt.sublime-package/gofmt.py", line 77, in __init__
    self.window)
  File "/Users/abrahma/Library/Application Support/Sublime Text 3/Packages/golangconfig/all/golangconfig.py", line 227, in subprocess_info
    raise exception
golangconfig.EnvVarError: The following environment variable is currently unset: GOPATH

My `` file has the following:

    "env" : {
        "GOPATH" : "/Users/abrahma/git/go",
        "GOROOT" : "/usr/local/Cellar/go/1.12.7/libexec",
    }

(which matches the output of go env on my terminal)

I also have a golang.sublime-settings file with the following:

{
    "env" : {
        "GOPATH" : "/Users/abrahma/git/go",
        "GOROOT" : "/usr/local/Cellar/go/1.12.7/libexec",
    }
}

So, for some reason, the GOPATH above clearly isn't getting picked up, any ideas why?

agam
  • 5,064
  • 6
  • 31
  • 37

2 Answers2

14

Instructions for setting the variables on different OS are mentioned in this doc https://github.com/golang/sublime-config/blob/master/docs/user.md

I use Sublime3 with GolangConfig in macOs and ran into the same error. Setting the below in golang.sublime-settings helped me fix the issue.

{
    "PATH": "/Users/suryapandian/go/bin",
    "GOPATH": "/Users/suryapandian/go"
}

Note: Attaching pic below to locate golang.sublime-settings

enter image description here

Surya
  • 2,429
  • 1
  • 21
  • 42
5

Thanks to the suggestion by @lbu, I realized that I needed this to exist in my login shell (~/.zprofile in my case), and it works fine now.

agam
  • 5,064
  • 6
  • 31
  • 37
  • I don't have a ~/.zprofile... Do i need to create it and add `export GOPATH=$(go env GOPATH)`? – Pylinux Jan 21 '20 at 11:23
  • 1
    Lol, had this error again, in a new place, and ... found my old question. Sheesh, I didn't _really_ learn after all :-) – agam Jan 24 '22 at 20:33
  • @Pylinux sorry I never answered your question (two years ago!) but ...yes. – agam Jan 24 '22 at 20:34