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: support ad-hoc packages with multiple files #36416

Wingboy-Jim2 opened this issue Jan 6, 2020 · 8 comments

x/tools/gopls: support ad-hoc packages with multiple files #36416

Wingboy-Jim2 opened this issue Jan 6, 2020 · 8 comments


Copy link

@Wingboy-Jim2 Wingboy-Jim2 commented Jan 6, 2020

What version of Go are you using (go version)?

$ go version
go version go1.13.1 windows/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Jim\AppData\Local\go-build
set GOENV=C:\Users\Jim\AppData\Roaming\go\env
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\Jim\go
set GOPROXY=,direct
set GOROOT=c:\go
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\Jim\AppData\Local\Temp\go-build664791186=/tmp/go-build -gno-record-gcc-switches

What did you do?

I started up VS Code on a directory. There was a single package (main) spread across multiple files. There was no go.mod file.

When looking at a file VS Code displayed multiple undeclared name errors for the current file.

Although true that the current file does not declare that names these names were declared in other files in the same directory.

Exiting VS Code and doing a go build in the directory builds with no errors. But entering back into VS Code shows errors for any file that I view in the editor.

What did you expect to see?

No errors

What did you see instead?

Errors for undeclared names. I get the same errors when do a gopls check on any file.

Creating a go.mod file addresses this, even if I do a gopls check on a single file.

First submitted this as a microsoft/vscode-go#2967 before I determined that gopls seemed to be the involved.

@gopherbot gopherbot added this to the Unreleased milestone Jan 6, 2020
@gopherbot gopherbot added the Tools label Jan 6, 2020
Copy link

@gopherbot gopherbot commented Jan 6, 2020

Thank you for filing a gopls issue! Please take a look at the Troubleshooting guide, and make sure that you have provided all of the relevant information here.

@gopherbot gopherbot added the gopls label Jan 6, 2020
Copy link

@stamblerre stamblerre commented Jan 6, 2020

@Wingboy-Jim2: It sounds like you are working outside of GOPATH and without modules. gopls will only partially work in that case, since that set up is considered "ad-hoc". It seems like the correct solution in your case is to initialize a module, as you did by adding a go.mod file.

Copy link

@Wingboy-Jim2 Wingboy-Jim2 commented Jan 6, 2020

What was a pain was that I have been working in such an ad hoc means for over two and a half years without any consequences.

I have not seen any reason not to initialize a module, but it would have saved me a few hours of research and aggravation if the change in requirements had been more clearly presented. At the very least I hope that this ticket will reduce the time for anyone else functioning in such a fashion.

@stamblerre stamblerre changed the title x/tools/gopls: VS Code reports errors only using a single file in a directory with multiple files. x/tools/gopls: support ad-hoc packages with multiple files Jan 6, 2020
Copy link

@stamblerre stamblerre commented Jan 6, 2020

The recommendation to use GOPATH is pretty common in Go documentation (for example,, and this specific issue with gopls has been discussed on a number of issues, including #33729 (comment). CL 202277 will add support for ad-hoc packages with multiple files, but I agree that, in the meantime, we should make the error messaging more clear. I will mail a CL to show a warning message.

Copy link

@gopherbot gopherbot commented Jan 6, 2020

Change mentions this issue: internal/lsp: warn user in case of a multi-file ad-hoc package

@stamblerre stamblerre modified the milestones: Unreleased, gopls v1.0 Jan 6, 2020
gopherbot pushed a commit to golang/tools that referenced this issue Jan 9, 2020
This change surfaces a warning to the user if they might be coding in a
multi-file ad-hoc package.

Updates golang/go#36416

Change-Id: Ifaa15a0777ea97e62c1477fb33911636b13e073e
Run-TryBot: Rebecca Stambler <>
TryBot-Result: Gobot Gobot <>
Reviewed-by: Heschi Kreinick <>
Copy link

@stamblerre stamblerre commented Jan 31, 2020

@Wingboy-Jim2: This will work as you expect in gopls/v0.3.0 (you can try it out now by running GO111MODULE=on go get

Leaving this issue open, as the fix should still be made in go/packages.

Copy link

@gopherbot gopherbot commented Jan 31, 2020

Change mentions this issue: go/packages: support multi-file ad-hoc packages

@stamblerre stamblerre modified the milestones: gopls/v1.0.0, gopls/v0.4.0 Feb 2, 2020
Copy link

@stamblerre stamblerre commented Feb 4, 2020

As discussed on the above CL, the additional work-arounds in go/packages are not worth the complexity, particularly since this issue has now been addressed in gopls. Closing as a result.

@stamblerre stamblerre closed this Feb 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.