Skip to content

Commit

Permalink
gopls/internal/regtest: fix goimports on windows when using vendoring
Browse files Browse the repository at this point in the history
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 <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
findleyr committed Jun 15, 2023
1 parent 41e4e56 commit 7261b32
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
38 changes: 38 additions & 0 deletions gopls/internal/regtest/misc/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,41 @@ func _() {
)
})
}

func TestWindowsVendoring_Issue56291(t *testing.T) {
const src = `
-- go.mod --
module mod.com
go 1.14
require golang.org/x/hello v1.2.3
-- go.sum --
golang.org/x/hello v1.2.3 h1:EcMp5gSkIhaTkPXp8/3+VH+IFqTpk3ZbpOhqk0Ncmho=
golang.org/x/hello v1.2.3/go.mod h1:WW7ER2MRNXWA6c8/4bDIek4Hc/+DofTrMaQQitGXcco=
-- main.go --
package main
import "golang.org/x/hello/hi"
func main() {
_ = hi.Goodbye
}
`
WithOptions(
Modes(Default),
ProxyFiles(basicProxy),
).Run(t, src, func(t *testing.T, env *Env) {
env.OpenFile("main.go")
env.AfterChange(NoDiagnostics())
env.RunGoCommand("mod", "tidy")
env.RunGoCommand("mod", "vendor")
env.AfterChange(NoDiagnostics())
env.RegexpReplace("main.go", `import "golang.org/x/hello/hi"`, "")
env.AfterChange(
Diagnostics(env.AtRegexp("main.go", "hi.Goodbye")),
)
env.SaveBuffer("main.go")
env.AfterChange(NoDiagnostics())
})
}
8 changes: 6 additions & 2 deletions internal/imports/mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type ModuleResolver struct {
mains []*gocommand.ModuleJSON
mainByDir map[string]*gocommand.ModuleJSON
modsByModPath []*gocommand.ModuleJSON // All modules, ordered by # of path components in module Path...
modsByDir []*gocommand.ModuleJSON // ...or Dir.
modsByDir []*gocommand.ModuleJSON // ...or number of path components in their Dir.

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

0 comments on commit 7261b32

Please sign in to comment.