golang / vscode-go

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

Import error with go mod dependencies when "Use Language Server" is enabled #2882

Closed golfxiao closed 1 year ago

golfxiao commented 1 year ago

Description: When the "Use Language Server" option is enabled in VS Code with the vscode-go extension, importing dependencies managed by go mod results in an error. The error message suggests a failure to read cache data for the imported package and indicates a problem with hashing the gopls executable.

Steps to Reproduce:

Open VS Code with the vscode-go extension installed. Enable the "Use Language Server" option in the settings. Create a Go module that depends on external packages managed by go mod, such as gnetis.com/golang/core/golib/uac. Import the package in a Go source file. Expected Behavior: The imported package should be recognized and imported without any errors or issues.

Actual Behavior: The following error message is displayed:

📋 Copy code could not import gnetis.com/golang/core/golib/uac (failed to read cache data for gnetis.com/golang/core/golib/uac: can't hash gopls executable: cannot find executable path)compilerBrokenImport Additional Information:

This issue occurs consistently when the "Use Language Server" option is enabled. Disabling the "Use Language Server" option resolves the import error. The go get and go build commands work correctly with the same dependencies.

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

Share the Go related settings you have added/edited

{ "typescript.useCodeSnippetsOnMethodSuggest": true, "files.autoSave": "onFocusChange", "python.disablePromptForFeatures": [ "pylint" ], "go.testFlags": [ "-v" ], "extensions.ignoreRecommendations": true, "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true }, "go.formatTool": "goimports", "[go]": { "editor.snippetSuggestions": "none", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true, } }, "gopls": { "usePlaceholders": true, // add parameter placeholders when completing a function // Experimental settings "completeUnimported": true, // autocomplete unimported packages "deepCompletion": true, // enable deep completion }, "files.eol": "\n", "go.toolsManagement.autoUpdate": true, "go.toolsGopath": "/Users/a200007/work/ucgit/golang", "explorer.confirmDelete": false, "search.followSymlinks": false, "files.exclude": { "/.git": true, "/.svn": true, "/.hg": true, "/CVS": true, "/.DS_Store": true, "/tmp": true, "/node_modules": true, "/bower_components": true, "/dist": true }, "files.watcherExclude": { "/.git/objects/": true, "/.git/subtree-cache/": true, "/node_modules/": true, "/tmp/": true, "/bower_components/": true, "/dist/*": true }, "editor.fontSize": 14, "security.workspace.trust.untrustedFiles": "open", "editor.largeFileOptimizations": false, "dart.flutterSdkPath": "/Users/a200007/work/software/flutter", "http.proxyAuthorization": null, "workbench.colorTheme": "Default Light+", "vscodeGoogleTranslate.preferredLanguage": "Chinese (Simplified)", "github.copilot.advanced": {}, "[python]": { "editor.formatOnType": true }, "codeium.enableSearch": true, "codeium.enableConfig": { "": true }, "go.languageServerFlags": [ "-rpc.trace" ] }

findleyr commented 1 year ago

Hmm, it looks like os.Executable() is failing from the gopls binary.

Do you have any idea why that may be the case? Could you write a small Go program to see if it reproduces? E.g.: https://go.dev/play/p/ZVqeBuju87z

golfxiao commented 1 year ago

I don't understand why you typing this problem as WaitingForInfo status, but what's missing?
Are Findleyr's comments above talking to me? The output path of the os.Executable() function is my GoPath path: /Users/a200007/work/ucgit/golang.

Hmm, it looks like os.Executable() is failing from the gopls binary.

Do you have any idea why that may be the case? Could you write a small Go program to see if it reproduces? E.g.: https://go.dev/play/p/ZVqeBuju87z

/Users/a200007/work/ucgit/golang/

findleyr commented 1 year ago

Thanks @golfxiao. Since it sounds like linking isn't broken, would you try reinstalling gopls? go install golang.org/x/tools/gopls@latest?

If that doesn't work, as a temporary fix you can install a version of gopls that didn't depend on access to os.Executable(): go install golang.org/x/tools/gopls@v0.11.0.

golfxiao commented 1 year ago

Thanks @finkleyr. The older version of gopls, v0.11.0, is working. I will temporarily use this version for now.