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

gopls: Go workspaces are no longer working correctly #2884

Closed
Josh-Ring-SD opened this issue Jul 21, 2023 · 21 comments
Closed

gopls: Go workspaces are no longer working correctly #2884

Josh-Ring-SD opened this issue Jul 21, 2023 · 21 comments
Labels
WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Milestone

Comments

@Josh-Ring-SD
Copy link

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.
    • go1.20.6 linux/amd64
  • Run gopls -v version to get version of Gopls from the VS Code integrated terminal.
    • Build info

golang.org/x/tools/gopls v0.12.4
golang.org/x/tools/[email protected] h1:nce5etAamR46d9oNGxop1aRK5rDQ0NqcY/SHIcyfEKY=
github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/google/[email protected] h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
golang.org/x/[email protected] h1:+WEEuIdZHnUeJJmEUjyYC2gfUMj69yZXw17EnHg/otA=
golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/[email protected] h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/[email protected] h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/[email protected] h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s=
golang.org/x/[email protected] h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/[email protected] h1:5PWemM67wMSPpO0Y3lOPlyvgO3z56YkZRxPFcdd300g=
golang.org/x/[email protected] h1:A9kONVi4+AnuOr1dopsibH6hLi1Huy54cbeJxnq4vmU=
honnef.co/go/[email protected] h1:6qXr+R5w+ktL5UkwEbPp+fEvfyoMPche6GkOpGHZcLc=
mvdan.cc/[email protected] h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM=
mvdan.cc/xurls/[email protected] h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
go: go1.20.2

  • Run code -v or code-insiders -v to get version of VS Code or VS Code Insiders.

    • 1.80.1
      74f6148eb9ea00507ec113ec51c489d6ffb4b771
      x64
  • Check your installed extensions to get the version of the VS Code Go extension

    • v0.39.1
  • Run Ctrl+Shift+P (Cmd+Shift+P on Mac OS) > Go: Locate Configured Go Tools command.

    • GOBIN: undefined
      toolsGopath:
      gopath: /home/joshuaring/go
      GOROOT: /usr/local/go
      PATH: /home/joshuaring/.nvm/versions/node/v18.12.1/bin:/home/joshuaring/miniconda3/bin:/home/joshuaring/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin:/home/joshuaring/zig/zig-linux-x86_64-0.11.0-dev.53+cbed6bb08:/home/joshuaring/zls/bin:/home/joshuaring/apps/blender-3.3.1-linux-x64:/usr/local/go/bin:/home/joshuaring/zig/zig-linux-x86_64-0.11.0-dev.53+cbed6bb08:/home/joshuaring/zls/bin:/home/joshuaring/apps/blender-3.3.1-linux-x64:/home/joshuaring/Projects/data_team_monorepo/infrastructure/node_modules/.bin

    go: /usr/local/go/bin/go: go version go1.20.6 linux/amd64

    gotests: not installed
    gomodifytags: not installed
    impl: not installed
    goplay: not installed
    dlv: /home/joshuaring/go/bin/dlv (version: v1.21.0 built with go: go1.20.6)
    staticcheck: not installed
    gopls: /home/joshuaring/go/bin/gopls (version: v0.12.4 built with go: go1.20.2)

go env
Workspace Folder (test): /home/joshuaring/Projects/test
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/joshuaring/.cache/go-build"
GOENV="/home/joshuaring/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/joshuaring/go/pkg/mod"
GONOPROXY="github.com/silverdoorapartments"
GONOSUMDB="github.com/silverdoorapartments"
GOOS="linux"
GOPATH="/home/joshuaring/go"
GOPRIVATE="github.com/silverdoorapartments"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.6"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="0"
GOMOD="/home/joshuaring/Projects/go.mod"
GOWORK="/home/joshuaring/Projects/test/go.work"
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build1384710745=/tmp/go-build -gno-record-gcc-switches"

Share the Go related settings you have added/edited

Run Preferences: Open Settings (JSON) command to open your settings.json file.
Share all the settings with the go. or ["go"] or gopls prefixes.

"go.testFlags": [
    "-v"
],
"go.testTimeout": "99999s",

Describe the bug

A clear and concise description of what the bug.

  • In VScode see errors on highlight of each module's package PACKAGE_NAME_HERE line like:

This file is within module "bar", which is not included in your workspace.
To fix this problem, you can add this module to your go.work file (/home/joshuaring/Projects/test/go.work)
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.go list

  • In VScode see errors on hover of the import from the other module:
import (
	"example-foo.com/foo_package"
)

error:

could not import example-foo.com/foo_package (current file is not included in a workspace module)compiler[BrokenImport]

