| 1 | // Copyright 2013 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 importsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ |
| 6 | |
| 7 | import ( |
| 8 | "bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/" |
| 9 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 10 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/" |
| 11 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 12 | "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/" |
| 13 | "go/buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/" |
| 14 | "go/parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/" |
| 15 | "go/tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/" |
| 16 | "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/" |
| 17 | "io/fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/" |
| 18 | "io/ioutilscip-go gomod github.com/golang/go/src go1.25.0 `io/ioutil`/" |
| 19 | "mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/" |
| 20 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 21 | "pathscip-go gomod github.com/golang/go/src go1.25.0 path/" |
| 22 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 23 | "reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/" |
| 24 | "sortscip-go gomod github.com/golang/go/src go1.25.0 sort/" |
| 25 | "strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/" |
| 26 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 27 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 28 | "unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/" |
| 29 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/" |
| 30 | |
| 31 | "golang.org/x/tools/go/ast/astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/" |
| 32 | "golang.org/x/tools/internal/eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/" |
| 33 | "golang.org/x/tools/internal/gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/" |
| 34 | "golang.org/x/tools/internal/gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/" |
| 35 | "golang.org/x/tools/internal/stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/" |
| 36 | ) |
| 37 | |
| 38 | // importToGroup is a list of functions which map from an import path to |
| 39 | // a group number. |
| 40 | var importToGroupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup. = []func(localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:localPrefix., importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath. string) (numscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:num. int, okscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:ok. bool){ |
| 41 | func(localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:localPrefix., importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath. string) (numscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:num. int, okscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:ok. bool) { |
| 42 | if localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:localPrefix. == "" { |
| 43 | return |
| 44 | } |
| 45 | for p := range stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.SplitSeqscip-go gomod github.com/golang/go/src go1.25.0 strings/SplitSeq().(localPrefixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:localPrefix., ",") { |
| 46 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath., p) || stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimSuffix().(p, "/") == importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath. { |
| 47 | return 3, true |
| 48 | } |
| 49 | } |
| 50 | return |
| 51 | }, |
| 52 | func(_, importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath. string) (numscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:num. int, okscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:ok. bool) { |
| 53 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath., "appengine") { |
| 54 | return 2, true |
| 55 | } |
| 56 | return |
| 57 | }, |
| 58 | func(_, importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath. string) (numscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:num. int, okscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:ok. bool) { |
| 59 | firstComponent := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.25.0 strings/Split().(importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup:importPath., "/")[0] |
| 60 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(firstComponent, ".") { |
| 61 | return 1, true |
| 62 | } |
| 63 | return |
| 64 | }, |
| 65 | } |
| 66 | |
| 67 | func importGroupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importGroup().(localPrefix, importPath string) int { |
| 68 | for _, fn := range importToGroupscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importToGroup. { |
| 69 | if n, ok := fn(localPrefix, importPath); ok { |
| 70 | return n |
| 71 | } |
| 72 | } |
| 73 | return 0 |
| 74 | } |
| 75 | |
| 76 | type ImportFixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFixType# int |
| 77 | |
| 78 | const ( |
| 79 | AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/AddImport. ImportFixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFixType# = iota |
| 80 | DeleteImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DeleteImport. |
| 81 | SetImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/SetImportName. |
| 82 | ) |
| 83 | |
| 84 | type ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix# struct { |
| 85 | // StmtInfo represents the import statement this fix will add, remove, or change. |
| 86 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo. ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# |
| 87 | // IdentName is the identifier that this fix will add or remove. |
| 88 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName. string |
| 89 | // FixType is the type of fix this is (AddImport, DeleteImport, SetImportName). |
| 90 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType. ImportFixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFixType# |
| 91 | Relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#Relevance. float64 // see pkg |
| 92 | } |
| 93 | |
| 94 | // parseOtherFiles parses all the Go files in srcDir except filename, including |
| 95 | // test files if filename looks like a test. |
| 96 | // |
| 97 | // It returns an error only if ctx is cancelled. Files with parse errors are |
| 98 | // ignored. |
| 99 | func parseOtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/parseOtherFiles().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, srcDir, filename string) ([]*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, error) { |
| 100 | // This could use go/packages but it doesn't buy much, and it fails |
| 101 | // with https://golang.org/issue/26296 in LoadFiles mode in some cases. |
| 102 | considerTests := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(filename, "_test.go") |
| 103 | |
| 104 | fileBase := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Basescip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Base().(filename) |
| 105 | packageFileInfos, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 os/ReadDir().(srcDir) |
| 106 | if err != nil { |
| 107 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() |
| 108 | } |
| 109 | |
| 110 | var files []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File# |
| 111 | for _, fi := range packageFileInfos { |
| 112 | if ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() != nil { |
| 113 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() |
| 114 | } |
| 115 | if fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().() == fileBase || !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().(), ".go") { |
| 116 | continue |
| 117 | } |
| 118 | if !considerTests && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().(), "_test.go") { |
| 119 | continue |
| 120 | } |
| 121 | |
| 122 | f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(srcDir, fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()), nil, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.SkipObjectResolutionscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/SkipObjectResolution.) |
| 123 | if err != nil { |
| 124 | continue |
| 125 | } |
| 126 | |
| 127 | files = append(files, f) |
| 128 | } |
| 129 | |
| 130 | return files, ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() |
| 131 | } |
| 132 | |
| 133 | // addGlobals puts the names of package vars into the provided map. |
| 134 | func addGlobalsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addGlobals().(f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, globals map[string]bool) { |
| 135 | for _, decl := range f.Declsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Decls. { |
| 136 | genDecl, ok := decl.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.GenDeclscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#) |
| 137 | if !ok { |
| 138 | continue |
| 139 | } |
| 140 | |
| 141 | for _, spec := range genDecl.Specsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/GenDecl#Specs. { |
| 142 | valueSpec, ok := spec.(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ValueSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#) |
| 143 | if !ok { |
| 144 | continue |
| 145 | } |
| 146 | globals[valueSpec.Namesscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ValueSpec#Names.[0].Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.] = true |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // collectReferences builds a map of selector expressions, from |
| 152 | // left hand side (X) to a set of right hand sides (Sel). |
| 153 | func collectReferencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectReferences().(f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References# { |
| 154 | refs := Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References#{} |
| 155 | |
| 156 | var visitor visitFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/visitFn# |
| 157 | visitor = func(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Visitorscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Visitor# { |
| 158 | if node == nil { |
| 159 | return visitor |
| 160 | } |
| 161 | switch v := node.(type) { |
| 162 | case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.SelectorExprscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#: |
| 163 | xident, ok := v.Xscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#X..(*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#) |
| 164 | if !ok { |
| 165 | break |
| 166 | } |
| 167 | if xident.Objscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Obj. != nil { |
| 168 | // If the parser can resolve it, it's not a package ref. |
| 169 | break |
| 170 | } |
| 171 | if !astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IsExportedscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IsExported().(v.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) { |
| 172 | // Whatever this is, it's not exported from a package. |
| 173 | break |
| 174 | } |
| 175 | pkgName := xident.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 176 | r := refs[pkgName] |
| 177 | if r == nil { |
| 178 | r = make(map[string]bool) |
| 179 | refs[pkgName] = r |
| 180 | } |
| 181 | r[v.Selscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/SelectorExpr#Sel..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.] = true |
| 182 | } |
| 183 | return visitor |
| 184 | } |
| 185 | astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Walkscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Walk().(visitor, f) |
| 186 | return refs |
| 187 | } |
| 188 | |
| 189 | // collectImports returns all the imports in f. |
| 190 | // Unnamed imports (., _) and "C" are ignored. |
| 191 | func collectImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectImports().(f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#) []*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# { |
| 192 | var imports []*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# |
| 193 | for _, imp := range f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. { |
| 194 | var name string |
| 195 | if imp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. != nil { |
| 196 | name = imp.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 197 | } |
| 198 | if imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value. == `"C"` || name == "_" || name == "." { |
| 199 | continue |
| 200 | } |
| 201 | path := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.0 strings/Trim().(imp.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value., `"`) |
| 202 | imports = append(imports, &ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ |
| 203 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name.: name, |
| 204 | ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: path, |
| 205 | }) |
| 206 | } |
| 207 | return imports |
| 208 | } |
| 209 | |
| 210 | // findMissingImport searches pass's candidates for an import that provides |
| 211 | // pkg, containing all of syms. |
| 212 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) findMissingImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#findMissingImport().(pkg string, syms map[string]bool) *ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# { |
| 213 | for _, candidate := range p.candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates. { |
| 214 | pkgInfo, ok := p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages.[candidate.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.] |
| 215 | if !ok { |
| 216 | continue |
| 217 | } |
| 218 | if p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(candidate) != pkg { |
| 219 | continue |
| 220 | } |
| 221 | |
| 222 | allFound := true |
| 223 | for right := range syms { |
| 224 | if !pkgInfo.Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Exports.[right] { |
| 225 | allFound = false |
| 226 | break |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | if allFound { |
| 231 | return candidate |
| 232 | } |
| 233 | } |
| 234 | return nil |
| 235 | } |
| 236 | |
| 237 | // A pass contains all the inputs and state necessary to fix a file's imports. |
| 238 | // It can be modified in some ways during use; see comments below. |
| 239 | type passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass# struct { |
| 240 | // Inputs. These must be set before a call to load, and not modified after. |
| 241 | fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fset. *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet# // fset used to parse f and its siblings. |
| 242 | fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f. *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File# // the file being fixed. |
| 243 | srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#srcDir. string // the directory containing f. |
| 244 | logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf. func(string, ...any) |
| 245 | sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#source. Sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Source# // the environment to use for go commands, etc. |
| 246 | loadRealPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#loadRealPackageNames. bool // if true, load package names from disk rather than guessing them. |
| 247 | otherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#otherFiles. []*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File# // sibling files. |
| 248 | gorootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#goroot. string |
| 249 | |
| 250 | // Intermediate state, generated by load. |
| 251 | existingImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#existingImports. map[string][]*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# |
| 252 | allRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#allRefs. Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References# |
| 253 | missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs. Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References# |
| 254 | |
| 255 | // Inputs to fix. These can be augmented between successive fix calls. |
| 256 | lastTryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#lastTry. bool // indicates that this is the last call and fix should clean up as best it can. |
| 257 | candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates. []*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# // candidate imports in priority order. |
| 258 | knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages. map[string]*PackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo# // information about all known packages. |
| 259 | } |
| 260 | |
| 261 | // loadPackageNames saves the package names for everything referenced by imports. |
| 262 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) loadPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#loadPackageNames().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, imports []*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#) error { |
| 263 | if p.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf. != nil { |
| 264 | p.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf.("loading package names for %v packages", len(imports)) |
| 265 | defer func() { |
| 266 | p.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf.("done loading package names for %v packages", len(imports)) |
| 267 | }() |
| 268 | } |
| 269 | var unknown []string |
| 270 | for _, imp := range imports { |
| 271 | if _, ok := p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages.[imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.]; ok { |
| 272 | continue |
| 273 | } |
| 274 | unknown = append(unknown, imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.) |
| 275 | } |
| 276 | names, err := p.sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#source..LoadPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Source#LoadPackageNames.(ctx, p.srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#srcDir., unknown) |
| 277 | if err != nil { |
| 278 | return err |
| 279 | } |
| 280 | |
| 281 | // TODO(rfindley): revisit this. Why do we need to store known packages with |
| 282 | // no exports? The inconsistent data is confusing. |
| 283 | for path, name := range names { |
| 284 | p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages.[path] = &PackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#{ |
| 285 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name.: name, |
| 286 | Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Exports.: map[string]bool{}, |
| 287 | } |
| 288 | } |
| 289 | return nil |
| 290 | } |
| 291 | |
| 292 | // WithoutVersion removes a trailing major version, if there is one. |
| 293 | func WithoutVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/WithoutVersion().(nm string) string { |
| 294 | if v := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(nm); len(v) > 0 && v[0] == 'v' { |
| 295 | if _, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(v[1:]); err == nil { |
| 296 | // this is, for instance, called with rand/v2 and returns rand |
| 297 | if len(v) < len(nm) { |
| 298 | xnm := nm[:len(nm)-len(v)-1] |
| 299 | return pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(xnm) |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | return nm |
| 304 | } |
| 305 | |
| 306 | // importIdentifier returns the identifier that imp will introduce. It will |
| 307 | // guess if the package name has not been loaded, e.g. because the source |
| 308 | // is not available. |
| 309 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp *ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#) string { |
| 310 | if imp.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. != "" { |
| 311 | return imp.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. |
| 312 | } |
| 313 | known := p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages.[imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.] |
| 314 | if known != nil && known.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name. != "" { |
| 315 | return WithoutVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/WithoutVersion().(known.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name.) |
| 316 | } |
| 317 | return ImportPathToAssumedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportPathToAssumedName().(imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.) |
| 318 | } |
| 319 | |
| 320 | // load reads in everything necessary to run a pass, and reports whether the |
| 321 | // file already has all the imports it needs. It fills in p.missingRefs with the |
| 322 | // file's missing symbols, if any, or removes unused imports if not. |
| 323 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#load().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#) ([]*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#, bool) { |
| 324 | p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages. = map[string]*PackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#{} |
| 325 | p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs. = Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References#{} |
| 326 | p.existingImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#existingImports. = map[string][]*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{} |
| 327 | |
| 328 | // Load basic information about the file in question. |
| 329 | p.allRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#allRefs. = collectReferencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectReferences().(p.fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f.) |
| 330 | |
| 331 | // Load stuff from other files in the same package: |
| 332 | // global variables so we know they don't need resolving, and imports |
| 333 | // that we might want to mimic. |
| 334 | globals := map[string]bool{} |
| 335 | for _, otherFile := range p.otherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#otherFiles. { |
| 336 | // Don't load globals from files that are in the same directory |
| 337 | // but a different package. Using them to suggest imports is OK. |
| 338 | if p.fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == otherFile.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. { |
| 339 | addGlobalsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addGlobals().(otherFile, globals) |
| 340 | } |
| 341 | p.candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates. = append(p.candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates., collectImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectImports().(otherFile)...) |
| 342 | } |
| 343 | |
| 344 | // Resolve all the import paths we've seen to package names, and store |
| 345 | // f's imports by the identifier they introduce. |
| 346 | imports := collectImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectImports().(p.fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f.) |
| 347 | if p.loadRealPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#loadRealPackageNames. { |
| 348 | err := p.loadPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#loadPackageNames().(ctx, append(imports, p.candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates....)) |
| 349 | if err != nil { |
| 350 | if p.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf. != nil { |
| 351 | p.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf.("loading package names: %v", err) |
| 352 | } |
| 353 | return nil, false |
| 354 | } |
| 355 | } |
| 356 | for _, imp := range imports { |
| 357 | p.existingImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#existingImports.[p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp)] = append(p.existingImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#existingImports.[p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp)], imp) |
| 358 | } |
| 359 | |
| 360 | // Find missing references. |
| 361 | for left, rights := range p.allRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#allRefs. { |
| 362 | if globals[left] { |
| 363 | continue |
| 364 | } |
| 365 | _, ok := p.existingImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#existingImports.[left] |
| 366 | if !ok { |
| 367 | p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs.[left] = rights |
| 368 | continue |
| 369 | } |
| 370 | } |
| 371 | if len(p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs.) != 0 { |
| 372 | return nil, false |
| 373 | } |
| 374 | |
| 375 | return p.fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fix().() |
| 376 | } |
| 377 | |
| 378 | // fix attempts to satisfy missing imports using p.candidates. If it finds |
| 379 | // everything, or if p.lastTry is true, it updates fixes to add the imports it found, |
| 380 | // delete anything unused, and update import names, and returns true. |
| 381 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fix().() ([]*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#, bool) { |
| 382 | // Find missing imports. |
| 383 | var selected []*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo# |
| 384 | for left, rights := range p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs. { |
| 385 | if imp := p.findMissingImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#findMissingImport().(left, rights); imp != nil { |
| 386 | selected = append(selected, imp) |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | if !p.lastTryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#lastTry. && len(selected) != len(p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs.) { |
| 391 | return nil, false |
| 392 | } |
| 393 | |
| 394 | // Found everything, or giving up. Add the new imports and remove any unused. |
| 395 | var fixes []*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix# |
| 396 | for _, identifierImports := range p.existingImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#existingImports. { |
| 397 | for _, imp := range identifierImports { |
| 398 | // We deliberately ignore globals here, because we can't be sure |
| 399 | // they're in the same package. People do things like put multiple |
| 400 | // main packages in the same directory, and we don't want to |
| 401 | // remove imports if they happen to have the same name as a var in |
| 402 | // a different package. |
| 403 | if _, ok := p.allRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#allRefs.[p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp)]; !ok { |
| 404 | fixes = append(fixes, &ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#{ |
| 405 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo.: *imp, |
| 406 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName.: p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp), |
| 407 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType.: DeleteImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DeleteImport., |
| 408 | }) |
| 409 | continue |
| 410 | } |
| 411 | |
| 412 | // An existing import may need to update its import name to be correct. |
| 413 | if name := p.importSpecNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importSpecName().(imp); name != imp.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. { |
| 414 | fixes = append(fixes, &ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#{ |
| 415 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo.: ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ |
| 416 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name.: name, |
| 417 | ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath., |
| 418 | }, |
| 419 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName.: p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp), |
| 420 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType.: SetImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/SetImportName., |
| 421 | }) |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | // Collecting fixes involved map iteration, so sort for stability. See |
| 426 | // golang/go#59976. |
| 427 | sortFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortFixes().(fixes) |
| 428 | |
| 429 | // collect selected fixes in a separate slice, so that it can be sorted |
| 430 | // separately. Note that these fixes must occur after fixes to existing |
| 431 | // imports. TODO(rfindley): figure out why. |
| 432 | var selectedFixes []*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix# |
| 433 | for _, imp := range selected { |
| 434 | selectedFixes = append(selectedFixes, &ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#{ |
| 435 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo.: ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ |
| 436 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name.: p.importSpecNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importSpecName().(imp), |
| 437 | ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath., |
| 438 | }, |
| 439 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName.: p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp), |
| 440 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType.: AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/AddImport., |
| 441 | }) |
| 442 | } |
| 443 | sortFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortFixes().(selectedFixes) |
| 444 | |
| 445 | return append(fixes, selectedFixes...), true |
| 446 | } |
| 447 | |
| 448 | func sortFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortFixes().(fixes []*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#) { |
| 449 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(fixes, func(i, j int) bool { |
| 450 | fi, fj := fixes[i], fixes[j] |
| 451 | if fi.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath. != fj.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath. { |
| 452 | return fi.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath. < fj.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath. |
| 453 | } |
| 454 | if fi.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. != fj.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. { |
| 455 | return fi.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. < fj.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. |
| 456 | } |
| 457 | if fi.IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName. != fj.IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName. { |
| 458 | return fi.IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName. < fj.IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName. |
| 459 | } |
| 460 | return fi.FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType. < fj.FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType. |
| 461 | }) |
| 462 | } |
| 463 | |
| 464 | // importSpecName gets the import name of imp in the import spec. |
| 465 | // |
| 466 | // When the import identifier matches the assumed import name, the import name does |
| 467 | // not appear in the import spec. |
| 468 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) importSpecNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importSpecName().(imp *ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#) string { |
| 469 | // If we did not load the real package names, or the name is already set, |
| 470 | // we just return the existing name. |
| 471 | if !p.loadRealPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#loadRealPackageNames. || imp.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. != "" { |
| 472 | return imp.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name. |
| 473 | } |
| 474 | |
| 475 | ident := p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp) |
| 476 | if ident == ImportPathToAssumedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportPathToAssumedName().(imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.) { |
| 477 | return "" // ident not needed since the assumed and real names are the same. |
| 478 | } |
| 479 | return ident |
| 480 | } |
| 481 | |
| 482 | // apply will perform the fixes on f in order. |
| 483 | func applyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/apply().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, fixes []*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#) { |
| 484 | for _, fix := range fixes { |
| 485 | switch fix.FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType. { |
| 486 | case DeleteImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DeleteImport.: |
| 487 | astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/.DeleteNamedImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/DeleteNamedImport().(fset, f, fix.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name., fix.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.) |
| 488 | case AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/AddImport.: |
| 489 | astutilscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/.AddNamedImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/astutil`/AddNamedImport().(fset, f, fix.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name., fix.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.) |
| 490 | case SetImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/SetImportName.: |
| 491 | // Find the matching import path and change the name. |
| 492 | for _, spec := range f.Importsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Imports. { |
| 493 | path := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Trimscip-go gomod github.com/golang/go/src go1.25.0 strings/Trim().(spec.Pathscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Path..Valuescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/BasicLit#Value., `"`) |
| 494 | if path == fix.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath. { |
| 495 | spec.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Name. = &astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Identscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#{ |
| 496 | Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.: fix.StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name., |
| 497 | NamePosscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#NamePos.: spec.Posscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#Pos().(), |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // assumeSiblingImportsValid assumes that siblings' use of packages is valid, |
| 506 | // adding the exports they use. |
| 507 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) assumeSiblingImportsValidscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#assumeSiblingImportsValid().() { |
| 508 | for _, f := range p.otherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#otherFiles. { |
| 509 | refs := collectReferencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectReferences().(f) |
| 510 | imports := collectImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/collectImports().(f) |
| 511 | importsByName := map[string]*ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{} |
| 512 | for _, imp := range imports { |
| 513 | importsByName[p.importIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#importIdentifier().(imp)] = imp |
| 514 | } |
| 515 | for left, rights := range refs { |
| 516 | if imp, ok := importsByName[left]; ok { |
| 517 | if m, ok := stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.PackageSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/PackageSymbols.[imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.]; ok { |
| 518 | // We have the stdlib in memory; no need to guess. |
| 519 | rights = symbolNameSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolNameSet().(m) |
| 520 | } |
| 521 | // TODO(rfindley): we should set package name here, for consistency. |
| 522 | p.addCandidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#addCandidate().(imp, &PackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#{ |
| 523 | // no name; we already know it. |
| 524 | Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Exports.: rights, |
| 525 | }) |
| 526 | } |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // addCandidate adds a candidate import to p, and merges in the information |
| 532 | // in pkg. |
| 533 | func (p *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#) addCandidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#addCandidate().(imp *ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#, pkg *PackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#) { |
| 534 | p.candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates. = append(p.candidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#candidates., imp) |
| 535 | if existing, ok := p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages.[imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.]; ok { |
| 536 | if existing.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name. == "" { |
| 537 | existing.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name. = pkg.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name. |
| 538 | } |
| 539 | for export := range pkg.Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Exports. { |
| 540 | existing.Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Exports.[export] = true |
| 541 | } |
| 542 | } else { |
| 543 | p.knownPackagesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#knownPackages.[imp.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.] = pkg |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | // fixImports adds and removes imports from f so that all its references are |
| 548 | // satisfied and there are no unused imports. |
| 549 | // |
| 550 | // This is declared as a variable rather than a function so goimports can |
| 551 | // easily be extended by adding a file with an init function. |
| 552 | // |
| 553 | // DO NOT REMOVE: used internally at Google. |
| 554 | var fixImportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/fixImports. = fixImportsDefaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/fixImportsDefault(). |
| 555 | |
| 556 | func fixImportsDefaultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/fixImportsDefault().(fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, filename string, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) error { |
| 557 | fixes, err := getFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getFixes().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Backgroundscip-go gomod github.com/golang/go/src go1.25.0 context/Background().(), fset, f, filename, env) |
| 558 | if err != nil { |
| 559 | return err |
| 560 | } |
| 561 | applyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/apply().(fset, f, fixes) |
| 562 | return nil |
| 563 | } |
| 564 | |
| 565 | // getFixes gets the import fixes that need to be made to f in order to fix the imports. |
| 566 | // It does not modify the ast. |
| 567 | func getFixesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getFixes().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, filename string, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) ([]*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#, error) { |
| 568 | source, err := NewProcessEnvSourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/NewProcessEnvSource().(env, filename, f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name.) |
| 569 | if err != nil { |
| 570 | return nil, err |
| 571 | } |
| 572 | goEnv, err := env.goEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#goEnv().() |
| 573 | if err != nil { |
| 574 | return nil, err |
| 575 | } |
| 576 | return getFixesWithSourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getFixesWithSource().(ctx, fset, f, filename, goEnv["GOROOT"], env.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf()., source) |
| 577 | } |
| 578 | |
| 579 | func getFixesWithSourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getFixesWithSource().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, fset *tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.FileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/FileSet#, f *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Filescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#, filename string, goroot string, logf func(string, ...any), source Sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Source#) ([]*ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#, error) { |
| 580 | // This logic is defensively duplicated from getFixes. |
| 581 | abs, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(filename) |
| 582 | if err != nil { |
| 583 | return nil, err |
| 584 | } |
| 585 | srcDir := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(abs) |
| 586 | |
| 587 | if logf != nil { |
| 588 | logf("fixImports(filename=%q), srcDir=%q ...", filename, srcDir) |
| 589 | } |
| 590 | |
| 591 | // First pass: looking only at f, and using the naive algorithm to |
| 592 | // derive package names from import paths, see if the file is already |
| 593 | // complete. We can't add any imports yet, because we don't know |
| 594 | // if missing references are actually package vars. |
| 595 | p := &passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#{ |
| 596 | fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fset.: fset, |
| 597 | fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f.: f, |
| 598 | srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#srcDir.: srcDir, |
| 599 | logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf.: logf, |
| 600 | gorootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#goroot.: goroot, |
| 601 | sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#source.: source, |
| 602 | } |
| 603 | if fixes, done := p.loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#load().(ctx); done { |
| 604 | return fixes, nil |
| 605 | } |
| 606 | |
| 607 | otherFiles, err := parseOtherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/parseOtherFiles().(ctx, fset, srcDir, filename) |
| 608 | if err != nil { |
| 609 | return nil, err |
| 610 | } |
| 611 | |
| 612 | // Second pass: add information from other files in the same package, |
| 613 | // like their package vars and imports. |
| 614 | p.otherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#otherFiles. = otherFiles |
| 615 | if fixes, done := p.loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#load().(ctx); done { |
| 616 | return fixes, nil |
| 617 | } |
| 618 | |
| 619 | // Now we can try adding imports from the stdlib. |
| 620 | p.assumeSiblingImportsValidscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#assumeSiblingImportsValid().() |
| 621 | addStdlibCandidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addStdlibCandidates().(p, p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs.) |
| 622 | if fixes, done := p.fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fix().(); done { |
| 623 | return fixes, nil |
| 624 | } |
| 625 | |
| 626 | // Third pass: get real package names where we had previously used |
| 627 | // the naive algorithm. |
| 628 | p = &passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#{ |
| 629 | fsetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fset.: fset, |
| 630 | fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f.: f, |
| 631 | srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#srcDir.: srcDir, |
| 632 | logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#logf.: logf, |
| 633 | gorootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#goroot.: goroot, |
| 634 | sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#source.: p.sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#source., // safe to reuse, as it's just a wrapper around env |
| 635 | } |
| 636 | p.loadRealPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#loadRealPackageNames. = true |
| 637 | p.otherFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#otherFiles. = otherFiles |
| 638 | if fixes, done := p.loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#load().(ctx); done { |
| 639 | return fixes, nil |
| 640 | } |
| 641 | |
| 642 | if err := addStdlibCandidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addStdlibCandidates().(p, p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs.); err != nil { |
| 643 | return nil, err |
| 644 | } |
| 645 | p.assumeSiblingImportsValidscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#assumeSiblingImportsValid().() |
| 646 | if fixes, done := p.fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fix().(); done { |
| 647 | return fixes, nil |
| 648 | } |
| 649 | |
| 650 | // Go look for candidates in $GOPATH, etc. We don't necessarily load |
| 651 | // the real exports of sibling imports, so keep assuming their contents. |
| 652 | if err := addExternalCandidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addExternalCandidates().(ctx, p, p.missingRefsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#missingRefs., filename); err != nil { |
| 653 | return nil, err |
| 654 | } |
| 655 | |
| 656 | p.lastTryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#lastTry. = true |
| 657 | fixes, _ := p.fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#fix().() |
| 658 | return fixes, nil |
| 659 | } |
| 660 | |
| 661 | // MaxRelevance is the highest relevance, used for the standard library. |
| 662 | // Chosen arbitrarily to match pre-existing gopls code. |
| 663 | const MaxRelevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/MaxRelevance. = 7.0 |
| 664 | |
| 665 | // getCandidatePkgs works with the passed callback to find all acceptable packages. |
| 666 | // It deduplicates by import path, and uses a cached stdlib rather than reading |
| 667 | // from disk. |
| 668 | func getCandidatePkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getCandidatePkgs().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, wrappedCallback *scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#, filename, filePkg string, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) error { |
| 669 | notSelf := func(p *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 670 | return p.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName. != filePkg || p.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir. != filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(filename) |
| 671 | } |
| 672 | goenv, err := env.goEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#goEnv().() |
| 673 | if err != nil { |
| 674 | return err |
| 675 | } |
| 676 | |
| 677 | var mu syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex# // to guard asynchronous access to dupCheck |
| 678 | dupCheck := map[string]struct{}{} |
| 679 | |
| 680 | // Start off with the standard library. |
| 681 | for importPath, symbols := range stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.PackageSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/PackageSymbols. { |
| 682 | p := &pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#{ |
| 683 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.: filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(goenv["GOROOT"], "src", importPath), |
| 684 | importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.: importPath, |
| 685 | packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName.: pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(importPath), |
| 686 | relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance.: MaxRelevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/MaxRelevance., |
| 687 | } |
| 688 | dupCheck[importPath] = struct{}{} |
| 689 | if notSelf(p) && wrappedCallback.dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.(p) && wrappedCallback.packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.(p) { |
| 690 | var exports []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol# |
| 691 | for _, sym := range symbols { |
| 692 | switch sym.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Kind. { |
| 693 | case stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Func., stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Type., stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Var., stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Const.: |
| 694 | exports = append(exports, sym) |
| 695 | } |
| 696 | } |
| 697 | wrappedCallback.exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#exportsLoaded.(p, exports) |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | scanFilter := &scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#{ |
| 702 | rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.: func(root gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool { |
| 703 | // Exclude goroot results -- getting them is relatively expensive, not cached, |
| 704 | // and generally redundant with the in-memory version. |
| 705 | return root.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type. != gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.RootGOROOTscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOROOT. && wrappedCallback.rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.(root) |
| 706 | }, |
| 707 | dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.: wrappedCallback.dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound., |
| 708 | packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 709 | mu.Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 710 | defer mu.Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 711 | if _, ok := dupCheck[pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.]; ok { |
| 712 | return false |
| 713 | } |
| 714 | dupCheck[pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.] = struct{}{} |
| 715 | return notSelf(pkg) && wrappedCallback.packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.(pkg) |
| 716 | }, |
| 717 | exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#exportsLoaded.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, exports []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#) { |
| 718 | // If we're an x_test, load the package under test's test variant. |
| 719 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(filePkg, "_test") && pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir. == filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Dirscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Dir().(filename) { |
| 720 | var err error |
| 721 | _, exports, err = loadExportsFromFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/loadExportsFromFiles().(ctx, env, pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir., true) |
| 722 | if err != nil { |
| 723 | return |
| 724 | } |
| 725 | } |
| 726 | wrappedCallback.exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#exportsLoaded.(pkg, exports) |
| 727 | }, |
| 728 | } |
| 729 | resolver, err := env.GetResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GetResolver().() |
| 730 | if err != nil { |
| 731 | return err |
| 732 | } |
| 733 | return resolver.scanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#scan.(ctx, scanFilter) |
| 734 | } |
| 735 | |
| 736 | func ScoreImportPathsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ScoreImportPaths().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#, paths []string) (map[string]float64, error) { |
| 737 | result := make(map[string]float64) |
| 738 | resolver, err := env.GetResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GetResolver().() |
| 739 | if err != nil { |
| 740 | return nil, err |
| 741 | } |
| 742 | for _, path := range paths { |
| 743 | result[path] = resolver.scoreImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#scoreImportPath.(ctx, path) |
| 744 | } |
| 745 | return result, nil |
| 746 | } |
| 747 | |
| 748 | func PrimeCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PrimeCache().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, resolver Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#) error { |
| 749 | // Fully scan the disk for directories, but don't actually read any Go files. |
| 750 | callback := &scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#{ |
| 751 | rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.: func(root gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool { |
| 752 | // See getCandidatePkgs: walking GOROOT is apparently expensive and |
| 753 | // unnecessary. |
| 754 | return root.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type. != gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.RootGOROOTscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOROOT. |
| 755 | }, |
| 756 | dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 757 | return false |
| 758 | }, |
| 759 | // packageNameLoaded and exportsLoaded must never be called. |
| 760 | } |
| 761 | |
| 762 | return resolver.scanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#scan.(ctx, callback) |
| 763 | } |
| 764 | |
| 765 | func candidateImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/candidateImportName().(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) string { |
| 766 | if ImportPathToAssumedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportPathToAssumedName().(pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.) != pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName. { |
| 767 | return pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName. |
| 768 | } |
| 769 | return "" |
| 770 | } |
| 771 | |
| 772 | // GetAllCandidates calls wrapped for each package whose name starts with |
| 773 | // searchPrefix, and can be imported from filename with the package name filePkg. |
| 774 | // |
| 775 | // Beware that the wrapped function may be called multiple times concurrently. |
| 776 | // TODO(adonovan): encapsulate the concurrency. |
| 777 | func GetAllCandidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/GetAllCandidates().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, wrapped func(ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#), searchPrefix, filename, filePkg string, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) error { |
| 778 | callback := &scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#{ |
| 779 | rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.: func(gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool { |
| 780 | return true |
| 781 | }, |
| 782 | dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 783 | if !CanUsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/CanUse().(filename, pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.) { |
| 784 | return false |
| 785 | } |
| 786 | // Try the assumed package name first, then a simpler path match |
| 787 | // in case of packages named vN, which are not uncommon. |
| 788 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(ImportPathToAssumedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportPathToAssumedName().(pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.), searchPrefix) || |
| 789 | stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.), searchPrefix) |
| 790 | }, |
| 791 | packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 792 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName., searchPrefix) { |
| 793 | return false |
| 794 | } |
| 795 | wrapped(ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#{ |
| 796 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo.: ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ |
| 797 | ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort., |
| 798 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name.: candidateImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/candidateImportName().(pkg), |
| 799 | }, |
| 800 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName.: pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName., |
| 801 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType.: AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/AddImport., |
| 802 | Relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#Relevance.: pkg.relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance., |
| 803 | }) |
| 804 | return false |
| 805 | }, |
| 806 | } |
| 807 | return getCandidatePkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getCandidatePkgs().(ctx, callback, filename, filePkg, env) |
| 808 | } |
| 809 | |
| 810 | // GetImportPaths calls wrapped for each package whose import path starts with |
| 811 | // searchPrefix, and can be imported from filename with the package name filePkg. |
| 812 | func GetImportPathsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/GetImportPaths().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, wrapped func(ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#), searchPrefix, filename, filePkg string, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) error { |
| 813 | callback := &scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#{ |
| 814 | rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.: func(gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool { |
| 815 | return true |
| 816 | }, |
| 817 | dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 818 | if !CanUsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/CanUse().(filename, pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.) { |
| 819 | return false |
| 820 | } |
| 821 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort., searchPrefix) |
| 822 | }, |
| 823 | packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 824 | wrapped(ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#{ |
| 825 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo.: ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ |
| 826 | ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort., |
| 827 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name.: candidateImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/candidateImportName().(pkg), |
| 828 | }, |
| 829 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName.: pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName., |
| 830 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType.: AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/AddImport., |
| 831 | Relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#Relevance.: pkg.relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance., |
| 832 | }) |
| 833 | return false |
| 834 | }, |
| 835 | } |
| 836 | return getCandidatePkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getCandidatePkgs().(ctx, callback, filename, filePkg, env) |
| 837 | } |
| 838 | |
| 839 | // A PackageExport is a package and its exports. |
| 840 | type PackageExportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport# struct { |
| 841 | Fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport#Fix. *ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix# |
| 842 | Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport#Exports. []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol# |
| 843 | } |
| 844 | |
| 845 | // GetPackageExports returns all known packages with name pkg and their exports. |
| 846 | func GetPackageExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/GetPackageExports().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, wrapped func(PackageExportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport#), searchPkg, filename, filePkg string, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) error { |
| 847 | callback := &scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#{ |
| 848 | rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.: func(gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool { |
| 849 | return true |
| 850 | }, |
| 851 | dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 852 | return pkgIsCandidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgIsCandidate().(filename, Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References#{searchPkg: nil}, pkg) |
| 853 | }, |
| 854 | packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 855 | return pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName. == searchPkg |
| 856 | }, |
| 857 | exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#exportsLoaded.: func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, exports []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#) { |
| 858 | sortSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortSymbols().(exports) |
| 859 | wrapped(PackageExportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport#{ |
| 860 | Fixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport#Fix.: &ImportFixscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#{ |
| 861 | StmtInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#StmtInfo.: ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ |
| 862 | ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort., |
| 863 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#Name.: candidateImportNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/candidateImportName().(pkg), |
| 864 | }, |
| 865 | IdentNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#IdentName.: pkg.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName., |
| 866 | FixTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#FixType.: AddImportscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/AddImport., |
| 867 | Relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportFix#Relevance.: pkg.relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance., |
| 868 | }, |
| 869 | Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageExport#Exports.: exports, |
| 870 | }) |
| 871 | }, |
| 872 | } |
| 873 | return getCandidatePkgsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/getCandidatePkgs().(ctx, callback, filename, filePkg, env) |
| 874 | } |
| 875 | |
| 876 | // TODO(rfindley): we should depend on GOOS and GOARCH, to provide accurate |
| 877 | // imports when doing cross-platform development. |
| 878 | var requiredGoEnvVarsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/requiredGoEnvVars. = []string{ |
| 879 | "GO111MODULE", |
| 880 | "GOFLAGS", |
| 881 | "GOINSECURE", |
| 882 | "GOMOD", |
| 883 | "GOMODCACHE", |
| 884 | "GONOPROXY", |
| 885 | "GONOSUMDB", |
| 886 | "GOPATH", |
| 887 | "GOPROXY", |
| 888 | "GOROOT", |
| 889 | "GOSUMDB", |
| 890 | "GOWORK", |
| 891 | } |
| 892 | |
| 893 | // ProcessEnv contains environment variables and settings that affect the use of |
| 894 | // the go command, the go/build package, etc. |
| 895 | // |
| 896 | // ...a ProcessEnv *also* overwrites its Env along with derived state in the |
| 897 | // form of the resolver. And because it is lazily initialized, an env may just |
| 898 | // be broken and unusable, but there is no way for the caller to detect that: |
| 899 | // all queries will just fail. |
| 900 | // |
| 901 | // TODO(rfindley): refactor this package so that this type (perhaps renamed to |
| 902 | // just Env or Config) is an immutable configuration struct, to be exchanged |
| 903 | // for an initialized object via a constructor that returns an error. Perhaps |
| 904 | // the signature should be `func NewResolver(*Env) (*Resolver, error)`, where |
| 905 | // resolver is a concrete type used for resolving imports. Via this |
| 906 | // refactoring, we can avoid the need to call ProcessEnv.init and |
| 907 | // ProcessEnv.GoEnv everywhere, and implicitly fix all the places where this |
| 908 | // these are misused. Also, we'd delegate the caller the decision of how to |
| 909 | // handle a broken environment. |
| 910 | type ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv# struct { |
| 911 | GocmdRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GocmdRunner. *gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.Runnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner# |
| 912 | |
| 913 | BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#BuildFlags. []string |
| 914 | ModFlagscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#ModFlag. string |
| 915 | |
| 916 | // SkipPathInScan returns true if the path should be skipped from scans of |
| 917 | // the RootCurrentModule root type. The function argument is a clean, |
| 918 | // absolute path. |
| 919 | SkipPathInScanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#SkipPathInScan. func(string) bool |
| 920 | |
| 921 | // Env overrides the OS environment, and can be used to specify |
| 922 | // GOPROXY, GO111MODULE, etc. PATH cannot be set here, because |
| 923 | // exec.Command will not honor it. |
| 924 | // Specifying all of requiredGoEnvVars avoids a call to `go env`. |
| 925 | Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env. map[string]string |
| 926 | |
| 927 | WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#WorkingDir. string |
| 928 | |
| 929 | // If Logf is non-nil, debug logging is enabled through this function. |
| 930 | Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf. func(format string, args ...any) |
| 931 | |
| 932 | // If set, ModCache holds a shared cache of directory info to use across |
| 933 | // multiple ProcessEnvs. |
| 934 | ModCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#ModCache. *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache# |
| 935 | |
| 936 | initializedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#initialized. bool // see TODO above |
| 937 | |
| 938 | // resolver and resolverErr are lazily evaluated (see GetResolver). |
| 939 | // This is unclean, but see the big TODO in the docstring for ProcessEnv |
| 940 | // above: for now, we can't be sure that the ProcessEnv is fully initialized. |
| 941 | resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolver. Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver# |
| 942 | resolverErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolverErr. error |
| 943 | } |
| 944 | |
| 945 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) goEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#goEnv().() (map[string]string, error) { |
| 946 | if err := e.initscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#init().(); err != nil { |
| 947 | return nil, err |
| 948 | } |
| 949 | return e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env., nil |
| 950 | } |
| 951 | |
| 952 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) matchFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#matchFile().(dir, name string) (bool, error) { |
| 953 | bctx, err := e.buildContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#buildContext().() |
| 954 | if err != nil { |
| 955 | return false, err |
| 956 | } |
| 957 | return bctx.MatchFilescip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#MatchFile().(dir, name) |
| 958 | } |
| 959 | |
| 960 | // CopyConfig copies the env's configuration into a new env. |
| 961 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) CopyConfigscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#CopyConfig().() *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv# { |
| 962 | copy := &ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#{ |
| 963 | GocmdRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GocmdRunner.: e.GocmdRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GocmdRunner., |
| 964 | initializedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#initialized.: e.initializedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#initialized., |
| 965 | BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#BuildFlags.: e.BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#BuildFlags., |
| 966 | Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf.: e.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf., |
| 967 | WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#WorkingDir.: e.WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#WorkingDir., |
| 968 | resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolver.: nil, |
| 969 | Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env.: map[string]string{}, |
| 970 | } |
| 971 | mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.Copyscip-go gomod github.com/golang/go/src go1.25.0 maps/Copy().(copy.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env., e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env.) |
| 972 | return copy |
| 973 | } |
| 974 | |
| 975 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) initscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#init().() error { |
| 976 | if e.initializedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#initialized. { |
| 977 | return nil |
| 978 | } |
| 979 | |
| 980 | foundAllRequired := true |
| 981 | for _, k := range requiredGoEnvVarsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/requiredGoEnvVars. { |
| 982 | if _, ok := e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env.[k]; !ok { |
| 983 | foundAllRequired = false |
| 984 | break |
| 985 | } |
| 986 | } |
| 987 | if foundAllRequired { |
| 988 | e.initializedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#initialized. = true |
| 989 | return nil |
| 990 | } |
| 991 | |
| 992 | if e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env. == nil { |
| 993 | e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env. = map[string]string{} |
| 994 | } |
| 995 | |
| 996 | goEnv := map[string]string{} |
| 997 | stdout, err := e.invokeGoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#invokeGo().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.TODOscip-go gomod github.com/golang/go/src go1.25.0 context/TODO().(), "env", append([]string{"-json"}, requiredGoEnvVarsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/requiredGoEnvVars....)...) |
| 998 | if err != nil { |
| 999 | return err |
| 1000 | } |
| 1001 | if err := jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(stdout.Bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#Bytes().(), &goEnv); err != nil { |
| 1002 | return err |
| 1003 | } |
| 1004 | mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.Copyscip-go gomod github.com/golang/go/src go1.25.0 maps/Copy().(e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env., goEnv) |
| 1005 | e.initializedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#initialized. = true |
| 1006 | return nil |
| 1007 | } |
| 1008 | |
| 1009 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#env().() []string { |
| 1010 | var env []string // the gocommand package will prepend os.Environ. |
| 1011 | for k, v := range e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env. { |
| 1012 | env = append(env, k+"="+v) |
| 1013 | } |
| 1014 | return env |
| 1015 | } |
| 1016 | |
| 1017 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) GetResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GetResolver().() (Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#, error) { |
| 1018 | if err := e.initscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#init().(); err != nil { |
| 1019 | return nil, err |
| 1020 | } |
| 1021 | |
| 1022 | if e.resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolver. == nil && e.resolverErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolverErr. == nil { |
| 1023 | // TODO(rfindley): we should only use a gopathResolver here if the working |
| 1024 | // directory is actually *in* GOPATH. (I seem to recall an open gopls issue |
| 1025 | // for this behavior, but I can't find it). |
| 1026 | // |
| 1027 | // For gopls, we can optionally explicitly choose a resolver type, since we |
| 1028 | // already know the view type. |
| 1029 | if e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env.["GOMOD"] == "" && (e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env.["GOWORK"] == "" || e.Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Env.["GOWORK"] == "off") { |
| 1030 | e.resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolver. = newGopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/newGopathResolver().(e) |
| 1031 | e.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf().("created gopath resolver") |
| 1032 | } else if r, err := newModuleResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/newModuleResolver().(e, e.ModCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#ModCache.); err != nil { |
| 1033 | e.resolverErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolverErr. = err |
| 1034 | e.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf().("failed to create module resolver: %v", err) |
| 1035 | } else { |
| 1036 | e.resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolver. = Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#(r) |
| 1037 | e.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf().("created module resolver") |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | return e.resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolver., e.resolverErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#resolverErr. |
| 1042 | } |
| 1043 | |
| 1044 | // logf logs if e.Logf is non-nil. |
| 1045 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf().(format string, args ...any) { |
| 1046 | if e.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf. != nil { |
| 1047 | e.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf.(format, args...) |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | // buildContext returns the build.Context to use for matching files. |
| 1052 | // |
| 1053 | // TODO(rfindley): support dynamic GOOS, GOARCH here, when doing cross-platform |
| 1054 | // development. |
| 1055 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) buildContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#buildContext().() (*buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, error) { |
| 1056 | ctx := buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Defaultscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Default. |
| 1057 | goenv, err := e.goEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#goEnv().() |
| 1058 | if err != nil { |
| 1059 | return nil, err |
| 1060 | } |
| 1061 | ctx.GOROOTscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOROOT. = goenv["GOROOT"] |
| 1062 | ctx.GOPATHscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#GOPATH. = goenv["GOPATH"] |
| 1063 | |
| 1064 | // As of Go 1.14, build.Context has a Dir field |
| 1065 | // (see golang.org/issue/34860). |
| 1066 | // Populate it only if present. |
| 1067 | rc := reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.ValueOfscip-go gomod github.com/golang/go/src go1.25.0 reflect/ValueOf().(&ctx).Elemscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Elem().() |
| 1068 | dir := rc.FieldByNamescip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#FieldByName().("Dir") |
| 1069 | if dir.IsValidscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#IsValid().() && dir.Kindscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#Kind().() == reflectscip-go gomod github.com/golang/go/src go1.25.0 reflect/.Stringscip-go gomod github.com/golang/go/src go1.25.0 reflect/String. { |
| 1070 | dir.SetStringscip-go gomod github.com/golang/go/src go1.25.0 reflect/Value#SetString().(e.WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#WorkingDir.) |
| 1071 | } |
| 1072 | |
| 1073 | // Since Go 1.11, go/build.Context.Import may invoke 'go list' depending on |
| 1074 | // the value in GO111MODULE in the process's environment. We always want to |
| 1075 | // run in GOPATH mode when calling Import, so we need to prevent this from |
| 1076 | // happening. In Go 1.16, GO111MODULE defaults to "on", so this problem comes |
| 1077 | // up more frequently. |
| 1078 | // |
| 1079 | // HACK: setting any of the Context I/O hooks prevents Import from invoking |
| 1080 | // 'go list', regardless of GO111MODULE. This is undocumented, but it's |
| 1081 | // unlikely to change before GOPATH support is removed. |
| 1082 | ctx.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#ReadDir. = ioutilscip-go gomod github.com/golang/go/src go1.25.0 `io/ioutil`/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 `io/ioutil`/ReadDir(). |
| 1083 | |
| 1084 | return &ctx, nil |
| 1085 | } |
| 1086 | |
| 1087 | func (e *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) invokeGoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#invokeGo().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, verb string, args ...string) (*bytesscip-go gomod github.com/golang/go/src go1.25.0 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.25.0 bytes/Buffer#, error) { |
| 1088 | inv := gocommandscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/.Invocationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#{ |
| 1089 | Verbscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Verb.: verb, |
| 1090 | Argsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Args.: args, |
| 1091 | BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#BuildFlags.: e.BuildFlagsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#BuildFlags., |
| 1092 | Envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Env.: e.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#env().(), |
| 1093 | Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#Logf.: e.Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf., |
| 1094 | WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Invocation#WorkingDir.: e.WorkingDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#WorkingDir., |
| 1095 | } |
| 1096 | return e.GocmdRunnerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#GocmdRunner..Runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gocommand`/Runner#Run().(ctx, inv) |
| 1097 | } |
| 1098 | |
| 1099 | func addStdlibCandidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addStdlibCandidates().(pass *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#, refs Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References#) error { |
| 1100 | localbase := func(nm string) string { |
| 1101 | ans := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(nm) |
| 1102 | if ans[0] == 'v' { |
| 1103 | // this is called, for instance, with math/rand/v2 and returns rand/v2 |
| 1104 | if _, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(ans[1:]); err == nil { |
| 1105 | ix := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(nm, ans) |
| 1106 | more := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(nm[:ix]) |
| 1107 | ans = pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Joinscip-go gomod github.com/golang/go/src go1.25.0 path/Join().(more, ans) |
| 1108 | } |
| 1109 | } |
| 1110 | return ans |
| 1111 | } |
| 1112 | add := func(pkg string) { |
| 1113 | // Prevent self-imports. |
| 1114 | if pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(pkg) == pass.fscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#f..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. && filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(pass.gorootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#goroot., "src", pkg) == pass.srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#srcDir. { |
| 1115 | return |
| 1116 | } |
| 1117 | exports := symbolNameSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolNameSet().(stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.PackageSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/PackageSymbols.[pkg]) |
| 1118 | pass.addCandidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#addCandidate().( |
| 1119 | &ImportInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#{ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportInfo#ImportPath.: pkg}, |
| 1120 | &PackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#{Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name.: localbase(pkg), Exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Exports.: exports}) |
| 1121 | } |
| 1122 | for left := range refs { |
| 1123 | if left == "rand" { |
| 1124 | // Make sure we try crypto/rand before any version of math/rand as both have Int() |
| 1125 | // and our policy is to recommend crypto |
| 1126 | add("crypto/rand") |
| 1127 | // if the user's no later than go1.21, this should be "math/rand" |
| 1128 | // but we have no way of figuring out what the user is using |
| 1129 | // TODO: investigate using the toolchain version to disambiguate in the stdlib |
| 1130 | add("math/rand/v2") |
| 1131 | // math/rand has an overlapping API |
| 1132 | // TestIssue66407 fails without this |
| 1133 | add("math/rand") |
| 1134 | continue |
| 1135 | } |
| 1136 | for importPath := range stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.PackageSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/PackageSymbols. { |
| 1137 | if pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(importPath) == left { |
| 1138 | add(importPath) |
| 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | return nil |
| 1143 | } |
| 1144 | |
| 1145 | // A Resolver does the build-system-specific parts of goimports. |
| 1146 | type Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver# interface { |
| 1147 | // loadPackageNames loads the package names in importPaths. |
| 1148 | loadPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#loadPackageNames.(importPaths []string, srcDir string) (map[string]string, error) |
| 1149 | |
| 1150 | // scan works with callback to search for packages. See scanCallback for details. |
| 1151 | scanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#scan.(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, callback *scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#) error |
| 1152 | |
| 1153 | // loadExports returns the package name and set of exported symbols in the |
| 1154 | // package at dir. loadExports may be called concurrently. |
| 1155 | loadExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#loadExports.(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, includeTest bool) (string, []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#, error) |
| 1156 | |
| 1157 | // scoreImportPath returns the relevance for an import path. |
| 1158 | scoreImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#scoreImportPath.(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, path string) float64 |
| 1159 | |
| 1160 | // ClearForNewScan returns a new Resolver based on the receiver that has |
| 1161 | // cleared its internal caches of directory contents. |
| 1162 | // |
| 1163 | // The new resolver should be primed and then set via |
| 1164 | // [ProcessEnv.UpdateResolver]. |
| 1165 | ClearForNewScanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver#ClearForNewScan.() Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver# |
| 1166 | } |
| 1167 | |
| 1168 | // A scanCallback controls a call to scan and receives its results. |
| 1169 | // In general, minor errors will be silently discarded; a user should not |
| 1170 | // expect to receive a full series of calls for everything. |
| 1171 | type scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback# struct { |
| 1172 | // rootFound is called before scanning a new root dir. If it returns true, |
| 1173 | // the root will be scanned. Returning false will not necessarily prevent |
| 1174 | // directories from that root making it to dirFound. |
| 1175 | rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound. func(gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool |
| 1176 | // dirFound is called when a directory is found that is possibly a Go package. |
| 1177 | // pkg will be populated with everything except packageName. |
| 1178 | // If it returns true, the package's name will be loaded. |
| 1179 | dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound. func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool |
| 1180 | // packageNameLoaded is called when a package is found and its name is loaded. |
| 1181 | // If it returns true, the package's exports will be loaded. |
| 1182 | packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded. func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool |
| 1183 | // exportsLoaded is called when a package's exports have been loaded. |
| 1184 | exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#exportsLoaded. func(pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, exports []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#) |
| 1185 | } |
| 1186 | |
| 1187 | func addExternalCandidatesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/addExternalCandidates().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, pass *passscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#, refs Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References#, filename string) error { |
| 1188 | ctx, done := eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/.Startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/Start().(ctx, "imports.addExternalCandidates") |
| 1189 | defer done() |
| 1190 | |
| 1191 | results, err := pass.sourcescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#source..ResolveReferencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Source#ResolveReferences.(ctx, filename, refs) |
| 1192 | if err != nil { |
| 1193 | return err |
| 1194 | } |
| 1195 | |
| 1196 | for _, result := range results { |
| 1197 | if result == nil { |
| 1198 | continue |
| 1199 | } |
| 1200 | // Don't offer completions that would shadow predeclared |
| 1201 | // names, such as github.com/coreos/etcd/error. |
| 1202 | if typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Universescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Universe..Lookupscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Scope#Lookup().(result.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Result#Package..Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/PackageInfo#Name.) != nil { // predeclared |
| 1203 | // Ideally we would skip this candidate only |
| 1204 | // if the predeclared name is actually |
| 1205 | // referenced by the file, but that's a lot |
| 1206 | // trickier to compute and would still create |
| 1207 | // an import that is likely to surprise the |
| 1208 | // user before long. |
| 1209 | continue |
| 1210 | } |
| 1211 | pass.addCandidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pass#addCandidate().(result.Importscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Result#Import., result.Packagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Result#Package.) |
| 1212 | } |
| 1213 | return nil |
| 1214 | } |
| 1215 | |
| 1216 | // notIdentifier reports whether ch is an invalid identifier character. |
| 1217 | func notIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/notIdentifier().(ch rune) bool { |
| 1218 | return !('a' <= ch && ch <= 'z' || 'A' <= ch && ch <= 'Z' || |
| 1219 | '0' <= ch && ch <= '9' || |
| 1220 | ch == '_' || |
| 1221 | ch >= utf8scip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/.RuneSelfscip-go gomod github.com/golang/go/src go1.25.0 `unicode/utf8`/RuneSelf. && (unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsLetterscip-go gomod github.com/golang/go/src go1.25.0 unicode/IsLetter().(ch) || unicodescip-go gomod github.com/golang/go/src go1.25.0 unicode/.IsDigitscip-go gomod github.com/golang/go/src go1.25.0 unicode/IsDigit().(ch))) |
| 1222 | } |
| 1223 | |
| 1224 | // ImportPathToAssumedName returns the assumed package name of an import path. |
| 1225 | // It does this using only string parsing of the import path. |
| 1226 | // It picks the last element of the path that does not look like a major |
| 1227 | // version, and then picks the valid identifier off the start of that element. |
| 1228 | // It is used to determine if a local rename should be added to an import for |
| 1229 | // clarity. |
| 1230 | // This function could be moved to a standard package and exported if we want |
| 1231 | // for use in other tools. |
| 1232 | func ImportPathToAssumedNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ImportPathToAssumedName().(importPath string) string { |
| 1233 | base := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(importPath) |
| 1234 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(base, "v") { |
| 1235 | if _, err := strconvscip-go gomod github.com/golang/go/src go1.25.0 strconv/.Atoiscip-go gomod github.com/golang/go/src go1.25.0 strconv/Atoi().(base[1:]); err == nil { |
| 1236 | dir := pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Dirscip-go gomod github.com/golang/go/src go1.25.0 path/Dir().(importPath) |
| 1237 | if dir != "." { |
| 1238 | base = pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(dir) |
| 1239 | } |
| 1240 | } |
| 1241 | } |
| 1242 | base = stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.TrimPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/TrimPrefix().(base, "go-") |
| 1243 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.IndexFuncscip-go gomod github.com/golang/go/src go1.25.0 strings/IndexFunc().(base, notIdentifierscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/notIdentifier().); i >= 0 { |
| 1244 | base = base[:i] |
| 1245 | } |
| 1246 | return base |
| 1247 | } |
| 1248 | |
| 1249 | // gopathResolver implements resolver for GOPATH workspaces. |
| 1250 | type gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver# struct { |
| 1251 | envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env. *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv# |
| 1252 | cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache. *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache# |
| 1253 | scanSemascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scanSema. chan struct{} // scanSema prevents concurrent scans. |
| 1254 | } |
| 1255 | |
| 1256 | func newGopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/newGopathResolver().(env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#) *gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver# { |
| 1257 | r := &gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#{ |
| 1258 | envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env.: env, |
| 1259 | cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache.: NewDirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/NewDirInfoCache().(), |
| 1260 | scanSemascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scanSema.: make(chan struct{}, 1), |
| 1261 | } |
| 1262 | r.scanSemascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scanSema. <- struct{}{} |
| 1263 | return r |
| 1264 | } |
| 1265 | |
| 1266 | func (r *gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#) ClearForNewScanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#ClearForNewScan().() Resolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/Resolver# { |
| 1267 | return newGopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/newGopathResolver().(r.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env.) |
| 1268 | } |
| 1269 | |
| 1270 | func (r *gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#) loadPackageNamesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#loadPackageNames().(importPaths []string, srcDir string) (map[string]string, error) { |
| 1271 | names := map[string]string{} |
| 1272 | bctx, err := r.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env..buildContextscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#buildContext().() |
| 1273 | if err != nil { |
| 1274 | return nil, err |
| 1275 | } |
| 1276 | for _, path := range importPaths { |
| 1277 | names[path] = importPathToNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPathToName().(bctx, path, srcDir) |
| 1278 | } |
| 1279 | return names, nil |
| 1280 | } |
| 1281 | |
| 1282 | // importPathToName finds out the actual package name, as declared in its .go files. |
| 1283 | func importPathToNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/importPathToName().(bctx *buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.Contextscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#, importPath, srcDir string) string { |
| 1284 | // Fast path for standard library without going to disk. |
| 1285 | if stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.HasPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/HasPackage().(importPath) { |
| 1286 | return pathscip-go gomod github.com/golang/go/src go1.25.0 path/.Basescip-go gomod github.com/golang/go/src go1.25.0 path/Base().(importPath) // stdlib packages always match their paths. |
| 1287 | } |
| 1288 | |
| 1289 | buildPkg, err := bctx.Importscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Context#Import().(importPath, srcDir, buildscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/.FindOnlyscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/FindOnly.) |
| 1290 | if err != nil { |
| 1291 | return "" |
| 1292 | } |
| 1293 | pkgName, err := packageDirToNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/packageDirToName().(buildPkg.Dirscip-go gomod github.com/golang/go/src go1.25.0 `go/build`/Package#Dir.) |
| 1294 | if err != nil { |
| 1295 | return "" |
| 1296 | } |
| 1297 | return pkgName |
| 1298 | } |
| 1299 | |
| 1300 | // packageDirToName is a faster version of build.Import if |
| 1301 | // the only thing desired is the package name. Given a directory, |
| 1302 | // packageDirToName then only parses one file in the package, |
| 1303 | // trusting that the files in the directory are consistent. |
| 1304 | func packageDirToNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/packageDirToName().(dir string) (packageName string, err error) { |
| 1305 | d, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Openscip-go gomod github.com/golang/go/src go1.25.0 os/Open().(dir) |
| 1306 | if err != nil { |
| 1307 | return "", err |
| 1308 | } |
| 1309 | names, err := d.Readdirnamesscip-go gomod github.com/golang/go/src go1.25.0 os/File#Readdirnames().(-1) |
| 1310 | d.Closescip-go gomod github.com/golang/go/src go1.25.0 os/File#Close().() |
| 1311 | if err != nil { |
| 1312 | return "", err |
| 1313 | } |
| 1314 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Stringsscip-go gomod github.com/golang/go/src go1.25.0 sort/Strings().(names) // to have predictable behavior |
| 1315 | var lastErr error |
| 1316 | var nfile int |
| 1317 | for _, name := range names { |
| 1318 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(name, ".go") { |
| 1319 | continue |
| 1320 | } |
| 1321 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(name, "_test.go") { |
| 1322 | continue |
| 1323 | } |
| 1324 | nfile++ |
| 1325 | fullFile := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, name) |
| 1326 | |
| 1327 | fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1328 | f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, fullFile, nil, parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.PackageClauseOnlyscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/PackageClauseOnly.) |
| 1329 | if err != nil { |
| 1330 | lastErr = err |
| 1331 | continue |
| 1332 | } |
| 1333 | pkgName := f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 1334 | if pkgName == "documentation" { |
| 1335 | // Special case from go/build.ImportDir, not |
| 1336 | // handled by ctx.MatchFile. |
| 1337 | continue |
| 1338 | } |
| 1339 | if pkgName == "main" { |
| 1340 | // Also skip package main, assuming it's a +build ignore generator or example. |
| 1341 | // Since you can't import a package main anyway, there's no harm here. |
| 1342 | continue |
| 1343 | } |
| 1344 | return pkgName, nil |
| 1345 | } |
| 1346 | if lastErr != nil { |
| 1347 | return "", lastErr |
| 1348 | } |
| 1349 | return "", fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("no importable package found in %d Go files", nfile) |
| 1350 | } |
| 1351 | |
| 1352 | type pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg# struct { |
| 1353 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir. string // absolute file path to pkg directory ("/usr/lib/go/src/net/http") |
| 1354 | importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort. string // vendorless import path ("net/http", "a/b") |
| 1355 | packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName. string // package name loaded from source if requested |
| 1356 | relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance. float64 // a weakly-defined score of how relevant a package is. 0 is most relevant. |
| 1357 | } |
| 1358 | |
| 1359 | type pkgDistancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance# struct { |
| 1360 | pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg. *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg# |
| 1361 | distancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#distance. int // relative distance to target |
| 1362 | } |
| 1363 | |
| 1364 | // byDistanceOrImportPathShortLength sorts by relative distance breaking ties |
| 1365 | // on the short import path length and then the import string itself. |
| 1366 | type byDistanceOrImportPathShortLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength# []pkgDistancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance# |
| 1367 | |
| 1368 | func (s byDistanceOrImportPathShortLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#) Lenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#Len().() int { return len(s) } |
| 1369 | func (s byDistanceOrImportPathShortLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#) Lessscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#Less().(i, j int) bool { |
| 1370 | di, dj := s[i].distancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#distance., s[j].distancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#distance. |
| 1371 | if di == -1 { |
| 1372 | return false |
| 1373 | } |
| 1374 | if dj == -1 { |
| 1375 | return true |
| 1376 | } |
| 1377 | if di != dj { |
| 1378 | return di < dj |
| 1379 | } |
| 1380 | |
| 1381 | vi, vj := s[i].pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort., s[j].pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort. |
| 1382 | if len(vi) != len(vj) { |
| 1383 | return len(vi) < len(vj) |
| 1384 | } |
| 1385 | return vi < vj |
| 1386 | } |
| 1387 | func (s byDistanceOrImportPathShortLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#) Swapscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#Swap().(i, j int) { s[i], s[j] = s[j], s[i] } |
| 1388 | |
| 1389 | func distancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/distance().(basepath, targetpath string) int { |
| 1390 | p, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(basepath, targetpath) |
| 1391 | if err != nil { |
| 1392 | return -1 |
| 1393 | } |
| 1394 | if p == "." { |
| 1395 | return 0 |
| 1396 | } |
| 1397 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Countscip-go gomod github.com/golang/go/src go1.25.0 strings/Count().(p, string(filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Separatorscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Separator.)) + 1 |
| 1398 | } |
| 1399 | |
| 1400 | func (r *gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#) scanscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scan().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, callback *scanCallbackscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#) error { |
| 1401 | add := func(root gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, dir string) { |
| 1402 | // We assume cached directories have not changed. We can skip them and their |
| 1403 | // children. |
| 1404 | if _, ok := r.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache..Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Load().(dir); ok { |
| 1405 | return |
| 1406 | } |
| 1407 | |
| 1408 | importpath := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(dir[len(root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.)+len("/"):]) |
| 1409 | info := directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 1410 | statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status.: directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned., |
| 1411 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir.: dir, |
| 1412 | rootTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#rootType.: root.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type., |
| 1413 | nonCanonicalImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#nonCanonicalImportPath.: VendorlessPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/VendorlessPath().(importpath), |
| 1414 | } |
| 1415 | r.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache..Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Store().(dir, info) |
| 1416 | } |
| 1417 | processDir := func(info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) { |
| 1418 | // Skip this directory if we were not able to get the package information successfully. |
| 1419 | if scanned, err := info.reachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#reachedStatus().(directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned.); !scanned || err != nil { |
| 1420 | return |
| 1421 | } |
| 1422 | |
| 1423 | p := &pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#{ |
| 1424 | importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.: info.nonCanonicalImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#nonCanonicalImportPath., |
| 1425 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.: info.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir., |
| 1426 | relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance.: MaxRelevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/MaxRelevance. - 1, |
| 1427 | } |
| 1428 | if info.rootTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#rootType. == gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.RootGOROOTscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOROOT. { |
| 1429 | p.relevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#relevance. = MaxRelevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/MaxRelevance. |
| 1430 | } |
| 1431 | |
| 1432 | if !callback.dirFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#dirFound.(p) { |
| 1433 | return |
| 1434 | } |
| 1435 | var err error |
| 1436 | p.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#packageName., err = r.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache..CachePackageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#CachePackageName().(info) |
| 1437 | if err != nil { |
| 1438 | return |
| 1439 | } |
| 1440 | |
| 1441 | if !callback.packageNameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#packageNameLoaded.(p) { |
| 1442 | return |
| 1443 | } |
| 1444 | if _, exports, err := r.loadExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#loadExports().(ctx, p, false); err == nil { |
| 1445 | callback.exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#exportsLoaded.(p, exports) |
| 1446 | } |
| 1447 | } |
| 1448 | stop := r.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache..ScanAndListenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#ScanAndListen().(ctx, processDir) |
| 1449 | defer stop() |
| 1450 | |
| 1451 | goenv, err := r.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env..goEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#goEnv().() |
| 1452 | if err != nil { |
| 1453 | return err |
| 1454 | } |
| 1455 | var roots []gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root# |
| 1456 | roots = append(roots, gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#{Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.: filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(goenv["GOROOT"], "src"), Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type.: gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.RootGOROOTscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOROOT.}) |
| 1457 | for _, p := range filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.SplitListscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/SplitList().(goenv["GOPATH"]) { |
| 1458 | roots = append(roots, gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#{Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.: filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(p, "src"), Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Type.: gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.RootGOPATHscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootGOPATH.}) |
| 1459 | } |
| 1460 | // The callback is not necessarily safe to use in the goroutine below. Process roots eagerly. |
| 1461 | roots = filterRootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/filterRoots().(roots, callback.rootFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/scanCallback#rootFound.) |
| 1462 | // We can't cancel walks, because we need them to finish to have a usable |
| 1463 | // cache. Instead, run them in a separate goroutine and detach. |
| 1464 | scanDone := make(chan struct{}) |
| 1465 | go func() { |
| 1466 | select { |
| 1467 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 1468 | return |
| 1469 | case <-r.scanSemascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scanSema.: |
| 1470 | } |
| 1471 | defer func() { r.scanSemascip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scanSema. <- struct{}{} }() |
| 1472 | gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Walkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Walk().(roots, add, gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Optionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#{Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#Logf.: r.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env..Logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#Logf., ModulesEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#ModulesEnabled.: false}) |
| 1473 | close(scanDone) |
| 1474 | }() |
| 1475 | select { |
| 1476 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 1477 | case <-scanDone: |
| 1478 | } |
| 1479 | return nil |
| 1480 | } |
| 1481 | |
| 1482 | func (r *gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#) scoreImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#scoreImportPath().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, path string) float64 { |
| 1483 | if stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.HasPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/HasPackage().(path) { |
| 1484 | return MaxRelevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/MaxRelevance. |
| 1485 | } |
| 1486 | return MaxRelevancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/MaxRelevance. - 1 |
| 1487 | } |
| 1488 | |
| 1489 | func filterRootsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/filterRoots().(roots []gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#, include func(gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#) bool) []gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root# { |
| 1490 | var result []gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.Rootscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root# |
| 1491 | for _, root := range roots { |
| 1492 | if !include(root) { |
| 1493 | continue |
| 1494 | } |
| 1495 | result = append(result, root) |
| 1496 | } |
| 1497 | return result |
| 1498 | } |
| 1499 | |
| 1500 | func (r *gopathResolverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#) loadExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#loadExports().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, includeTest bool) (string, []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#, error) { |
| 1501 | if info, ok := r.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache..Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Load().(pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.); ok && !includeTest { |
| 1502 | return r.cachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#cache..CacheExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#CacheExports().(ctx, r.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env., info) |
| 1503 | } |
| 1504 | return loadExportsFromFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/loadExportsFromFiles().(ctx, r.envscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/gopathResolver#env., pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir., includeTest) |
| 1505 | } |
| 1506 | |
| 1507 | // VendorlessPath returns the devendorized version of the import path ipath. |
| 1508 | // For example, VendorlessPath("foo/bar/vendor/a/b") returns "a/b". |
| 1509 | func VendorlessPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/VendorlessPath().(ipath string) string { |
| 1510 | // Devendorize for use in import statement. |
| 1511 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(ipath, "/vendor/"); i >= 0 { |
| 1512 | return ipath[i+len("/vendor/"):] |
| 1513 | } |
| 1514 | if stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasPrefixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasPrefix().(ipath, "vendor/") { |
| 1515 | return ipath[len("vendor/"):] |
| 1516 | } |
| 1517 | return ipath |
| 1518 | } |
| 1519 | |
| 1520 | func loadExportsFromFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/loadExportsFromFiles().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, env *ProcessEnvscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#, dir string, includeTest bool) (string, []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#, error) { |
| 1521 | // Look for non-test, buildable .go files which could provide exports. |
| 1522 | all, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.ReadDirscip-go gomod github.com/golang/go/src go1.25.0 os/ReadDir().(dir) |
| 1523 | if err != nil { |
| 1524 | return "", nil, err |
| 1525 | } |
| 1526 | var files []fsscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/.DirEntryscip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry# |
| 1527 | for _, fi := range all { |
| 1528 | name := fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().() |
| 1529 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(name, ".go") || (!includeTest && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(name, "_test.go")) { |
| 1530 | continue |
| 1531 | } |
| 1532 | match, err := env.matchFilescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#matchFile().(dir, fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()) |
| 1533 | if err != nil || !match { |
| 1534 | continue |
| 1535 | } |
| 1536 | files = append(files, fi) |
| 1537 | } |
| 1538 | |
| 1539 | if len(files) == 0 { |
| 1540 | return "", nil, fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("dir %v contains no buildable, non-test .go files", dir) |
| 1541 | } |
| 1542 | |
| 1543 | var pkgName string |
| 1544 | var exports []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol# |
| 1545 | fset := tokenscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/.NewFileSetscip-go gomod github.com/golang/go/src go1.25.0 `go/token`/NewFileSet().() |
| 1546 | for _, fi := range files { |
| 1547 | select { |
| 1548 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 1549 | return "", nil, ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() |
| 1550 | default: |
| 1551 | } |
| 1552 | |
| 1553 | fullFile := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Joinscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Join().(dir, fi.Namescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/DirEntry#Name().()) |
| 1554 | // Legacy ast.Object resolution is needed here. |
| 1555 | f, err := parserscip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/.ParseFilescip-go gomod github.com/golang/go/src go1.25.0 `go/parser`/ParseFile().(fset, fullFile, nil, 0) |
| 1556 | if err != nil { |
| 1557 | env.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf().("error parsing %v: %v", fullFile, err) |
| 1558 | continue |
| 1559 | } |
| 1560 | if f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. == "documentation" { |
| 1561 | // Special case from go/build.ImportDir, not |
| 1562 | // handled by MatchFile above. |
| 1563 | continue |
| 1564 | } |
| 1565 | if includeTest && stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name., "_test") { |
| 1566 | // x_test package. We want internal test files only. |
| 1567 | continue |
| 1568 | } |
| 1569 | pkgName = f.Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Name..Namescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Ident#Name. |
| 1570 | for name, obj := range f.Scopescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/File#Scope..Objectsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Scope#Objects. { |
| 1571 | if astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.IsExportedscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/IsExported().(name) { |
| 1572 | var kind stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Kind# |
| 1573 | switch obj.Kindscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Object#Kind. { |
| 1574 | case astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Conscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Con.: |
| 1575 | kind = stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Const. |
| 1576 | case astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Typscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Typ.: |
| 1577 | kind = stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Type. |
| 1578 | case astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Varscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Var.: |
| 1579 | kind = stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Var. |
| 1580 | case astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Funscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Fun.: |
| 1581 | kind = stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Func. |
| 1582 | } |
| 1583 | exports = append(exports, stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#{ |
| 1584 | Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Name.: name, |
| 1585 | Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Kind.: kind, |
| 1586 | Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Version.: 0, // unknown; be permissive |
| 1587 | }) |
| 1588 | } |
| 1589 | } |
| 1590 | } |
| 1591 | sortSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortSymbols().(exports) |
| 1592 | |
| 1593 | env.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ProcessEnv#logf().("loaded exports in dir %v (package %v): %v", dir, pkgName, exports) |
| 1594 | return pkgName, exports, nil |
| 1595 | } |
| 1596 | |
| 1597 | func sortSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/sortSymbols().(syms []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#) { |
| 1598 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Slicescip-go gomod github.com/golang/go/src go1.25.0 sort/Slice().(syms, func(i, j int) bool { |
| 1599 | return syms[i].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Name. < syms[j].Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Name. |
| 1600 | }) |
| 1601 | } |
| 1602 | |
| 1603 | // A symbolSearcher searches for a package with a set of symbols, among a set |
| 1604 | // of candidates. See [symbolSearcher.search]. |
| 1605 | // |
| 1606 | // The search occurs within the scope of a single file, with context captured |
| 1607 | // in srcDir and xtest. |
| 1608 | type symbolSearcherscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher# struct { |
| 1609 | logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf. func(string, ...any) |
| 1610 | srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#srcDir. string // directory containing the file |
| 1611 | xtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#xtest. bool // if set, the file containing is an x_test file |
| 1612 | loadExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#loadExports. func(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, includeTest bool) (string, []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#, error) |
| 1613 | } |
| 1614 | |
| 1615 | // search searches the provided candidates for a package containing all |
| 1616 | // exported symbols. |
| 1617 | // |
| 1618 | // If successful, returns the resulting package. |
| 1619 | func (s *symbolSearcherscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#) searchscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#search().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, candidates []pkgDistancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#, pkgName string, symbols map[string]bool) (*pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, error) { |
| 1620 | // Sort the candidates by their import package length, |
| 1621 | // assuming that shorter package names are better than long |
| 1622 | // ones. Note that this sorts by the de-vendored name, so |
| 1623 | // there's no "penalty" for vendoring. |
| 1624 | sortscip-go gomod github.com/golang/go/src go1.25.0 sort/.Sortscip-go gomod github.com/golang/go/src go1.25.0 sort/Sort().(byDistanceOrImportPathShortLengthscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/byDistanceOrImportPathShortLength#(candidates)) |
| 1625 | if s.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf. != nil { |
| 1626 | for i, c := range candidates { |
| 1627 | s.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf.("%s candidate %d/%d: %v in %v", pkgName, i+1, len(candidates), c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort., c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.) |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | // Arrange rescv so that we can we can await results in order of relevance |
| 1632 | // and exit as soon as we find the first match. |
| 1633 | // |
| 1634 | // Search with bounded concurrency, returning as soon as the first result |
| 1635 | // among rescv is non-nil. |
| 1636 | rescv := make([]chan *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, len(candidates)) |
| 1637 | for i := range candidates { |
| 1638 | rescv[i] = make(chan *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, 1) |
| 1639 | } |
| 1640 | const maxConcurrentPackageImport = 4 |
| 1641 | loadExportsSem := make(chan struct{}, maxConcurrentPackageImport) |
| 1642 | |
| 1643 | // Ensure that all work is completed at exit. |
| 1644 | ctx, cancel := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithCancelscip-go gomod github.com/golang/go/src go1.25.0 context/WithCancel().(ctx) |
| 1645 | var wg syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup# |
| 1646 | defer func() { |
| 1647 | cancel() |
| 1648 | wg.Waitscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Wait().() |
| 1649 | }() |
| 1650 | |
| 1651 | // Start the search. |
| 1652 | wg.Goscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Go().(func() { |
| 1653 | for i, c := range candidates { |
| 1654 | select { |
| 1655 | case loadExportsSem <- struct{}{}: |
| 1656 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 1657 | return |
| 1658 | } |
| 1659 | |
| 1660 | i := i |
| 1661 | c := c |
| 1662 | wg.Addscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Add().(1) |
| 1663 | go func() { |
| 1664 | defer func() { |
| 1665 | <-loadExportsSem |
| 1666 | wg.Donescip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Done().() |
| 1667 | }() |
| 1668 | if s.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf. != nil { |
| 1669 | s.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf.("loading exports in dir %s (seeking package %s)", c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir., pkgName) |
| 1670 | } |
| 1671 | pkg, err := s.searchOnescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#searchOne().(ctx, c, symbols) |
| 1672 | if err != nil { |
| 1673 | if s.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf. != nil && ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() == nil { |
| 1674 | s.logfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#logf.("loading exports in dir %s (seeking package %s): %v", c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir., pkgName, err) |
| 1675 | } |
| 1676 | pkg = nil |
| 1677 | } |
| 1678 | rescv[i] <- pkg // may be nil |
| 1679 | }() |
| 1680 | } |
| 1681 | }) |
| 1682 | |
| 1683 | // Await the first (best) result. |
| 1684 | for _, resc := range rescv { |
| 1685 | select { |
| 1686 | case r := <-resc: |
| 1687 | if r != nil { |
| 1688 | return r, nil |
| 1689 | } |
| 1690 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 1691 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() |
| 1692 | } |
| 1693 | } |
| 1694 | return nil, nil |
| 1695 | } |
| 1696 | |
| 1697 | func (s *symbolSearcherscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#) searchOnescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#searchOne().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, c pkgDistancescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#, symbols map[string]bool) (*pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#, error) { |
| 1698 | if ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() != nil { |
| 1699 | return nil, ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() |
| 1700 | } |
| 1701 | // If we're considering the package under test from an x_test, load the |
| 1702 | // test variant. |
| 1703 | includeTest := s.xtestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#xtest. && c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg..dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir. == s.srcDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#srcDir. |
| 1704 | _, exports, err := s.loadExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolSearcher#loadExports.(ctx, c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg., includeTest) |
| 1705 | if err != nil { |
| 1706 | return nil, err |
| 1707 | } |
| 1708 | |
| 1709 | exportsMap := make(map[string]bool, len(exports)) |
| 1710 | for _, sym := range exports { |
| 1711 | exportsMap[sym.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Name.] = true |
| 1712 | } |
| 1713 | for symbol := range symbols { |
| 1714 | if !exportsMap[symbol] { |
| 1715 | return nil, nil // no match |
| 1716 | } |
| 1717 | } |
| 1718 | return c.pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgDistance#pkg., nil |
| 1719 | } |
| 1720 | |
| 1721 | // pkgIsCandidate reports whether pkg is a candidate for satisfying the |
| 1722 | // finding which package pkgIdent in the file named by filename is trying |
| 1723 | // to refer to. |
| 1724 | // |
| 1725 | // This check is purely lexical and is meant to be as fast as possible |
| 1726 | // because it's run over all $GOPATH directories to filter out poor |
| 1727 | // candidates in order to limit the CPU and I/O later parsing the |
| 1728 | // exports in candidate packages. |
| 1729 | // |
| 1730 | // filename is the file being formatted. |
| 1731 | // pkgIdent is the package being searched for, like "client" (if |
| 1732 | // searching for "client.New") |
| 1733 | func pkgIsCandidatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkgIsCandidate().(filename string, refs Referencesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/References#, pkg *pkgscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#) bool { |
| 1734 | // Check "internal" and "vendor" visibility: |
| 1735 | if !CanUsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/CanUse().(filename, pkg.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#dir.) { |
| 1736 | return false |
| 1737 | } |
| 1738 | |
| 1739 | // Speed optimization to minimize disk I/O: |
| 1740 | // |
| 1741 | // Use the matchesPath heuristic to filter to package paths that could |
| 1742 | // reasonably match a dangling reference. |
| 1743 | // |
| 1744 | // This permits mismatch naming like directory "go-foo" being package "foo", |
| 1745 | // or "pkg.v3" being "pkg", or directory |
| 1746 | // "google.golang.org/api/cloudbilling/v1" being package "cloudbilling", but |
| 1747 | // doesn't permit a directory "foo" to be package "bar", which is strongly |
| 1748 | // discouraged anyway. There's no reason goimports needs to be slow just to |
| 1749 | // accommodate that. |
| 1750 | for pkgIdent := range refs { |
| 1751 | if matchesPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/matchesPath().(pkgIdent, pkg.importPathShortscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/pkg#importPathShort.) { |
| 1752 | return true |
| 1753 | } |
| 1754 | } |
| 1755 | return false |
| 1756 | } |
| 1757 | |
| 1758 | // CanUse reports whether the package in dir is usable from filename, |
| 1759 | // respecting the Go "internal" and "vendor" visibility rules. |
| 1760 | func CanUsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/CanUse().(filename, dir string) bool { |
| 1761 | // Fast path check, before any allocations. If it doesn't contain vendor |
| 1762 | // or internal, it's not tricky: |
| 1763 | // Note that this can false-negative on directories like "notinternal", |
| 1764 | // but we check it correctly below. This is just a fast path. |
| 1765 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(dir, "vendor") && !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(dir, "internal") { |
| 1766 | return true |
| 1767 | } |
| 1768 | |
| 1769 | dirSlash := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(dir) |
| 1770 | if !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(dirSlash, "/vendor/") && !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(dirSlash, "/internal/") && !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(dirSlash, "/internal") { |
| 1771 | return true |
| 1772 | } |
| 1773 | // Vendor or internal directory only visible from children of parent. |
| 1774 | // That means the path from the current directory to the target directory |
| 1775 | // can contain ../vendor or ../internal but not ../foo/vendor or ../foo/internal |
| 1776 | // or bar/vendor or bar/internal. |
| 1777 | // After stripping all the leading ../, the only okay place to see vendor or internal |
| 1778 | // is at the very beginning of the path. |
| 1779 | absfile, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(filename) |
| 1780 | if err != nil { |
| 1781 | return false |
| 1782 | } |
| 1783 | absdir, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Absscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Abs().(dir) |
| 1784 | if err != nil { |
| 1785 | return false |
| 1786 | } |
| 1787 | rel, err := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.Relscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Rel().(absfile, absdir) |
| 1788 | if err != nil { |
| 1789 | return false |
| 1790 | } |
| 1791 | relSlash := filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/.ToSlashscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/ToSlash().(rel) |
| 1792 | if i := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.LastIndexscip-go gomod github.com/golang/go/src go1.25.0 strings/LastIndex().(relSlash, "../"); i >= 0 { |
| 1793 | relSlash = relSlash[i+len("../"):] |
| 1794 | } |
| 1795 | return !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(relSlash, "/vendor/") && !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Containsscip-go gomod github.com/golang/go/src go1.25.0 strings/Contains().(relSlash, "/internal/") && !stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.HasSuffixscip-go gomod github.com/golang/go/src go1.25.0 strings/HasSuffix().(relSlash, "/internal") |
| 1796 | } |
| 1797 | |
| 1798 | // matchesPath reports whether ident may match a potential package name |
| 1799 | // referred to by path, using heuristics to filter out unidiomatic package |
| 1800 | // names. |
| 1801 | // |
| 1802 | // Specifically, it checks whether either of the last two '/'- or '\'-delimited |
| 1803 | // path segments matches the identifier. The segment-matching heuristic must |
| 1804 | // allow for various conventions around segment naming, including go-foo, |
| 1805 | // foo-go, and foo.v3. To handle all of these, matching considers both (1) the |
| 1806 | // entire segment, ignoring '-' and '.', as well as (2) the last subsegment |
| 1807 | // separated by '-' or '.'. So the segment foo-go matches all of the following |
| 1808 | // identifiers: foo, go, and foogo. All matches are case insensitive (for ASCII |
| 1809 | // identifiers). |
| 1810 | // |
| 1811 | // See the docstring for [pkgIsCandidate] for an explanation of how this |
| 1812 | // heuristic filters potential candidate packages. |
| 1813 | func matchesPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/matchesPath().(ident, path string) bool { |
| 1814 | // Ignore case, for ASCII. |
| 1815 | lowerIfASCII := func(b byte) byte { |
| 1816 | if 'A' <= b && b <= 'Z' { |
| 1817 | return b + ('a' - 'A') |
| 1818 | } |
| 1819 | return b |
| 1820 | } |
| 1821 | |
| 1822 | // match reports whether path[start:end] matches ident, ignoring [.-]. |
| 1823 | match := func(start, end int) bool { |
| 1824 | ii := len(ident) - 1 // current byte in ident |
| 1825 | pi := end - 1 // current byte in path |
| 1826 | for ; pi >= start && ii >= 0; pi-- { |
| 1827 | pb := path[pi] |
| 1828 | if pb == '-' || pb == '.' { |
| 1829 | continue |
| 1830 | } |
| 1831 | pb = lowerIfASCII(pb) |
| 1832 | ib := lowerIfASCII(ident[ii]) |
| 1833 | if pb != ib { |
| 1834 | return false |
| 1835 | } |
| 1836 | ii-- |
| 1837 | } |
| 1838 | return ii < 0 && pi < start // all bytes matched |
| 1839 | } |
| 1840 | |
| 1841 | // segmentEnd and subsegmentEnd hold the end points of the current segment |
| 1842 | // and subsegment intervals. |
| 1843 | segmentEnd := len(path) |
| 1844 | subsegmentEnd := len(path) |
| 1845 | |
| 1846 | // Count slashes; we only care about the last two segments. |
| 1847 | nslash := 0 |
| 1848 | |
| 1849 | for i := len(path) - 1; i >= 0; i-- { |
| 1850 | switch b := path[i]; b { |
| 1851 | // TODO(rfindley): we handle backlashes here only because the previous |
| 1852 | // heuristic handled backslashes. This is perhaps overly defensive, but is |
| 1853 | // the result of many lessons regarding Chesterton's fence and the |
| 1854 | // goimports codebase. |
| 1855 | // |
| 1856 | // However, this function is only ever called with something called an |
| 1857 | // 'importPath'. Is it possible that this is a real import path, and |
| 1858 | // therefore we need only consider forward slashes? |
| 1859 | case '/', '\\': |
| 1860 | if match(i+1, segmentEnd) || match(i+1, subsegmentEnd) { |
| 1861 | return true |
| 1862 | } |
| 1863 | nslash++ |
| 1864 | if nslash == 2 { |
| 1865 | return false // did not match above |
| 1866 | } |
| 1867 | segmentEnd, subsegmentEnd = i, i // reset |
| 1868 | case '-', '.': |
| 1869 | if match(i+1, subsegmentEnd) { |
| 1870 | return true |
| 1871 | } |
| 1872 | subsegmentEnd = i |
| 1873 | } |
| 1874 | } |
| 1875 | return match(0, segmentEnd) || match(0, subsegmentEnd) |
| 1876 | } |
| 1877 | |
| 1878 | type visitFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/visitFn# func(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Visitorscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Visitor# |
| 1879 | |
| 1880 | func (fn visitFnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/visitFn#) Visitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/visitFn#Visit().(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Visitorscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Visitor# { |
| 1881 | return fn(node) |
| 1882 | } |
| 1883 | |
| 1884 | func symbolNameSetscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/symbolNameSet().(symbols []stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Symbolscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#) map[string]bool { |
| 1885 | names := make(map[string]bool) |
| 1886 | for _, sym := range symbols { |
| 1887 | switch sym.Kindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Kind. { |
| 1888 | case stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Constscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Const., stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Varscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Var., stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Typescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Type., stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/.Funcscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Func.: |
| 1889 | names[sym.Namescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/Symbol#Name.] = true |
| 1890 | } |
| 1891 | } |
| 1892 | return names |
| 1893 | } |