Sublime Forum

LSP-gopls not using the right GOPATH

#1

I am using the LSP-gopls plugin. When I write Go code, I have this message on the LSP Diagnostics Panel

4:2   error   could not import myProject (cannot find package "myProject" in any of  ​compiler:
                  	/usr/local/Cellar/go/1.17.2/libexec/src/dst (from $GOROOT)
                  	/Users/username/go/src/myProject (from $GOPATH))

I notice it’s using the system default GOROOT and GOPATH, which I have override with my own setup using gvm. So if I type which go in the terminal, I have

$ which go
/Users/username/.gvm/gos/go1.16.7/bin/go

I tried to configure this in the LSP setting by adding the env field

// Settings in here override those in "LSP/LSP.sublime-settings"
{
    "clients": {
        "gopls": {
            "env": {
                "PATH": "/Users/username/.gvm/gos/go1.16.7",
                "GOPATH": "/Users/username/.gvm/pkgsets/go1.16.7/global"
            },
            "enabled": true,
            "command": ["gopls"],
            "selector": "source.go",
            "initializationOptions": {
                "experimentalWorkspaceModule": false
            }
        }
    }
}

However, the message still exists. I have also tried

  • restart the LSP server using LSP: Restart Server
  • and restart the Sublime Text itself.

I am pretty sure the error message wasn’t correct, because the code runs fine, and running the gopls check command gives no error in the terminal.

Spec

I am using

  • MacOS Monterey(12.0.1)
  • Sublime Text 4 (Build 4121)
    • LSP v1.14.0
    • LSP-gopls V1.0.2

Any suggestions?

0 Likes

#2

For Mac you need to adjust your env for non-login shells. See https://lsp.sublimetext.io/troubleshooting/#updating-the-path-used-by-lsp-servers

0 Likes

#3

Thanks for the info.

I am not sure how putting export PATH="/usr/local/bin:$PATH" in my ~/.profile is going to change the behavior. I did that, restarted my Sublime Text, the PATH printed out from the console remains the same.

I also tried

  • change that to export PATH="/Users/username/.gvm/pkgsets/go1.16.7/global:$PATH"
  • change the env in LSP setting

neither of them worked.

But I did find out that the PATH in Sublime Text console is different from my terminal, so I start the Sublime Text from terminal, and then the PATH can be inherited from the terminal. And it works, the error was gone.

It’s weird to solve the problem this way, but I am satisfied with it.

0 Likes