-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: when working in vendor mode autoimport adds packages from vendor
dir
#56291
Comments
Thank you for the report. CC @adonovan, as this coincidentally seems very similar to https://go.dev/cl/443636: clearly a package path and import path are being confused here (though I don't necessarily think this will be fixed by that CL -- goimports is mostly disconnected from the rest of gopls). @inliquid are you by any chance working on an open-source project? If not, we can try to reproduce from scratch. |
@findleyr, unfortunately this is a private repo. |
Working on a private repo I've noticed the same. |
Change https://go.dev/cl/448377 mentions this issue: |
This change adds a FileSet field to Package, and uses it in preference to Snapshot.FileSet wherever that is appropriate: all but a handful of places. For now, they must continue to refer to the same instance, but once we do away with the Snapshot's cache of parsed files, there will be no need for a global FileSet and each Package will be able to create its own. (Some care will be required to make sure it is always clear which package owns each each token.Pos/ast.Node/types.Object when there are several in play.) Updates golang/go#56291 Change-Id: I017eb794ffb737550da6ae88462d23a8f5c1e1e7 Reviewed-on: https://go-review.googlesource.com/c/tools/+/448377 Run-TryBot: Alan Donovan <adonovan@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Robert Findley <rfindley@google.com>
Hi @inliquid, I've tried to reproduce this bug a number of ways but with no success. (For example, I vendored a number of packages, created a file with missing imports of packages among the vendored set, and ran 'gopls imports' on them, and each time it got the right answer. I tried various versions of gopls before recent changes in relevant logic. And I tried it without and without a go.work file. I also looked at the various places in our code that we've flagged as suspicious in the way in which they convert between import paths and package paths, and none of them looks like an obvious candidate to explain it.) I understand this is private code you can't share, but next time you encounter the problem, would you be willing to spend a few minutes trying to create a reproducible test case in terms of packages you can share? As soon as I can reproduce this I can work on a fix. Thanks. |
@adonovan I work with Just tried once again by adding If you have any idea what additional information I can provide please let me know. You are correct this is private repo so I can't share its code unfortunately. |
Still no luck reproducing this. The "implementations" issue you mention in #56291 (comment) is likely a dup of #56169, which should now be fixed. |
Well the problem is definitely still present. Except for sharing the project files, what else can I do to help in solving it? |
I, and several of my coworkers, are also experiencing this issue. I hacked together a shell script that seems to reliably reproduce it (tested in git-bash and cygwin). Requires gopls to be installed as a command (I ran #!/bin/sh
# create a new dir to hold our package
mkdir vendorbug
cd vendorbug
# create a simple main.go that uses github.com/google/uuid
cat > main.go <<EOT
package main
import (
"fmt"
"github.com/google/uuid"
)
func main() {
fmt.Printf("This is a test package.")
myUUID, _ := uuid.NewRandom()
fmt.Printf("The UUID generated was: %s", myUUID)
}
EOT
# create a module
go mod init github.com/bferullo/vendorbug
go mod tidy
go mod vendor
# remove github.com/google/uuid import from main.go
sed --in-place -E '/github.com\/google\/uuid/d' ./main.go
# (verify here that main.go no longer has that import, if you want)
# run gopls imports
gopls imports -d main.go
# EXPECTED RESULT: "github.com/google/uuid" reappears in import block
# OBSERVED RESULT: "github.com/bferullo/vendorbug/vendor/github.com/google/uuid" appears in import block |
Huh, FWIW that script didn't reproduce the bug for me. I completely believe this bug exists, but it must depend on state (likely the state of the module cache). |
Same here. github.com/google/uuid appears each time. I tried with GOMODCACHE=$(mktemp -d) too. Could you @bferullo try with that env var and report whether it still happens? |
It does still repro. I also imagine the issue has to do with some persistent state, but I can't figure it out either -- I even installed go, gopls, and gitbash on a machine that previously had none of those, and was able to repro it there as well. |
can you give the exact versions of |
$ go version
go version go1.19.4 windows/amd64
$ gopls version
golang.org/x/tools/gopls v0.11.0
golang.org/x/tools/gopls@v0.11.0 h1:/nvKHdTtePQmrv9XN3gIUN9MOdUrKzO/dcqgbG6x8EY=
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\bferu\AppData\Local\go-build
set GOENV=C:\Users\bferu\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\bferu\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\bferu\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.19.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
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 -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=C:\Users\bferu\AppData\Local\Temp\go-build375371210=/tmp/go-build -gno-record-gcc-switches |
I also can reproduce the issue with the above script. Since I also run it on Windows, can OS be a problem? |
Anecdotally, some colleagues who run OSX also encounter this issue. I'll see if I can get them to test the script specifically. |
Still no luck reproducing this on macOS using go1.19 (or go1.19.5) and the script and environment above, and either gopls@v0.11.0 or today's gopls@latest. |
I still can reproduce this with the script from above comments on a brand new Windows 11 machine (old one was Windows 10): import (
"fmt"
+ "github.com/bferullo/vendorbug/vendor/github.com/google/uuid"
) PS: I'm using gopls@master with latest commits. |
Change https://go.dev/cl/498695 mentions this issue: |
The test in the above CL (based on the script provided by @bferullo reproduces the failure on our windows trybot. I'll fix this for gopls@v0.12.1 (which we expect not long after v0.12.0...) |
gopls version
go env
What did you do?
When working in vendor module mode, and file does not contain imports yet, press
Ctrl-S
to force format and auto import actions.What did you expect to see?
Imports added with correct package names, for example:
google.golang.org/grpc
What did you see instead?
Packages added from
vendor
dir, for example:"path/to/my/module/vendor/google.golang.org/grpc"
The text was updated successfully, but these errors were encountered: