Skip to content

x/tools/cmd/goimports: should prefer higher major versions #46038

@j2gg0s

Description

@j2gg0s

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

$ go version
go version go1.15.6 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

For one module, if v1 and v2 all exists in $GOPATH/pkg/mod, goimports will used v1 first.

One Example:
If grpc-gateway and grpc-gateway/v2 all exists, most of people really need is grpc-gateway/v2.

➜  imports cat main.go
package main

func main() {
        var _ = runtime.ServeMux
}
➜  imports goimports -d main.go
diff -u main.go.orig main.go
--- main.go.orig        2021-05-07 21:19:41.000000000 +0800
+++ main.go     2021-05-07 21:19:41.000000000 +0800
@@ -1,5 +1,7 @@
 package main

+import "github.com/grpc-ecosystem/grpc-gateway/runtime"
+
 func main() {
        var _ = runtime.ServeMux
 }
➜  imports ls $GOPATH/pkg/mod/github.com/grpc-ecosystem/grpc-gateway
v2@v2.0.1 v2@v2.3.0 v2@v2.4.0
➜  imports ls $GOPATH/pkg/mod/github.com/grpc-ecosystem/
go-grpc-middleware                                      grpc-gateway                                            grpc-gateway@v1.15.0
go-grpc-middleware@v1.0.0                               grpc-gateway@v1.12.1                                    grpc-gateway@v1.16.0
go-grpc-middleware@v1.0.1-0.20190118093823-f849b5445de4 grpc-gateway@v1.13.0                                    grpc-gateway@v1.9.0
go-grpc-middleware@v1.2.0                               grpc-gateway@v1.14.3                                    grpc-gateway@v1.9.5
go-grpc-middleware@v1.2.2                               grpc-gateway@v1.14.5                                    grpc-health-probe@v0.3.6
go-grpc-prometheus@v1.2.0                               grpc-gateway@v1.14.6                                    grpc-opentracing@v0.0.0-20180507213350-8e809c8a8645

What did you expect to see?

v2 is used first.
Maybe we should change byDistanceOrImportPathShortLength

What did you see instead?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions