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/imports: package in v2 module listed in go.mod is resolved to non-listed v1 #58382

Open
dolmen opened this issue Feb 7, 2023 · 8 comments
Assignees
Labels
gopls/imports modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@dolmen
Copy link
Contributor

dolmen commented Feb 7, 2023

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

$ go version
1.20
$ go version -m $(which goimports)
/home/dolmen/go/bin/goimports: go1.20
	path	golang.org/x/tools/cmd/goimports
	mod	golang.org/x/tools	v0.5.0	h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
	dep	golang.org/x/mod	v0.7.0	h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
	dep	golang.org/x/sys	v0.4.0	h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
	build	-buildmode=exe
	build	-compiler=gc
	build	CGO_ENABLED=1
	build	CGO_CFLAGS=
	build	CGO_CPPFLAGS=
	build	CGO_CXXFLAGS=
	build	CGO_LDFLAGS=
	build	GOARCH=amd64
	build	GOOS=darwin
	build	GOAMD64=v3

Does this issue reproduce with the latest release?

yes

Issue is reproducible with the latest commit from the repository:

$ go install golang.org/x/tools/cmd/goimports@master
go: downloading golang.org/x/tools v0.5.1-0.20230207145906-edddc5fc3223

What did you do?

  1. Create main.go without incomplete imports. xurls symbol is not linked to any declared imports.
  2. Create go.mod to list dependencies. One module has major version 2: mvdan.cc/xurls/v2@v2.4.0
  3. Run goimports to fix imports
$ cat main.go
package main

// Some imports are missing.
// I expect goimports to resolve them using modules listed in go.mod.
import "os"

func main() {
	os.Args[1] = os.Args[0]
	os.Args = os.Args[1:]
//line :1
	fmt.Println(xurls.Relaxed().FindAllString(os.Args[1], -1))
}
$ cat go.mod
module demo

go 1.20

require mvdan.cc/xurls/v2 v2.4.0
$ cat go.sum
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
mvdan.cc/xurls/v2 v2.4.0 h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc=
mvdan.cc/xurls/v2 v2.4.0/go.mod h1:+GEjq9uNjqs8LQfM9nVnM8rff0OQ5Iash5rzX+N1CSg=
$ go install golang.org/x/tools/cmd/goimports@master
go: downloading golang.org/x/tools v0.5.1-0.20230207145906-edddc5fc3223
$ goimports main.go
package main

// Some imports are missing.
// I expect goimports to resolve them using modules listed in go.mod.
import (
	"fmt"
	"os"

	"mvdan.cc/xurls"
)

func main() {
	os.Args[1] = os.Args[0]
	os.Args = os.Args[1:]
//line :1
	fmt.Println(xurls.Relaxed().FindAllString(os.Args[1], -1))
}

What did you expect to see?

Imports fixed in main.go. In particular an import of mvdan.cc/xurls/v2.

import (
	"fmt"
	"os"

	"mvdan.cc/xurls/v2"
)

What did you see instead?

I get an import of mvdan.cc/xurls (which is v1 and a different API) instead of mvdan.cc/xurls/v2.

import (
	"fmt"
	"os"

	"mvdan.cc/xurls"
)

As a result the code modified by the goimports doesn't compile.

@gopherbot gopherbot added the Tools This label describes issues relating to any tools in the x/tools repository. label Feb 7, 2023
@gopherbot gopherbot added this to the Unreleased milestone Feb 7, 2023
@bcmills
Copy link
Contributor

bcmills commented Feb 7, 2023

See also #41800.

@bcmills bcmills added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. modules labels Feb 7, 2023
@dolmen
Copy link
Contributor Author

dolmen commented Mar 16, 2023

I agree that #41800 is the same issue. At least here I'm providing a process that is more easily reproducible.

@pjweinb
Copy link

pjweinb commented Apr 16, 2024

Thank you for the simple example. Imports is getting the wrong version even with the v2 version both in the module cache and being required in go.mod.

@findleyr findleyr removed their assignment Apr 16, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/582557 mentions this issue: internal/imports: recognize major versions and use go.mod

@Oxygels
Copy link

Oxygels commented Nov 22, 2024

Hello! Any news for this issue?

@findleyr
Copy link
Member

@Oxygels yes. We understand this bug, and are working on a holistic rewrite of goimports for performance and correctness. It will probably land in the coming months.

@feranwq
Copy link

feranwq commented Dec 26, 2024

is there any temporary workaround or solution that can be applied to mitigate the issue until the rewrite is implemented?

@DeedleFake
Copy link

As far as I'm aware, the only solution is to change the import that isn't working manually, @feranwq.

@findleyr findleyr modified the milestones: gopls/v0.18.0, gopls/v0.19.0 Feb 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gopls/imports modules NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

8 participants