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: expands workspace to the wrong root when go.mod file is above go.work. #63917

Closed
findleyr opened this issue Nov 2, 2023 · 1 comment
Assignees
Labels
gopls/metadata Issues related to metadata loading in gopls gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@findleyr
Copy link
Contributor

findleyr commented Nov 2, 2023

The following bug was discovered in golang/vscode-go#2884:

output.txt:

go/packages.Load #1
	snapshot=0
	directory=file:///C:/Users/Kindred87/OneDrive%20-%20Company/Projects/Project%20Name/code
	query=[C:\Users\Kindred87\OneDrive - Company\Projects\Project Name\code\TGB\... C:\Users\Kindred87\OneDrive - Company\Projects\Project Name\code\cwb\... builtin]
	packages=3

happy_output.txt:

go/packages.Load #1
	snapshot=0
	directory=file:///C:/Users/Kindred87/OneDrive%20-%20Company/Projects/Project%20Name/code
	query=[C:\Users\Kindred87\OneDrive - Company\Projects\Project Name\code\TGB\... C:\Users\Kindred87\OneDrive - Company\Projects\Project Name\code\cwb\... builtin]
	packages=12

In other words, it looks like the same query, from the same workspace, is returning different results.

Digging deeper, I found it:

output.txt:

Params: {"type":3,"message":"2023/11/02 11:27:46 go info for C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name\\code\n(go dir C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name)\n(go version go version go1.21.3 windows/amd64)\n(valid build configuration = true)\n(build flags: [])\n(selected go env: [GO111MODULE=, GOCACHE=C:\\Users\\Kindred87\\AppData\\Local\\go-build, GOFLAGS=, GOMODCACHE=C:\\Users\\Kindred87\\go\\pkg\\mod, GOPATH=C:\\Users\\Kindred87\\go, GOPRIVATE=, GOROOT=C:\\Program Files\\Go, GOWORK=C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name\\code\\go.work])\n\n"}

happy_output.txt:

Params: {"type":3,"message":"2023/11/02 11:42:45 go info for C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name\\code\n(go dir C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name\\code)\n(go version go version go1.21.3 windows/amd64)\n(valid build configuration = true)\n(build flags: [])\n(selected go env: [GO111MODULE=, GOCACHE=C:\\Users\\Kindred87\\AppData\\Local\\go-build, GOFLAGS=, GOMODCACHE=C:\\Users\\Kindred87\\go\\pkg\\mod, GOPATH=C:\\Users\\Kindred87\\go, GOPRIVATE=, GOROOT=C:\\Program Files\\Go, GOWORK=C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name\\code\\go.work])\n\n"}

The "go dir" is being computed incorrectly. That logic is here:
https://cs.opensource.google/go/x/tools/+/master:gopls/internal/lsp/cache/view.go;l=1005;drc=cf5aad9d898a74d0f6cd041af86762f1122b926a

So @Kindred87 can you please confirm that you have a go.mod file in C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name? If that is the case, then gopls is expanding the workspace to the wrong directory. Adding another go.mod file truncates the search, as you discovered.

Gopls should instead be truncating that expansion at a go.work file, if present. This is just a bug, though we probably don't hit it that often as go.mod > go.work is relatively unlikely. All of this is being rewritten right now in https://go.dev/issue/57979, so I'm not sure if it is worth fixing this bug, which has been present for a long time. Soon gopls will "do the right thing always"...

Originally posted by @findleyr in golang/vscode-go#2884 (comment)

@findleyr findleyr added the gopls/metadata Issues related to metadata loading in gopls label Nov 2, 2023
@gopherbot gopherbot added Tools This label describes issues relating to any tools in the x/tools repository. gopls Issues related to the Go language server, gopls. labels Nov 2, 2023
@gopherbot gopherbot added this to the Unreleased milestone Nov 2, 2023
@findleyr findleyr modified the milestones: Unreleased, gopls/v0.14.2 Nov 2, 2023
@findleyr findleyr modified the milestones: gopls/v0.14.2, gopls/v0.15.0 Nov 14, 2023
@findleyr findleyr self-assigned this Feb 6, 2024
@gopherbot
Copy link

Change https://go.dev/cl/562015 mentions this issue: gopls/internal/test/integration: add a test for an inner go.work file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/metadata Issues related to metadata loading in gopls gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

2 participants