Skip to content

Commit bf9e2a8

Browse files
committed
gopls/internal: test fixes for some imports bugs
The CL has tests for two fixed bugs. For the new imports to work the metadata graph has to be current, which in this CL, is accomplished with a call to snapshot.WordspaceMetadata which may wait for changes to be assimilated. Fixes: golang.go/go#44510 Fixes: golang.go/go#67973 Change-Id: Ieb5a9361a75796a172da953cc58853d38f596ebd Reviewed-on: https://go-review.googlesource.com/c/tools/+/649315 Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 851c747 commit bf9e2a8

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

gopls/internal/cache/source.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func (s *goplsSource) resolveWorkspaceReferences(filename string, missing import
212212
// keep track of used syms and found results by package name
213213
// TODO: avoid import cycles (is current package in forward closure)
214214
founds := make(map[string][]found)
215-
for i := 0; i < len(ids); i++ {
215+
for i := range len(ids) {
216216
nm := string(pkgs[i].Name)
217217
if satisfies(syms[i], missing[nm]) {
218218
got := &imports.Result{

gopls/internal/golang/format.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ func computeImportEdits(ctx context.Context, pgf *parsego.File, snapshot *cache.
152152
case settings.ImportsSourceGoimports:
153153
source = isource
154154
}
155+
// imports require a current metadata graph
156+
// TODO(rfindlay) improve the API
157+
snapshot.WorkspaceMetadata(ctx)
155158
allFixes, err := imports.FixImports(ctx, filename, pgf.Src, goroot, options.Env.Logf, source)
156159
if err != nil {
157160
return nil, nil, err

gopls/internal/test/integration/misc/imports_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,31 @@ return nil
401401
}
402402
})
403403
}
404+
405+
// use the import from a different package in the same module
406+
func Test44510(t *testing.T) {
407+
const files = `-- go.mod --
408+
module test
409+
go 1.19
410+
-- foo/foo.go --
411+
package main
412+
import strs "strings"
413+
var _ = strs.Count
414+
-- bar/bar.go --
415+
package main
416+
var _ = strs.Builder
417+
`
418+
WithOptions(
419+
WriteGoSum("."),
420+
).Run(t, files, func(T *testing.T, env *Env) {
421+
env.OpenFile("bar/bar.go")
422+
env.SaveBuffer("bar/bar.go")
423+
buf := env.BufferText("bar/bar.go")
424+
if !strings.Contains(buf, "strs") {
425+
t.Error(buf)
426+
}
427+
})
428+
}
404429
func TestRelativeReplace(t *testing.T) {
405430
const files = `
406431
-- go.mod --
@@ -688,3 +713,33 @@ func Test() {
688713
}
689714
})
690715
}
716+
717+
// this test replaces 'package bar' with 'package foo'
718+
// saves the file, and then looks for the import in the main package.s
719+
func Test67973(t *testing.T) {
720+
const files = `-- go.mod --
721+
module hello
722+
go 1.19
723+
-- hello.go --
724+
package main
725+
var _ = foo.Bar
726+
-- internal/foo/foo.go --
727+
package bar
728+
func Bar() {}
729+
`
730+
WithOptions(
731+
Settings{"importsSource": settings.ImportsSourceGopls},
732+
).Run(t, files, func(t *testing.T, env *Env) {
733+
env.OpenFile("hello.go")
734+
env.AfterChange(env.DoneWithOpen())
735+
env.SaveBuffer("hello.go")
736+
env.OpenFile("internal/foo/foo.go")
737+
env.RegexpReplace("internal/foo/foo.go", "bar", "foo")
738+
env.SaveBuffer("internal/foo/foo.go")
739+
env.SaveBuffer("hello.go")
740+
buf := env.BufferText("hello.go")
741+
if !strings.Contains(buf, "internal/foo") {
742+
t.Errorf(`expected import "hello/internal/foo" but got %q`, buf)
743+
}
744+
})
745+
}

0 commit comments

Comments
 (0)