A clear and concise description of what you expected to happen.

  • Expect to be able to ctrl click into each module's functions
  • Expect to be able hover and see description of function
  • Expect to be able to import module from within workspace
  • Expect the workspace file to be read

Steps to reproduce the behavior:

  • create two directories foo and bar
  • in the foo directory init a module via go mod init example-foo.com
  • in the bar directory init a module via go mod init example-bar.com
  • in the parent directory above the module directories go work init and go work use foo bar
  • This gives a go.work like:
go 1.20

use (
	./bar
	./foo
)
  • The go.mod file in directory foo:
module example-foo.com

go 1.20
  • The go.mod file in the directory bar edited to the following, so it can import the foo module
module example-bar.com

go 1.20

require (
	example-foo.com/foo_package v0.0.0
)

replace example-foo.com/foo_package v0.0.0 => ../foo

  • added a main.go to foo directory:
package foo_package

import "log"

func GreatSandra() {
	log.Print("hello from foo")
}
  • added a main.go to bar directory:
package main

import (
	"log"

	"example-foo.com/foo_package"
)

func GreatScot() {
	foo_package.GreatSandra()
}

func main() {
	GreatScot()
}

  • Program runs correctly outputting:
2023/07/21 14:46:49 hello from foo

Screenshots or recordings

If applicable, add screenshots or recordings to help explain your problem.

image
image

output from Go pls :

https://pastebin.com/qhb6LXGR

@gopherbot gopherbot added this to the Untriaged milestone Jul 21, 2023
@Josh-Ring-SD Josh-Ring-SD changed the title Go workspaces are no longer working correctly gopls: Go workspaces are no longer working correctly Jul 21, 2023
@findleyr
Copy link
Member

Hi, thanks for the report. This is strange, and clearly very confusing. However, I can't reproduce this with your reproducer. I expected something obvious, but nothing obvious jumps out at me from your logs (thanks for providing them).

Could you please try the following: restart gopls (Go: Restart Language Server) in the command palette. Does the problem persist? If so, I'm very confused.

I need more information to diagnose this. Could you please collect full RPC logs from a short session demonstrating the problem, following the instructions here: https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information

Thanks!

@findleyr findleyr added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Jul 25, 2023
@Josh-Ring-SD
Copy link
Author

Josh-Ring-SD commented Aug 1, 2023

Unfortunately could not be resolved easily by and language server restarts, tried also nuking the config and also everything in the go env and temporary files for the system too, could not make any progress.

For now the only way to make progress was to make a new system user as there was some remnant of something which was not working as expected which I could not resolve. The new user was able to work correctly, but definitely a nuclear solution to this. Would be good to figure out what happened!

Log (Extension Host)

2023-08-01 13:49:06.684 [info] Extension host with pid 272058 started 2023-08-01 13:49:06.684 [info] Skipping acquiring lock for /home/joshuaring/.config/Code/User/workspaceStorage/26ac9007df1a271fa574b9172138ee02. 2023-08-01 13:49:06.801 [info] ExtensionService#_doActivateExtension vscode.emmet, startup: false, activationEvent: 'onLanguage' 2023-08-01 13:49:06.812 [info] ExtensionService#_doActivateExtension golang.go, startup: false, activationEvent: 'onLanguage:go' 2023-08-01 13:49:07.264 [info] ExtensionService#_doActivateExtension vscode.git-base, startup: true, activationEvent: '*', root cause: vscode.git 2023-08-01 13:49:07.313 [info] ExtensionService#_doActivateExtension vscode.git, startup: true, activationEvent: '*' 2023-08-01 13:49:07.346 [info] ExtensionService#_doActivateExtension vscode.github, startup: true, activationEvent: '*' 2023-08-01 13:49:07.432 [info] Eager extensions activated 2023-08-01 13:49:07.504 [error] [golang.go] provider FAILED 2023-08-01 13:49:07.517 [error] Error: semantictokens are disabled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:07.519 [info] ExtensionService#_doActivateExtension vscode.debug-auto-launch, startup: false, activationEvent: 'onStartupFinished' 2023-08-01 13:49:07.525 [info] ExtensionService#_doActivateExtension vscode.merge-conflict, startup: false, activationEvent: 'onStartupFinished' 2023-08-01 13:49:07.679 [error] [golang.go] provider FAILED 2023-08-01 13:49:07.679 [error] Error: semantictokens are disabled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:07.801 [info] ExtensionService#_doActivateExtension vscode.github-authentication, startup: false, activationEvent: 'onAuthenticationRequest:github' 2023-08-01 13:49:07.835 [info] ExtensionService#_doActivateExtension vscode.microsoft-authentication, startup: false, activationEvent: 'onAuthenticationRequest:microsoft' 2023-08-01 13:49:08.451 [error] [golang.go] provider FAILED 2023-08-01 13:49:08.452 [error] Error: no package data for import "example-foo.com" at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:10.210 [error] [golang.go] provider FAILED 2023-08-01 13:49:10.210 [error] Error: no package data for import "example-foo.com" at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:19.976 [error] [golang.go] provider FAILED 2023-08-01 13:49:19.976 [error] Error: semantictokens are disabled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:20.101 [error] [golang.go] provider FAILED 2023-08-01 13:49:20.101 [error] Error: semantictokens are disabled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:21.662 [error] [golang.go] provider FAILED 2023-08-01 13:49:21.662 [error] Error: context canceled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:21.663 [error] [golang.go] provider FAILED 2023-08-01 13:49:21.663 [error] Error: semantictokens are disabled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21) 2023-08-01 13:49:21.811 [error] [golang.go] provider FAILED 2023-08-01 13:49:21.811 [error] Error: semantictokens are disabled at handleResponse (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:2126:40) at handleMessage (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1936:11) at processMessageQueue (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1951:13) at Immediate. (/home/joshuaring/.vscode/extensions/golang.go-0.39.1/dist/goMain.js:1927:11) at process.processImmediate (node:internal/timers:466:21)

