| 1 | // Copyright 2024 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 modindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/ |
| 6 | |
| 7 | import ( |
| 8 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 9 | "logscip-go gomod github.com/golang/go/src go1.25.0 log/" |
| 10 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 11 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 12 | "regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/" |
| 13 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 14 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 15 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 16 | |
| 17 | "golang.org/x/mod/semverscip-go gomod golang.org/x/mod v0.37.0 `golang.org/x/mod/semver`/" |
| 18 | "golang.org/x/tools/internal/gopathwalkscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/" |
| 19 | ) |
| 20 | |
| 21 | type directoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory# struct { |
| 22 | pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#path. string // relative to GOMODCACHE |
| 23 | importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#importPath. string |
| 24 | versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#version. string // semantic version |
| 25 | } |
| 26 | |
| 27 | // bestDirByImportPath returns the best directory for each import |
| 28 | // path, where "best" means most recent semantic version. These import |
| 29 | // paths are inferred from the GOMODCACHE-relative dir names in dirs. |
| 30 | func bestDirByImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/bestDirByImportPath().(dirs []string) (map[string]directoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#, error) { |
| 31 | dirsByPath := make(map[string]directoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#) |
| 32 | for _, dir := range dirs { |
| 33 | importPath, version, err := dirToImportPathVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/dirToImportPathVersion().(dir) |
| 34 | if err != nil { |
| 35 | return nil, err |
| 36 | } |
| 37 | new := directoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#{ |
| 38 | pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#path.: dir, |
| 39 | importPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#importPath.: importPath, |
| 40 | versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#version.: version, |
| 41 | } |
| 42 | if old, ok := dirsByPath[importPath]; !ok || compareDirectoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/compareDirectory().(new, old) < 0 { |
| 43 | dirsByPath[importPath] = new |
| 44 | } |
| 45 | } |
| 46 | return dirsByPath, nil |
| 47 | } |
| 48 | |
| 49 | // compareDirectory defines an ordering of path@version directories, |
| 50 | // by descending version, then by ascending path. |
| 51 | func compareDirectoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/compareDirectory().(x, y directoryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#) int { |
| 52 | if sign := -semverscip-go gomod golang.org/x/mod v0.37.0 `golang.org/x/mod/semver`/.Comparescip-go gomod golang.org/x/mod v0.37.0 `golang.org/x/mod/semver`/Compare().(x.versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#version., y.versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#version.); sign != 0 { |
| 53 | return sign // latest first |
| 54 | } |
| 55 | return stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Comparescip-go gomod github.com/golang/go/src go1.25.0 strings/Compare().(string(x.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#path.), string(y.pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#path.)) |
| 56 | } |
| 57 | |
| 58 | // modCacheRegexp splits a relpathpath into module, module version, and package. |
| 59 | var modCacheRegexpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/modCacheRegexp. = regexpscip-go gomod github.com/golang/go/src go1.25.0 regexp/.MustCompilescip-go gomod github.com/golang/go/src go1.25.0 regexp/MustCompile().(`(.*)@([^/\\]*)(.*)`) |
| 60 | |
| 61 | // dirToImportPathVersion computes import path and semantic version |
| 62 | // from a GOMODCACHE-relative directory name. |
| 63 | func dirToImportPathVersionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/dirToImportPathVersion().(dir string) (string, string, error) { |
| 64 | m := modCacheRegexpscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/modCacheRegexp..FindStringSubmatchscip-go gomod github.com/golang/go/src go1.25.0 regexp/Regexp#FindStringSubmatch().(string(dir)) |
| 65 | // m[1] is the module path |
| 66 | // m[2] is the version major.minor.patch(-<pre release identifier) |
| 67 | // m[3] is the rest of the package path |
| 68 | if len(m) != 4 { |
| 69 | 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().("bad dir %s", dir) |
| 70 | } |
| 71 | if !semverscip-go gomod golang.org/x/mod v0.37.0 `golang.org/x/mod/semver`/.IsValidscip-go gomod golang.org/x/mod v0.37.0 `golang.org/x/mod/semver`/IsValid().(m[2]) { |
| 72 | 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().("bad semantic version %s", m[2]) |
| 73 | } |
| 74 | // ToSlash is required to convert Windows file paths |
| 75 | // into Go package import paths. |
| 76 | return 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().(m[1] + m[3]), m[2], nil |
| 77 | } |
| 78 | |
| 79 | // findDirs returns an unordered list of relevant package directories, |
| 80 | // relative to the specified module cache root. The result includes only |
| 81 | // module dirs whose mtime is within (start, end). |
| 82 | func findDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/findDirs().(root string, start, end timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timescip-go gomod github.com/golang/go/src go1.25.0 time/Time#) []string { |
| 83 | var ( |
| 84 | resMu 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# |
| 85 | res []string |
| 86 | ) |
| 87 | |
| 88 | addDir := 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) { |
| 89 | // TODO(pjw): do we need to check times? |
| 90 | resMu.Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().() |
| 91 | defer resMu.Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().() |
| 92 | res = append(res, relativescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/relative().(root.Pathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Root#Path., dir)) |
| 93 | } |
| 94 | |
| 95 | skipDir := 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 { |
| 96 | // The cache directory is already ignored in gopathwalk. |
| 97 | if 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().(dir) == "internal" { |
| 98 | return true |
| 99 | } |
| 100 | |
| 101 | // Skip toolchains. |
| 102 | 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, "toolchain@") { |
| 103 | return true |
| 104 | } |
| 105 | |
| 106 | // Don't look inside @ directories that are too old/new. |
| 107 | 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().(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().(dir), "@") { |
| 108 | st, err := osscip-go gomod github.com/golang/go/src go1.25.0 os/.Statscip-go gomod github.com/golang/go/src go1.25.0 os/Stat().(dir) |
| 109 | if err != nil { |
| 110 | logscip-go gomod github.com/golang/go/src go1.25.0 log/.Printfscip-go gomod github.com/golang/go/src go1.25.0 log/Printf().("can't stat dir %s %v", dir, err) |
| 111 | return true |
| 112 | } |
| 113 | mtime := st.ModTimescip-go gomod github.com/golang/go/src go1.25.0 `io/fs`/FileInfo#ModTime().() |
| 114 | return mtime.Beforescip-go gomod github.com/golang/go/src go1.25.0 time/Time#Before().(start) || mtime.Afterscip-go gomod github.com/golang/go/src go1.25.0 time/Time#After().(end) |
| 115 | } |
| 116 | |
| 117 | return false |
| 118 | } |
| 119 | |
| 120 | // TODO(adonovan): parallelize this. Even with a hot buffer cache, |
| 121 | // find $(go env GOMODCACHE) -type d |
| 122 | // can easily take up a minute. |
| 123 | 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.: 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`/.RootModuleCachescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/RootModuleCache.}} |
| 124 | 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().(roots, addDir, skipDir, 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#{ |
| 125 | ModulesEnabledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#ModulesEnabled.: true, |
| 126 | Concurrencyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/gopathwalk`/Options#Concurrency.: 1, // TODO(pjw): adjust concurrency |
| 127 | // Logf: log.Printf, |
| 128 | }) |
| 129 | |
| 130 | return res |
| 131 | } |