golang / vscode-go

Go extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=golang.Go
Other
3.88k stars 752 forks source link

vscode-go cannot run `go env` on Windows #2643

Open clowenhg opened 1 year ago

clowenhg commented 1 year ago

What version of Go, VS Code & VS Code Go extension are you using?

Version Information
* Run `go version` to get version of Go from _the VS Code integrated terminal_. - go version go1.20 windows/amd64 - also experienced with go1.19 windows/amd64 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. Build info ---------- golang.org/x/tools/gopls 0.4.3 golang.org/x/tools/gopls@v0.4.3 h1:irz7Q+XdHNECamFKbNWKvMV2Ak6zBbwdwbZndG4545I= github.com/BurntSushi/toml@v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= golang.org/x/mod@v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= golang.org/x/sync@v0.0.0-20190911185100-cd5d95a43a6e h1:vcxGaoTs7kV8m5Np9uUNQin4BrLOthgV7252N8V+FwY= golang.org/x/tools@v0.0.0-20200708181441-6004c8539734 h1:Vc0Vx98oU/O3+qPQ36fnTT5UduS55KLh2uSGbL7mqEo= golang.org/x/xerrors@v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= honnef.co/go/tools@v0.0.1-2020.1.4 h1:UoveltGrhghAA7ePc+e+QYDHXrBps2PqFZiHkGR/xK8= mvdan.cc/xurls/v2@v2.2.0 h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A= Go info ------- go version go1.20 windows/amd64 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. 1.75.0 e2816fe719a4026ffa1ee0189dc89bdfdbafb164 x64 * Check your installed extensions to get the version of the VS Code Go extension - v0.37.1 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. - Command 'Go: Locate Configured Go Tools' resulted in an error (command 'go.locate.tools' not round)

Share the Go related settings you have added/edited

"go.gopath": "$HOME/go",
    "go.lintFlags": [
        "--fast",
        "--skip-dirs='vendor'",
        "--print-resources-usage"
    ],
    "go.useLanguageServer": true,
    "go.formatTool": "gofumports",
    "go.lintTool": "golangci-lint",
    "go.lintOnSave": "file",
    "go.autocompleteUnimportedPackages": true,
    "go.testOnSave": false,
    "go.buildOnSave": "off",
    "go.editorContextMenuCommands": {
        "toggleTestFile": true,
        "addTags": true,
        "removeTags": false,
        "testAtCursor": true,
        "testFile": false,
        "testPackage": false,
        "generateTestForFunction": true,
        "generateTestForFile": false,
        "generateTestForPackage": false,
        "addImport": true,
        "testCoverage": true,
        "playground": false,
        "debugTestAtCursor": true
    },
    "go.enableCodeLens": {
        "references": false,
        "runtest": false
    },
    "go.useCodeSnippetsOnFunctionSuggest": true,
    "go.useCodeSnippetsOnFunctionSuggestWithoutType": false,
    "go.gotoSymbol.includeImports": true,
    "go.buildTags": "dev",
    "go.liveErrors": {
        "enabled": true,
        "delay": 500
    },
    "[go]": {
        "editor.snippetSuggestions": "none",
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }
    },
    "gopls": {
        "usePlaceholders": true,
    },

Describe the bug

The Go extension appears to fail initializing. I have no code completion, automatic vetting, linting or formatting.

In my Output for Go:

Failed to run 'C:\Program Files\Go\bin\go.exe env' (cwd: D:\work\spartan-dog\chase-a-lot\mocks): Error: Command failed: C:\Program Files\Go\bin\go.exe env -json GOPATH GOROOT GOPROXY GOBIN GOMODCACHE
go: GOPATH entry is relative; must be absolute path: "$HOME/go".
For more details see: 'go help gopath'

go: GOPATH entry is relative; must be absolute path: "$HOME/go".
For more details see: 'go help gopath'

The locations are correct.

I think relevantly, my default terminal is PowerShell and the above command 'C:\Program Files\Go\bin\go.exe env' would be invalid and should be prefixed with an &.

& 'C:\Program Files\Go\bin\go.exe' env -json GOPATH GOROOT GOPROXY GOBIN GOMODCACHE Yields:

PS D:\work\spartan-dog\chase-a-lot\mocks> & 'C:\Program Files\Go\bin\go.exe' env -json GOPATH GOROOT GOPROXY GOBIN GOMODCACHE
{
        "GOBIN": "",
        "GOMODCACHE": "C:\\Users\\Christopher\\go\\pkg\\mod",
        "GOPATH": "C:\\Users\\Christopher\\go",
        "GOPROXY": "https://proxy.golang.org,direct",
        "GOROOT": "C:\\Program Files\\Go"
}

I'd expect the extension to behave as it does in other environments. It currently works exactly as I'd like inside of a WSL2 Ubuntu instance on the same machine.

Steps to reproduce the behavior:

  1. Start vscode with Go extension

Screenshots or recordings

N/A

clowenhg commented 1 year ago

I have actually figured out what the problem was. Simply removing: "go.gopath": "$HOME/go", from my settings fixed the problem.

I'm hoping this helps others.

I don't know how this got set in the first place, so perhaps this is a bug in the installation. I'm inclined to leave this open as this may be a bug in the setup.

loveyourstack commented 11 months ago

I have actually figured out what the problem was. Simply removing: "go.gopath": "$HOME/go", from my settings fixed the problem.

Thank you, you got me along the right lines. After the latest VSCode update, I guess my go.gopath under Remote/WSL2 settings was set to "" (empty string). I copied in the value I found by running "go env", saved, restarted VSCode, and it seems ok again now.

hosseinmirzapur commented 11 months ago

I have actually figured out what the problem was. Simply removing: "go.gopath": "$HOME/go", from my settings fixed the problem.

I'm hoping this helps others.

I don't know how this got set in the first place, so perhaps this is a bug in the installation. I'm inclined to leave this open as this may be a bug in the setup.

Thanks for your help, saved me an hour of searching for such little problem