@findleyr
Copy link
Member

findleyr commented Aug 2, 2023

Thanks for following up, @Josh-Ring-SD.

I'm afraid we don't have enough information to figure this out. Do you still have access to the old, broken user?

@findleyr findleyr added WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. and removed WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. labels Aug 2, 2023
@Josh-Ring-SD
Copy link
Author

I do still have access to that user yes.

Open to any suggestions I ran out of ideas honestly

@findleyr
Copy link
Member

It sounds like something was broken in your go environment (via go env -w, perhaps?). We'd need full LSP logs (including RPCs) to determine this, not just the extension host logs (as described at https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information).

Given that this appears to be some sort of isolated environment corruption, it may not be worth tracking down.

@Josh-Ring-SD
Copy link
Author

It sounds like something was broken in your go environment (via go env -w, perhaps?). We'd need full LSP logs (including RPCs) to determine this, not just the extension host logs (as described at https://github.com/golang/vscode-go/blob/master/docs/troubleshooting.md#collect-gopls-information).

Given that this appears to be some sort of isolated environment corruption, it may not be worth tracking down.

I could not recover the environment despite resetting everything I could find, I threw in the towel here

@andremelinski
Copy link

I had to delete my go.mod and create a new one (idk why).. setup golang is vscode looks like magic

@byronic-azure
Copy link

byronic-azure commented Sep 19, 2023 via email

@annybs
Copy link

annybs commented Oct 8, 2023

I'm late to this party, but I had a similar problem as @Josh-Ring-SD this week.

I created a Go workspace that worked fine on my MacBook, but then I experienced the same issue as OP when I cloned it to my Ubuntu workstation:

This file is within module "[...]", which is not included in your workspace.
To fix this problem, you can add this module to your go.work file ([...]/go.work)
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.

  • All typical Go extension options
  • No modifications to gopls config. Pretty much off-the-shelf usage
  • Go installed on my Ubuntu system by extracting the tar.gz to $HOME/go

I experimented with restarting the language server and mucking about with env, with no luck. What clued me in to my issue was that while GOWORK referenced my go.work file correctly, GOMOD referenced $HOME/go/src/go.mod because I had placed my workspace inside of $HOME/go/src/github.com/my-organisation/my-repository causing GOMOD resolution to reach Go internal source. It seems like this caused gopls to override my Go workspace in favour of what it inferred as the root module for my VS Code window.

I figured out there are two solutions to this:

  • Run go mod init in the workspace root and gitignore the resulting go.mod file. This compels gopls to treat your workspace root as the de facto root directory, and sub-packages work fine
  • Move the workspace out of $HOME/go entirely, e.g. $HOME/Documents/MyProject. As long as there is no go.mod file in parent directories, GOMOD resolves to /dev/null (or whatever is appropriate for your OS) allowing the Go workspace to take precedence

I'm not sure whether this is a bug or expected behaviour in the VS Code extension or gopls itself, but both have worked for me (and I'm going to stick with the latter!)

@Kindred87
Copy link

Kindred87 commented Nov 2, 2023

@annybs Was just having this issue today on a fresh Go install with a workspace in an isolated directory. Compiler had no trouble building workspace sub-module A which imported sub-module B, but the VS Code extension wouldn't provide Intellisense for any of the imported objects. Running "go mod init t" in the workspace root solved the issue.

