diff --git a/internal/lsp/source/implementation.go b/internal/lsp/source/implementation.go index 58ae3e1f92e..29da25f874e 100644 --- a/internal/lsp/source/implementation.go +++ b/internal/lsp/source/implementation.go @@ -247,11 +247,18 @@ func qualifiedObjsAtProtocolPos(ctx context.Context, s Snapshot, fh FileHandle, } objs = append(objs, obj) } + // Get all of the transitive dependencies of the search package. pkgs := make(map[*types.Package]Package) - pkgs[searchpkg.GetTypes()] = searchpkg - for _, imp := range searchpkg.Imports() { - pkgs[imp.GetTypes()] = imp + var addPkg func(pkg Package) + addPkg = func(pkg Package) { + pkgs[pkg.GetTypes()] = pkg + for _, imp := range pkg.Imports() { + if _, ok := pkgs[imp.GetTypes()]; !ok { + addPkg(imp) + } + } } + addPkg(searchpkg) for _, obj := range objs { if obj.Parent() == types.Universe { return nil, fmt.Errorf("%w %q", errBuiltin, obj.Name()) diff --git a/internal/lsp/testdata/lsp/primarymod/references/another/another.go b/internal/lsp/testdata/lsp/primarymod/references/another/another.go new file mode 100644 index 00000000000..de2ea16f829 --- /dev/null +++ b/internal/lsp/testdata/lsp/primarymod/references/another/another.go @@ -0,0 +1,13 @@ +// Package another has another type. +package another + +import ( + other "golang.org/x/tools/internal/lsp/references/other" +) + +func _() { + xes := other.GetXes() + for _, x := range xes { + _ = x.Y //@mark(anotherXY, "Y"),refs("Y", typeXY, anotherXY, GetXesY) + } +} diff --git a/internal/lsp/testdata/lsp/primarymod/references/other/other.go b/internal/lsp/testdata/lsp/primarymod/references/other/other.go index 3987d358cee..de35cc81a9e 100644 --- a/internal/lsp/testdata/lsp/primarymod/references/other/other.go +++ b/internal/lsp/testdata/lsp/primarymod/references/other/other.go @@ -4,6 +4,14 @@ import ( references "golang.org/x/tools/internal/lsp/references" ) +func GetXes() []references.X { + return []references.X{ + { + Y: 1, //@mark(GetXesY, "Y"),refs("Y", typeXY, GetXesY, anotherXY) + }, + } +} + func _() { references.Q = "hello" //@mark(assignExpQ, "Q") bob := func(_ string) {} diff --git a/internal/lsp/testdata/lsp/primarymod/references/refs.go b/internal/lsp/testdata/lsp/primarymod/references/refs.go index 6ce4afc6a8e..933a36f54e9 100644 --- a/internal/lsp/testdata/lsp/primarymod/references/refs.go +++ b/internal/lsp/testdata/lsp/primarymod/references/refs.go @@ -3,6 +3,10 @@ package refs type i int //@mark(typeI, "i"),refs("i", typeI, argI, returnI, embeddedI) +type X struct { + Y int //@mark(typeXY, "Y") +} + func _(_ i) []bool { //@mark(argI, "i") return nil } diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden index bd3db882212..940567ee71f 100644 --- a/internal/lsp/testdata/lsp/summary.txt.golden +++ b/internal/lsp/testdata/lsp/summary.txt.golden @@ -15,7 +15,7 @@ SuggestedFixCount = 6 DefinitionsCount = 45 TypeDefinitionsCount = 2 HighlightsCount = 52 -ReferencesCount = 9 +ReferencesCount = 11 RenamesCount = 24 PrepareRenamesCount = 7 SymbolsCount = 3