Closed shabbyrobe closed 2 years ago
Duplicate of #41800
Just in case someone else comes across this who is looking for a solution, I have a hacky workaround that seems to be working so far.
Clone https://github.com/golang/tools, apply the following patch, then go install ./cmd/goimports && go install ./gopls
. YMMV, but this is working OK for me at the moment. I will update the patch if I find issues with it.
diff --git a/internal/imports/fix.go b/internal/imports/fix.go
index 9e373d64e..049811e89 100644
--- a/internal/imports/fix.go
+++ b/internal/imports/fix.go
@@ -18,6 +18,7 @@ import (
"path"
"path/filepath"
"reflect"
+ "regexp"
"sort"
"strconv"
"strings"
@@ -1047,7 +1048,14 @@ func addExternalCandidates(pass *pass, refs references, filename string) error {
}
mu.Lock()
defer mu.Unlock()
- found[pkg.packageName] = append(found[pkg.packageName], pkgDistance{pkg, distance(pass.srcDir, pkg.dir)})
+ importPathShortSansVersion, version, hasVersion := extractVersion(pkg.importPathShort)
+ found[pkg.packageName] = append(found[pkg.packageName], pkgDistance{
+ pkg: pkg,
+ distance: distance(pass.srcDir, pkg.dir),
+ importPathShortSansVersion: importPathShortSansVersion,
+ version: version,
+ hasVersion: hasVersion,
+ })
return false // We'll do our own loading after we sort.
},
}
@@ -1271,8 +1279,11 @@ type pkg struct {
}
type pkgDistance struct {
- pkg *pkg
- distance int // relative distance to target
+ pkg *pkg
+ distance int // relative distance to target
+ importPathShortSansVersion string
+ version int
+ hasVersion bool
}
// byDistanceOrImportPathShortLength sorts by relative distance breaking ties
@@ -1281,6 +1292,16 @@ type byDistanceOrImportPathShortLength []pkgDistance
func (s byDistanceOrImportPathShortLength) Len() int { return len(s) }
func (s byDistanceOrImportPathShortLength) Less(i, j int) bool {
+ if s[i].importPathShortSansVersion == s[j].importPathShortSansVersion {
+ if s[i].hasVersion && s[j].hasVersion {
+ return s[i].version > s[j].version
+ } else if s[i].hasVersion {
+ return true
+ } else if s[j].hasVersion {
+ return false
+ }
+ }
+
di, dj := s[i].distance, s[j].distance
if di == -1 {
return false
@@ -1298,8 +1319,22 @@ func (s byDistanceOrImportPathShortLength) Less(i, j int) bool {
}
return vi < vj
}
+
func (s byDistanceOrImportPathShortLength) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
+var extractVersionPattern = regexp.MustCompile(`^(.*)/v([0-9]+)(/.*)?$`)
+
+func extractVersion(importPathShort string) (importPathShortSansVersion string, version int, found bool) {
+ matches := extractVersionPattern.FindAllStringSubmatch(importPathShort, 2)
+ if len(matches) != 1 {
+ return importPathShort, 0, false
+ }
+ match := matches[0]
+ version, _ = strconv.Atoi(match[2])
+ importPathShortSansVersion = match[1] + match[3]
+ return importPathShortSansVersion, version, true
+}
+
func distance(basepath, targetpath string) int {
p, err := filepath.Rel(basepath, targetpath)
if err != nil {
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes, latest 1.18 and 1.19rc1 are the same.
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
I also completely cleared out my
$GOPATH/pkg
directory and the issue reproduced.What did you expect to see?
What did you see instead?
This persists even if other source files in the module already contain imports for the correct v45 version, and after the package has been successfully built.
Full
goimports -v
Output