@findleyr If you would like to take a look, I removed the go.mod file in the workspace root, restarted the language server, and captured the output in the attached.

output.txt

Same output, but after running "go mod init t" in the workspace root.
happy_output.txt

Directory tree, in case that's of use to see the relationships between modules.

Directory Tree

@findleyr
Copy link
Member

findleyr commented Nov 2, 2023

@Kindred87 what is the content of your go.work file?

@Kindred87
Copy link

Kindred87 commented Nov 2, 2023

@findleyr

go 1.21.3

import (
	"./TGB"
	"./cwb"
)

@findleyr
Copy link
Member

findleyr commented Nov 2, 2023

Sorry for missing the response from @annybs above.

Something is definitely wrong here:

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.

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"...

@findleyr
Copy link
Member

findleyr commented Nov 2, 2023

Actually, I think we should fix this first, since the fix should be easy. I filed https://go.dev/issue/63917 to follow up. Thanks for the detailed analysis, @annybs and @Kindred87!

@Kindred87
Copy link

Confirming that when go.mod, with the contents

module t

go 1.21.3

is in C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name, the same directory that go.work is in, the happy_output.txt results are produced.

@findleyr
Copy link
Member

findleyr commented Nov 2, 2023

@Kindred87 to be absolutely clear, I think the go.mod file must be in is in C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name\code (the code subdirectory) in order for this to work as expected.

Do you have a go.mod file in C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name, which is the parent directory of the directory containing go.work? I think you must -- if you can confirm then I believe I understand the bug.

@Kindred87
Copy link

@findleyr Ah, I understand the distinction. There was indeed a go.mod file in the parent directory, C:\\Users\\Kindred87\\OneDrive - Company\\Projects\\Project Name, in addition to the one I added to code. Removing both resolves the issue.

@quenbyako
Copy link

quenbyako commented Jan 2, 2024

Hi Guys, third year in a row i got absolutely similar problem.

Here is my error message:

This file is within module "../../../../../repositories/mtproto", which is not included in your workspace.
To fix this problem, you can add this module to your go.work file (/Users/quenbyako/repositories/mtproto/go.work)
See the documentation for more information on setting up your workspace:
https://github.com/golang/tools/blob/master/gopls/doc/workspace.md.go list

It's REALLY confusing and i have no idea what's happening, especially since i adding go.work files to every top level modules without real purpose or usage of this workspace.

One more important thing to mention: It's always no difference between single root editor (like one folder per window) and multi-root editor (files added via code --add ./otherpkg), error is always same: "this file within module ../../../../..(billion times)../.. which is not included in your workspace"

And also: i have only top level modules, so there are no submodules or something like this. One module per one root.

@findleyr May i ask you to look at this issue too?

EDIT: what is most unfunny is that this error is completely phantom: It randomly appears and randomly dissapears without any (even theoretical) reason, why it might happen. No edits of modules, no edits, nothing. Just running a test, for example.

Will be happy to provide any logs any configs, any info that will help yall 🙏 just please help

@findleyr
Copy link
Member

findleyr commented Jan 3, 2024

@quenbyako with the recently closed #57979 your workspace layout should be automatically detected. However, this feature has not yet been released.

Could you please try installing gopls@master?

git clone https://go.googlesource.com/tools && cd tools/gopls && go install

Let us know how it goes.

(as for the most unfunny part: if you are using an editor that automatically detects workspace folders, I suspect that you've opened an outer module before opening a nested module. Most editors will not detect this subdirectory as a logically distinct workspace folder)

@quenbyako
Copy link

quenbyako commented Jan 3, 2024

@findleyr Thanks for quick response! Tried it for a whole day, unfortunately it's not helping 😢

One thing i noticed: for some reason, if i opening file in two different folders, everything starts to die, for example: first opened file is in root, second one is in root also, but related to package_test package, third one, for example in sub directory (not in submodule, just a folder)

I wondering how exactly gopls looking into the codebase, cause every possible structure, slightly more complex than "one opened file, one root, flat module structure" continuously fails for a looong long time (3-4 years at least)

Regarding to random fails:

if you are using an editor that automatically detects workspace folders, I suspect that you've opened an outer module before opening a nested module.

I never ever using sub or nested modules, so i don't thing that it ever happened in my case. all my work with go projects are always: git repo in root, only one go.mod in root, maybe some symlinks, but they never loop

@hyangah
Copy link
Contributor

hyangah commented Feb 6, 2024

@quenbyako Can you please open a separate issue and include your workspace structure (including 1. where go.mod files are, 2. where go.work files are and what they have, and 3. gopls traces)? Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

9 participants