Skip to content

x/tools/imports: package in v2 module listed in go.mod is resolved to non-listed v1 #58382

@dolmen

Description

@dolmen

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/[email protected]
  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.

Metadata

Metadata

Assignees

Labels

NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolsThis label describes issues relating to any tools in the x/tools repository.gopls/importsmodules

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions