golang / vscode-go

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

For any files which have `linux` or `windows` in their names, got this error: This file is within module ".", which is not included in your workspace. #3120

Closed qianzhangxa closed 6 months ago

qianzhangxa commented 6 months 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_. - 1.21.5 * Run `gopls -v version` to get version of Gopls from _the VS Code integrated terminal_. - v0.14.2 * Run `code -v` or `code-insiders -v` to get version of VS Code or VS Code Insiders. - 1.85.1 * Check your installed extensions to get the version of the VS Code Go extension - v0.40.1 * Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > `Go: Locate Configured Go Tools` command. ``` Checking configured tools.... GOBIN: undefined toolsGopath: gopath: /Users/qzhang/go GOROOT: /Users/qzhang/sdk/go1.21.5 PATH: /Users/qzhang/sdk/go1.21.5/bin:/usr/local/Cellar/pyenv-virtualenv/1.2.1/shims:/Users/qzhang/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/qzhang/bin:/usr/local/go/bin PATH (vscode launched with): /usr/local/Cellar/pyenv-virtualenv/1.2.1/shims:/Users/qzhang/.pyenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/qzhang/bin:/usr/local/go/bin go: /Users/qzhang/sdk/go1.21.5/bin/go: go version go1.21.5 darwin/amd64 gopls: /Users/qzhang/go/bin/gopls (version: v0.14.2 built with go: go1.21.5) gotests: not installed gomodifytags: not installed impl: not installed goplay: not installed dlv: /Users/qzhang/go/bin/dlv (version: v1.21.2 built with go: go1.21.5) staticcheck: /Users/qzhang/go/bin/staticcheck (version: v0.4.6 built with go: go1.21.5) go env Workspace Folder (kubernetes): /Users/qzhang/Works/projects/go/src/k8s.io/kubernetes GO111MODULE='' GOARCH='amd64' GOBIN='' GOCACHE='/Users/qzhang/Library/Caches/go-build' GOENV='/Users/qzhang/Library/Application Support/go/env' GOEXE='' GOEXPERIMENT='' GOFLAGS='' GOHOSTARCH='amd64' GOHOSTOS='darwin' GOINSECURE='' GOMODCACHE='/Users/qzhang/go/pkg/mod' GONOPROXY='' GONOSUMDB='' GOOS='darwin' GOPATH='/Users/qzhang/go' GOPRIVATE='' GOPROXY='https://proxy.golang.org,direct' GOROOT='/Users/qzhang/sdk/go1.21.5' GOSUMDB='sum.golang.org' GOTMPDIR='' GOTOOLCHAIN='auto' GOTOOLDIR='/Users/qzhang/sdk/go1.21.5/pkg/tool/darwin_amd64' GOVCS='' GOVERSION='go1.21.5' GCCGO='gccgo' GOAMD64='v1' AR='ar' CC='clang' CXX='clang++' CGO_ENABLED='1' GOMOD='/Users/qzhang/Works/projects/go/src/k8s.io/kubernetes/go.mod' 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 -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/7d/xksqy41d6fld6c9x9y2z8kmw0000gn/T/go-build2905093160=/tmp/go-build -gno-record-gcc-switches -fno-common' ```

Share the Go related settings you have added/edited

    "go.toolsManagement.autoUpdate": true,
    "[go]": {
        "editor.insertSpaces": false,
        "editor.formatOnSave": true,
        "editor.codeActionsOnSave": {
            "source.organizeImports": "explicit"
        }
    },
    "go.toolsEnvVars": {},
    "gopls": {}

Describe the bug

I am using VS code with Go extension to check Kubernetes source code in my macOS, and I found for any files which have linux or windows in their names (e.g. cgroup_manager_linux.go), I always get this error:

[{
    "resource": "/Users/qzhang/Works/projects/go/src/k8s.io/kubernetes/pkg/kubelet/cm/cgroup_manager_linux.go",
    "owner": "_generated_diagnostic_collection_name_#0",
    "severity": 4,
    "message": "This file is within module \".\", which is not included in your workspace.\nTo fix this problem, you can add a go.work file that uses this directory.\nSee the documentation for more information on setting up your workspace:\nhttps://github.com/golang/tools/blob/master/gopls/doc/workspace.md.",
    "source": "go list",
    "startLineNumber": 17,
    "startColumn": 9,
    "endLineNumber": 17,
    "endColumn": 11
}]

Steps to reproduce the behavior:

  1. Go to any files which have linux or windows in their names.
  2. See the error below.

image

hyangah commented 6 months ago

It is https://github.com/golang/go/issues/29202 (os/arch in filenames serve like build tags).

Good news - the upcoming version of the language server is going to improve the build tag handling. gopls v0.15.0-pre.1 is currently out for testing. Can you give it a try and provide feedback/bug reports? (cc @findleyr)

go install golang.org/x/tools/gopls@v0.15.0-pre.1

And run "Go: Restart Language Server" command.

qianzhangxa commented 6 months ago

@hyangah Yes, gopls v0.15.0-pre.1 has fixed this issue, thanks!

BTW, do you happen to know when gopls v0.15.0 will be released?

findleyr commented 6 months ago

@qianzhangxa we're taking out time as this is a large change and we want to have another prerelease, but probably within the next few weeks. Thank you for testing.

findleyr commented 6 months ago

Closing as this will be fixed by gopls@v0.15.0.