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

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

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

Wingboy-Jim2 opened this issue Jan 6, 2020 · 8 comments
Labels
FrozenDueToAge 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

@Wingboy-Jim2
Copy link

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 GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Jim\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=c:\go\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-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 This label describes issues relating to any tools in the x/tools repository. label Jan 6, 2020
@gopherbot
Copy link

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 Issues related to the Go language server, gopls. label Jan 6, 2020
@stamblerre
Copy link
Contributor

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

@Wingboy-Jim2
Copy link
Author

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
@stamblerre
Copy link
Contributor

The recommendation to use GOPATH is pretty common in Go documentation (for example, https://golang.org/doc/install#testing), 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.

@gopherbot
Copy link

Change https://golang.org/cl/213458 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
Reviewed-on: https://go-review.googlesource.com/c/tools/+/213458
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
@stamblerre
Copy link
Contributor

@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 golang.org/x/tools/gopls@v0.3.0-pre3).

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

@gopherbot
Copy link

Change https://golang.org/cl/202277 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
@stamblerre
Copy link
Contributor

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.

@golang golang locked and limited conversation to collaborators Feb 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge 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

3 participants