Debian Codegraph @

gopls/internal/mod/references.go

raw symbols SCIP
1 // Copyright 2025 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package mod
6
7 import (
8 "context"
9 "fmt"
10 "maps"
11 "slices"
12
13 "golang.org/x/tools/gopls/internal/cache"
14 "golang.org/x/tools/gopls/internal/cache/metadata"
15 "golang.org/x/tools/gopls/internal/cache/parsego"
16 "golang.org/x/tools/gopls/internal/file"
17 "golang.org/x/tools/gopls/internal/protocol"
18 )
19
20 // References returns the locations of imports referenced by a required module
21 func References(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, params *protocol.ReferenceParams) ([]protocol.Location, error) {
22 // Th go.mod file will contain a module name (e.g., golang.org/x/tools)
23 // but the import statements contain package names
24 // (e.g., golang.org/x/tools/internal/astutil).
25 // The code has to avoid imports from submodules, like golang.org/x/tools/gopls/internal/file
26 // so simple string matching would not work.
27
28 // find modpath, the module path the user wants the references for
29 gomod, err := snapshot.ParseMod(ctx, fh)
30 if err != nil {
31 return nil, err
32 }
33 if gomod.File == nil {
34 return nil, fmt.Errorf("mysterious parse failure %s", fh.URI().Path())
35 }
36 // protocol.ReferenceParams contains both a Position and a Range.
37 // The Range would contain the Position, so use Position.
38 offset, err := gomod.Mapper.PositionOffset(params.Position)
39 if err != nil {
40 return nil, err
41 }
42 var modpath string
43 for _, req := range gomod.File.Require {
44 if req.Syntax.Start.Byte <= offset && offset <= req.Syntax.End.Byte {
45 modpath = req.Mod.Path
46 break
47 }
48 }
49 if modpath == "" {
50 // nothing to report
51 return nil, nil
52 }
53
54 locs := make(map[protocol.Location]bool)
55
56 includeDecl := params.Context.IncludeDeclaration
57 // A. find the IDs of all packages in the module that was required
58 ids := make(map[metadata.PackageID]bool)
59 g := snapshot.MetadataGraph()
60 for _, pkg := range g.Packages {
61 if pkg.Module != nil && pkg.Module.Path == modpath {
62 ids[pkg.ID] = true
63 // once, if the definition is needed
64 if includeDecl {
65 fh, err := snapshot.ReadFile(ctx, protocol.URIFromPath(pkg.Module.GoMod))
66 if err != nil {
67 return nil, err
68 }
69 parsed, err := snapshot.ParseMod(ctx, fh)
70 if err != nil {
71 return nil, err
72 }
73 if parsed.File != nil && parsed.File.Module != nil {
74 start, end := parsed.File.Module.Syntax.Span()
75 loc, err := parsed.Mapper.OffsetLocation(start.Byte, end.Byte)
76 if err != nil {
77 return nil, err
78 }
79 locs[loc] = true
80 includeDecl = false // no need to do this again
81 }
82 }
83 }
84 }
85 // B. find all the importers of these packages that are in the go.mod's module
86 uris := make(map[protocol.DocumentURI]*metadata.Package)
87 for id := range ids {
88 for _, importer := range g.ImportedBy[id] {
89 if importer.Module.GoMod != gomod.URI.Path() {
90 continue
91 }
92 // files to be read.
93 // at least one of them has an import to be reported.
94 for _, uri := range importer.CompiledGoFiles {
95 upkgs := g.ForFile[uri]
96 for _, upkg := range upkgs {
97 if upkg.ID == importer.ID {
98 uris[uri] = importer // may overwrite, but it doesn't matter
99 break
100 }
101 }
102 }
103 }
104 }
105 // C. for each of the uris, pick out the locs of imports from the requested module
106 for uri := range uris {
107 fh, err := snapshot.ReadFile(ctx, uri)
108 if err != nil {
109 return nil, err
110 }
111 pgf, err := snapshot.ParseGo(ctx, fh, parsego.Header) // Header promotes cache hits
112 if err != nil {
113 return nil, err
114 }
115 xpkg := uris[uri]
116 for _, spec := range pgf.File.Imports {
117 importPath := metadata.UnquoteImportPath(spec)
118 if ids[xpkg.DepsByImpPath[importPath]] {
119 loc, err := pgf.NodeLocation(spec.Path)
120 if err != nil {
121 return nil, err
122 }
123 locs[loc] = true
124 continue
125 }
126 }
127 }
128
129 ans := slices.SortedFunc(maps.Keys(locs), protocol.CompareLocation)
130 return ans, nil
131 }