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

x/tools/gopls: failed to import standard libraries #4198

Closed
opheadacheh opened this issue Dec 19, 2024 · 2 comments
Closed

x/tools/gopls: failed to import standard libraries #4198

opheadacheh opened this issue Dec 19, 2024 · 2 comments

Comments

@opheadacheh
Copy link

What version of rules_go are you using?

v0.50.1

What version of gazelle are you using?

v0.39.1

What version of Bazel are you using?

7.1.1

Does this issue reproduce with the latest releases of all the above?

The version above is relatively new I think.

What operating system and processor architecture are you using?

Ubuntu 24.04, x86_64 (Intel(R) Core(TM) i7-10700)

gopls version

(version: v0.17.0 built with go: go1.23.4)

go env

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/wanjia-he/.cache/go-build'
GOENV='/home/wanjia-he/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/wanjia-he/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/home/wanjia-he/go'
GOPRIVATE=''
GOPROXY='https://goproxy.woa.com,direct'
GOROOT='/home/wanjia-he/.cache/bazel/_bazel_wanjia-he/c3e05d378a37a82e15a3115ca8042150/external/go_sdk'
GOSUMDB='sum.woa.com+643d7a06+Ac5f5VOC4N8NUXdmhbm8pZSXIWfhek5JSmWdWrq7pLX4'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/wanjia-he/.cache/bazel/_bazel_wanjia-he/c3e05d378a37a82e15a3115ca8042150/external/go_sdk/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.23.0'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/home/wanjia-he/.config/go/telemetry'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/dev/null'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build971942491=/tmp/go-build -gno-record-gcc-switches'

What did you do?

Our project uses bazel with a monorepo setup, I am using vscode and trying to get gopls working with my golang projects. The OS I am running is ubuntu 24.04.

My vscode settings looks like this:

"go.goroot": "${workspaceFolder}/bazel-${workspaceFolderBasename}/external/go_sdk",
    "go.toolsEnvVars": {
      "GOPACKAGESDRIVER": "${workspaceFolder}/scripts/gopackagesdriver.sh",
      "GOPACKAGESDRIVER_BAZEL_BUILD_FLAGS": "--strategy=GoStdlibList=local",
    },
    "go.enableCodeLens": {
      "runtest": false
    },
    "gopls": {
      "build.directoryFilters": [
        "-bazel-bin",
        "-bazel-out",
        "-bazel-testlogs",
        "-bazel-robotics",
      ],
      "formatting.gofumpt": true,
      "formatting.local": "data_pipeline",
      "ui.completion.usePlaceholders": true,
      "ui.semanticTokens": true,
      "ui.codelenses": {
        "gc_details": false,
        "regenerate_cgo": false,
        "generate": false,
        "test": false,
        "tidy": false,
        "upgrade_dependency": false,
        "vendor": false
      },
      "verboseOutput": true
    },
    "go.useLanguageServer": true,
    "go.lintOnSave": "off",
    "go.vetOnSave": "off",
    "go.languageServerFlags": [
      "-rpc.trace"
    ]

Here is my sample golang code:

package main

import "fmt"

func main() {
	fmt.Println("this is an agent in go")
}

What did you see happen?

So compling is fine with the project, the external packages (including the pb.go packages generated from .proto files with bazel) is working well, I can use F12 to go to the definitions, use autofills etc. However, the standard libraries are broken, e.g. fmt, http.

The source code of "fmt" package is there under ***/go_sdk/src directory.

When I hover the mouse on "fmt", it shows error:
could not import fmt (missing metadata for import of "fmt")compilerBrokenImport

If I press F12, it says:
No definition found for "fmt"

What did you expect to see?

When I hover on "fmt" and press F12, vscode should take me to the source code of "fmt".

When I call fmt.***, autofill should work.

Editor and settings

No response

Logs

[Trace - 20:44:31.202 PM] Sending request 'textDocument/hover - (118)'.
Params: {"textDocument":{"uri":"file:///home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go"},"position":{"line":2,"character":11}}

[Error - Received] 20:44:31.202 PM #118 no package data for import "fmt"

[Error - 12:44:31 PM] Request textDocument/hover failed.
Message: no package data for import "fmt"
Code: 0
[Trace - 20:44:31.385 PM] Sending request 'textDocument/hover - (119)'.
Params: {"textDocument":{"uri":"file:///home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go"},"position":{"line":2,"character":10}}

[Error - Received] 20:44:31.385 PM #119 no package data for import "fmt"

[Error - 12:44:31 PM] Request textDocument/hover failed.
Message: no package data for import "fmt"
Code: 0
[Trace - 20:44:31.543 PM] Sending request 'textDocument/codeAction - (120)'.
Params: {"textDocument":{"uri":"file:///home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go"},"range":{"start":{"line":2,"character":7},"end":{"line":2,"character":12}},"context":{"diagnostics":[{"range":{"start":{"line":2,"character":7},"end":{"line":2,"character":12}},"message":"could not import fmt (missing metadata for import of "fmt")","code":"BrokenImport","codeDescription":{"href":"https://pkg.go.dev/golang.org/x/tools/internal/typesinternal#BrokenImport"},"severity":1,"source":"compiler"}],"only":["quickfix"],"triggerKind":1}}

[Trace - 20:44:31.544 PM] Received response 'textDocument/codeAction - (120)' in 0ms.
Result: null

[Trace - 20:44:31.544 PM] Received notification 'window/logMessage'.
Params: {"type":3,"message":"2024/12/19 20:44:31 fixImports(filename="/home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go"), srcDir="/home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go" ...%!(EXTRA string=/home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go)\n"}

[Info - 12:44:31 PM] 2024/12/19 20:44:31 fixImports(filename="/home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go"), srcDir="/home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go/main.go" ...%!(EXTRA string=/home/wanjia-he/Documents/robotics/data_pipeline/upload_agent_go)

@opheadacheh
Copy link
Author

There is a similar issue: #3110

But the error is not exactly same and the solution doesn't work for my case.

@opheadacheh
Copy link
Author

Update my solution here, it turns out in our project .bazelrc file, following options were set for some other purposes:

build --linkopt="-Wl,-rpath,\$ORIGIN"
build --linkopt="-Wl,-rpath,\$ORIGIN/lib"

Just comment out those 2 lines and things start to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant