Skip to content

Commit 7261b32

Browse files
committed
gopls/internal/regtest: fix goimports on windows when using vendoring
Add a test for goimports when using mod vendoring on windows, along with a very subtle one-line fix. Fixes golang/go#56291 Change-Id: I2e45f70fc6dfa32164d4664acad886ec811474b8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/498695 Run-TryBot: Robert Findley <[email protected]> Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent 41e4e56 commit 7261b32

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

gopls/internal/regtest/misc/vendor_test.go

+38
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,41 @@ func _() {
6363
)
6464
})
6565
}
66+
67+
func TestWindowsVendoring_Issue56291(t *testing.T) {
68+
const src = `
69+
-- go.mod --
70+
module mod.com
71+
72+
go 1.14
73+
74+
require golang.org/x/hello v1.2.3
75+
-- go.sum --
76+
golang.org/x/hello v1.2.3 h1:EcMp5gSkIhaTkPXp8/3+VH+IFqTpk3ZbpOhqk0Ncmho=
77+
golang.org/x/hello v1.2.3/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco=
78+
-- main.go --
79+
package main
80+
81+
import "golang.org/x/hello/hi"
82+
83+
func main() {
84+
_ = hi.Goodbye
85+
}
86+
`
87+
WithOptions(
88+
Modes(Default),
89+
ProxyFiles(basicProxy),
90+
).Run(t, src, func(t *testing.T, env *Env) {
91+
env.OpenFile("main.go")
92+
env.AfterChange(NoDiagnostics())
93+
env.RunGoCommand("mod", "tidy")
94+
env.RunGoCommand("mod", "vendor")
95+
env.AfterChange(NoDiagnostics())
96+
env.RegexpReplace("main.go", `import "golang.org/x/hello/hi"`, "")
97+
env.AfterChange(
98+
Diagnostics(env.AtRegexp("main.go", "hi.Goodbye")),
99+
)
100+
env.SaveBuffer("main.go")
101+
env.AfterChange(NoDiagnostics())
102+
})
103+
}

internal/imports/mod.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type ModuleResolver struct {
3838
mains []*gocommand.ModuleJSON
3939
mainByDir map[string]*gocommand.ModuleJSON
4040
modsByModPath []*gocommand.ModuleJSON // All modules, ordered by # of path components in module Path...
41-
modsByDir []*gocommand.ModuleJSON // ...or Dir.
41+
modsByDir []*gocommand.ModuleJSON // ...or number of path components in their Dir.
4242

4343
// moduleCacheCache stores information about the module cache.
4444
moduleCacheCache *dirInfoCache
@@ -124,7 +124,7 @@ func (r *ModuleResolver) init() error {
124124
})
125125
sort.Slice(r.modsByDir, func(i, j int) bool {
126126
count := func(x int) int {
127-
return strings.Count(r.modsByDir[x].Dir, "/")
127+
return strings.Count(r.modsByDir[x].Dir, string(filepath.Separator))
128128
}
129129
return count(j) < count(i) // descending order
130130
})
@@ -328,6 +328,10 @@ func (r *ModuleResolver) findModuleByDir(dir string) *gocommand.ModuleJSON {
328328
// - in /vendor/ in -mod=vendor mode.
329329
// - nested module? Dunno.
330330
// Rumor has it that replace targets cannot contain other replace targets.
331+
//
332+
// Note that it is critical here that modsByDir is sorted to have deeper dirs
333+
// first. This ensures that findModuleByDir finds the innermost module.
334+
// See also golang/go#56291.
331335
for _, m := range r.modsByDir {
332336
if !strings.HasPrefix(dir, m.Dir) {
333337
continue

0 commit comments

Comments
 (0)