| 1 | // Copyright 2019 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 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 9 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "pathscip-go gomod github.com/golang/go/src go1.25.0 path/" |
| 11 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 12 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 13 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 14 | |
| 15 | "golang.org/x/mod/modulescip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/module`/" |
| 16 | "golang.org/x/tools/internal/gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/" |
| 17 | "golang.org/x/tools/internal/stdlibscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/stdlib`/" |
| 18 | ) |
| 19 | |
| 20 | // To find packages to import, the resolver needs to know about all of |
| 21 | // the packages that could be imported. This includes packages that are |
| 22 | // already in modules that are in (1) the current module, (2) replace targets, |
| 23 | // and (3) packages in the module cache. Packages in (1) and (2) may change over |
| 24 | // time, as the client may edit the current module and locally replaced modules. |
| 25 | // The module cache (which includes all of the packages in (3)) can only |
| 26 | // ever be added to. |
| 27 | // |
| 28 | // The resolver can thus save state about packages in the module cache |
| 29 | // and guarantee that this will not change over time. To obtain information |
| 30 | // about new modules added to the module cache, the module cache should be |
| 31 | // rescanned. |
| 32 | // |
| 33 | // It is OK to serve information about modules that have been deleted, |
| 34 | // as they do still exist. |
| 35 | // TODO(suzmue): can we share information with the caller about |
| 36 | // what module needs to be downloaded to import this package? |
| 37 | |
| 38 | type directoryPackageStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageStatus# int |
| 39 | |
| 40 | const ( |
| 41 | _ directoryPackageStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageStatus# = iota |
| 42 | directoryScannedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryScanned. |
| 43 | nameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/nameLoaded. |
| 44 | exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/exportsLoaded. |
| 45 | ) |
| 46 | |
| 47 | // directoryPackageInfo holds (possibly incomplete) information about packages |
| 48 | // contained in a given directory. |
| 49 | type directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo# struct { |
| 50 | // status indicates the extent to which this struct has been filled in. |
| 51 | statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. directoryPackageStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageStatus# |
| 52 | // err is non-nil when there was an error trying to reach status. |
| 53 | errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. error |
| 54 | |
| 55 | // Set when status >= directoryScanned. |
| 56 | |
| 57 | // dir is the absolute directory of this package. |
| 58 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir. string |
| 59 | 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`/.RootTypescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootType# |
| 60 | // nonCanonicalImportPath is the package's expected import path. It may |
| 61 | // not actually be importable at that path. |
| 62 | nonCanonicalImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#nonCanonicalImportPath. string |
| 63 | |
| 64 | // Module-related information. |
| 65 | moduleDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#moduleDir. string // The directory that is the module root of this dir. |
| 66 | moduleNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#moduleName. string // The module name that contains this dir. |
| 67 | |
| 68 | // Set when status >= nameLoaded. |
| 69 | |
| 70 | packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName. string // the package name, as declared in the source. |
| 71 | |
| 72 | // Set when status >= exportsLoaded. |
| 73 | // TODO(rfindley): it's hard to see this, but exports depend implicitly on |
| 74 | // the default build context GOOS and GOARCH. |
| 75 | // |
| 76 | // We can make this explicit, and key exports by GOOS, GOARCH. |
| 77 | exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#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# |
| 78 | } |
| 79 | |
| 80 | // reachedStatus returns true when info has a status at least target and any error associated with |
| 81 | // an attempt to reach target. |
| 82 | func (info *directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) reachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#reachedStatus().(target directoryPackageStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageStatus#) (bool, error) { |
| 83 | if info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. == nil { |
| 84 | return info.statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. >= target, nil |
| 85 | } |
| 86 | if info.statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. == target { |
| 87 | return true, info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. |
| 88 | } |
| 89 | return true, nil |
| 90 | } |
| 91 | |
| 92 | // DirInfoCache is a concurrency-safe map for storing information about |
| 93 | // directories that may contain packages. |
| 94 | // |
| 95 | // The information in this cache is built incrementally. Entries are initialized in scan. |
| 96 | // No new keys should be added in any other functions, as all directories containing |
| 97 | // packages are identified in scan. |
| 98 | // |
| 99 | // Other functions, including loadExports and findPackage, may update entries in this cache |
| 100 | // as they discover new things about the directory. |
| 101 | // |
| 102 | // The information in the cache is not expected to change for the cache's |
| 103 | // lifetime, so there is no protection against competing writes. Users should |
| 104 | // take care not to hold the cache across changes to the underlying files. |
| 105 | type DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache# struct { |
| 106 | muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#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# |
| 107 | // dirs stores information about packages in directories, keyed by absolute path. |
| 108 | dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs. map[string]*directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo# |
| 109 | listenersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#listeners. map[*int]cacheListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/cacheListener# |
| 110 | } |
| 111 | |
| 112 | func NewDirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/NewDirInfoCache().() *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache# { |
| 113 | return &DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#{ |
| 114 | dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs.: make(map[string]*directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#), |
| 115 | listenersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#listeners.: make(map[*int]cacheListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/cacheListener#), |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | type cacheListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/cacheListener# func(directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) |
| 120 | |
| 121 | // ScanAndListen calls listener on all the items in the cache, and on anything |
| 122 | // newly added. The returned stop function waits for all in-flight callbacks to |
| 123 | // finish and blocks new ones. |
| 124 | func (d *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#) ScanAndListenscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#ScanAndListen().(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#, listener cacheListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/cacheListener#) func() { |
| 125 | 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) |
| 126 | |
| 127 | // Flushing out all the callbacks is tricky without knowing how many there |
| 128 | // are going to be. Setting an arbitrary limit makes it much easier. |
| 129 | const maxInFlight = 10 |
| 130 | sema := make(chan struct{}, maxInFlight) |
| 131 | for range maxInFlight { |
| 132 | sema <- struct{}{} |
| 133 | } |
| 134 | |
| 135 | cookie := new(int) // A unique ID we can use for the listener. |
| 136 | |
| 137 | // We can't hold mu while calling the listener. |
| 138 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 139 | var keys []string |
| 140 | for key := range d.dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs. { |
| 141 | keys = append(keys, key) |
| 142 | } |
| 143 | d.listenersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#listeners.[cookie] = func(info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) { |
| 144 | select { |
| 145 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 146 | return |
| 147 | case <-sema: |
| 148 | } |
| 149 | listener(info) |
| 150 | sema <- struct{}{} |
| 151 | } |
| 152 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 153 | |
| 154 | stop := func() { |
| 155 | cancel() |
| 156 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 157 | delete(d.listenersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#listeners., cookie) |
| 158 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 159 | for range maxInFlight { |
| 160 | <-sema |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Process the pre-existing keys. |
| 165 | for _, k := range keys { |
| 166 | select { |
| 167 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 168 | return stop |
| 169 | default: |
| 170 | } |
| 171 | if v, ok := d.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Load().(k); ok { |
| 172 | listener(v) |
| 173 | } |
| 174 | } |
| 175 | |
| 176 | return stop |
| 177 | } |
| 178 | |
| 179 | // Store stores the package info for dir. |
| 180 | func (d *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#) Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Store().(dir string, info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) { |
| 181 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 182 | // TODO(rfindley, golang/go#59216): should we overwrite an existing entry? |
| 183 | // That seems incorrect as the cache should be idempotent. |
| 184 | _, old := d.dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs.[dir] |
| 185 | d.dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs.[dir] = &info |
| 186 | var listeners []cacheListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/cacheListener# |
| 187 | for _, l := range d.listenersscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#listeners. { |
| 188 | listeners = append(listeners, l) |
| 189 | } |
| 190 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 191 | |
| 192 | if !old { |
| 193 | for _, l := range listeners { |
| 194 | l(info) |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // Load returns a copy of the directoryPackageInfo for absolute directory dir. |
| 200 | func (d *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#) Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Load().(dir string) (directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#, bool) { |
| 201 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 202 | defer d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 203 | info, ok := d.dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs.[dir] |
| 204 | if !ok { |
| 205 | return directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{}, false |
| 206 | } |
| 207 | return *info, true |
| 208 | } |
| 209 | |
| 210 | // Keys returns the keys currently present in d. |
| 211 | func (d *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#) Keysscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Keys().() (keys []string) { |
| 212 | d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 213 | defer d.muscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#mu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 214 | for key := range d.dirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#dirs. { |
| 215 | keys = append(keys, key) |
| 216 | } |
| 217 | return keys |
| 218 | } |
| 219 | |
| 220 | func (d *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#) CachePackageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#CachePackageName().(info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) (string, error) { |
| 221 | if loaded, err := info.reachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#reachedStatus().(nameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/nameLoaded.); loaded { |
| 222 | return info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., err |
| 223 | } |
| 224 | 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 { |
| 225 | 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().("cannot read package name, scan error: %v", err) |
| 226 | } |
| 227 | info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. = packageDirToNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/packageDirToName().(info.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir.) |
| 228 | info.statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. = nameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/nameLoaded. |
| 229 | d.Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Store().(info.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir., info) |
| 230 | return info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. |
| 231 | } |
| 232 | |
| 233 | func (d *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#) CacheExportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#CacheExports().(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#, info directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#) (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) { |
| 234 | if reached, _ := info.reachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#reachedStatus().(exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/exportsLoaded.); reached { |
| 235 | return info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., info.exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#exports., info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. |
| 236 | } |
| 237 | if reached, err := info.reachedStatusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#reachedStatus().(nameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/nameLoaded.); reached && err != nil { |
| 238 | return "", nil, err |
| 239 | } |
| 240 | info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., info.exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#exports., info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. = loadExportsFromFilesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/loadExportsFromFiles().(ctx, env, info.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir., false) |
| 241 | if info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. == contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Canceledscip-go gomod github.com/golang/go/src go1.25.0 context/Canceled. || info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. == contextscip-go gomod github.com/golang/go/src go1.25.0 context/.DeadlineExceededscip-go gomod github.com/golang/go/src go1.25.0 context/DeadlineExceeded. { |
| 242 | return info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., info.exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#exports., info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. |
| 243 | } |
| 244 | // The cache structure wants things to proceed linearly. We can skip a |
| 245 | // step here, but only if we succeed. |
| 246 | if info.statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. == nameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/nameLoaded. || info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. == nil { |
| 247 | info.statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. = exportsLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/exportsLoaded. |
| 248 | } else { |
| 249 | info.statusscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#status. = nameLoadedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/nameLoaded. |
| 250 | } |
| 251 | d.Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Store().(info.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir., info) |
| 252 | return info.packageNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#packageName., info.exportsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#exports., info.errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err. |
| 253 | } |
| 254 | |
| 255 | // ScanModuleCache walks the given directory, which must be a GOMODCACHE value, |
| 256 | // for directory package information, storing the results in cache. |
| 257 | func ScanModuleCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/ScanModuleCache().(dir string, cache *DirInfoCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#, logf func(string, ...any)) { |
| 258 | // Note(rfindley): it's hard to see, but this function attempts to implement |
| 259 | // just the side effects on cache of calling PrimeCache with a ProcessEnv |
| 260 | // that has the given dir as its GOMODCACHE. |
| 261 | // |
| 262 | // Teasing out the control flow, we see that we can avoid any handling of |
| 263 | // vendor/ and can infer module info entirely from the path, simplifying the |
| 264 | // logic here. |
| 265 | |
| 266 | 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#{ |
| 267 | 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`/.Cleanscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/Clean().(dir), |
| 268 | 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`/.RootModuleCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootModuleCache., |
| 269 | } |
| 270 | |
| 271 | directoryInfo := 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) directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo# { |
| 272 | // This is a copy of ModuleResolver.scanDirForPackage, trimmed down to |
| 273 | // logic that applies to a module cache directory. |
| 274 | |
| 275 | subdir := "" |
| 276 | if dir != root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path. { |
| 277 | subdir = dir[len(root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path.)+len("/"):] |
| 278 | } |
| 279 | |
| 280 | matches := modCacheRegexpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/modCacheRegexp..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindStringSubmatch().(subdir) |
| 281 | if len(matches) == 0 { |
| 282 | return directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 283 | 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., |
| 284 | errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err.: 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().("invalid module cache path: %v", subdir), |
| 285 | } |
| 286 | } |
| 287 | modPath, err := modulescip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/module`/.UnescapePathscip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/module`/UnescapePath().(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().(matches[1])) |
| 288 | if err != nil { |
| 289 | if logf != nil { |
| 290 | logf("decoding module cache path %q: %v", subdir, err) |
| 291 | } |
| 292 | return directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 293 | 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., |
| 294 | errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#err.: 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().("decoding module cache path %q: %v", subdir, err), |
| 295 | } |
| 296 | } |
| 297 | importPath := 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().(modPath, 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().(matches[3])) |
| 298 | index := stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Indexscip-go gomod github.com/golang/go/src go1.25.0 strings/Index().(dir, matches[1]+"@"+matches[2]) |
| 299 | modDir := 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[:index], matches[1]+"@"+matches[2]) |
| 300 | modName := readModNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/readModName().(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().(modDir, "go.mod")) |
| 301 | return directoryPackageInfoscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#{ |
| 302 | 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., |
| 303 | dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir.: dir, |
| 304 | 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., |
| 305 | nonCanonicalImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#nonCanonicalImportPath.: importPath, |
| 306 | moduleDirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#moduleDir.: modDir, |
| 307 | moduleNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#moduleName.: modName, |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | 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) { |
| 312 | info := directoryInfo(root, dir) |
| 313 | cache.Storescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Store().(info.dirscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/directoryPackageInfo#dir., info) |
| 314 | } |
| 315 | |
| 316 | skip := 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#, dir string) bool { |
| 317 | // Skip directories that have already been scanned. |
| 318 | // |
| 319 | // Note that gopathwalk only adds "package" directories, which must contain |
| 320 | // a .go file, and all such package directories in the module cache are |
| 321 | // immutable. So if we can load a dir, it can be skipped. |
| 322 | info, ok := cache.Loadscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/imports`/DirInfoCache#Load().(dir) |
| 323 | if !ok { |
| 324 | return false |
| 325 | } |
| 326 | packageScanned, _ := 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.) |
| 327 | return packageScanned |
| 328 | } |
| 329 | |
| 330 | gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/.WalkSkipscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/WalkSkip().([]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#{root}, add, skip, 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.: logf, ModulesEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#ModulesEnabled.: true}) |
| 331 | } |