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

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

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

Wingboy-Jim2 opened this issue Jan 6, 2020 · 5 comments
Labels
Milestone

Comments

@Wingboy-Jim2
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 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 label Jan 6, 2020
@gopherbot

This comment has been minimized.

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

This comment has been minimized.

Copy link
Contributor

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

@Wingboy-Jim2

This comment has been minimized.

Copy link
Author

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

This comment has been minimized.

Copy link
Contributor

@stamblerre stamblerre commented Jan 6, 2020

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

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Jan 6, 2020

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.