| 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 modindex contains code for building and searching an |
| 6 | // [Index] of the Go module cache. |
| 7 | package modindexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/ |
| 8 | |
| 9 | // The directory containing the index, returned by |
| 10 | // [IndexDir], contains a file index-name-<ver> that contains the name |
| 11 | // of the current index. We believe writing that short file is atomic. |
| 12 | // [Read] reads that file to get the file name of the index. |
| 13 | // WriteIndex writes an index with a unique name and then |
| 14 | // writes that name into a new version of index-name-<ver>. |
| 15 | // (<ver> stands for the CurrentVersion of the index format.) |
| 16 | |
| 17 | import ( |
| 18 | "mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/" |
| 19 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 20 | "path/filepathscip-go gomod github.com/golang/go/src go1.25.0 `path/filepath`/" |
| 21 | "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/" |
| 22 | "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/" |
| 23 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 24 | |
| 25 | "golang.org/x/mod/semverscip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/semver`/" |
| 26 | ) |
| 27 | |
| 28 | // Update updates the index for the specified Go |
| 29 | // module cache directory, creating it as needed. |
| 30 | // On success it returns the current index. |
| 31 | func Updatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Update().(gomodcache string) (*Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#, error) { |
| 32 | prev, err := Readscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Read().(gomodcache) |
| 33 | if err != nil { |
| 34 | if !osscip-go gomod github.com/golang/go/src go1.25.0 os/.IsNotExistscip-go gomod github.com/golang/go/src go1.25.0 os/IsNotExist().(err) { |
| 35 | return nil, err |
| 36 | } |
| 37 | prev = nil |
| 38 | } |
| 39 | return updatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/update().(gomodcache, prev) |
| 40 | } |
| 41 | |
| 42 | // update builds, writes, and returns the current index. |
| 43 | // |
| 44 | // If old is nil, the new index is built from all of GOMODCACHE; |
| 45 | // otherwise it is built from the old index plus cache updates |
| 46 | // since the previous index's time. |
| 47 | func updatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/update().(gomodcache string, old *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#) (*Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#, error) { |
| 48 | gomodcache, 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().(gomodcache) |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | new, changed, err := buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/build().(gomodcache, old) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | if old == nil || changed { |
| 57 | if err := writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/write().(gomodcache, new); err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | } |
| 61 | return new, nil |
| 62 | } |
| 63 | |
| 64 | // build returns a new index for the specified Go module cache (an |
| 65 | // absolute path). |
| 66 | // |
| 67 | // If an old index is provided, only directories more recent than it |
| 68 | // that it are scanned; older directories are provided by the old |
| 69 | // Index. |
| 70 | // |
| 71 | // The boolean result indicates whether new entries were found. |
| 72 | func buildscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/build().(gomodcache string, old *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#) (*Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#, bool, error) { |
| 73 | // Set the time window. |
| 74 | var start 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# // = dawn of time |
| 75 | if old != nil { |
| 76 | // -1s to accommodate skew between (fine-grained) time.Now |
| 77 | // and (coarse) file system's mtime clock. |
| 78 | start = old.ValidAtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#ValidAt..Addscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Add().(-1 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Secondscip-go gomod github.com/golang/go/src go1.25.0 time/Second.) |
| 79 | } |
| 80 | now := timescip-go gomod github.com/golang/go/src go1.25.0 time/.Nowscip-go gomod github.com/golang/go/src go1.25.0 time/Now().() |
| 81 | end := now.Addscip-go gomod github.com/golang/go/src go1.25.0 time/Time#Add().(24 * timescip-go gomod github.com/golang/go/src go1.25.0 time/.Hourscip-go gomod github.com/golang/go/src go1.25.0 time/Hour.) // safely in the future |
| 82 | |
| 83 | // Enumerate GOMODCACHE package directories. |
| 84 | // Choose the best (latest) package for each import path. |
| 85 | pkgDirs := findDirsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/findDirs().(gomodcache, start, end) |
| 86 | dirByPath, err := bestDirByImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/bestDirByImportPath().(pkgDirs) |
| 87 | if err != nil { |
| 88 | return nil, false, err |
| 89 | } |
| 90 | |
| 91 | // For each import path it might occur only in |
| 92 | // dirByPath, only in old, or in both. |
| 93 | // If both, use the semantically later one. |
| 94 | var entries []Entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry# |
| 95 | if old != nil { |
| 96 | for _, entry := range old.Entriesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#Entries. { |
| 97 | dir, ok := dirByPath[entry.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#ImportPath.] |
| 98 | if !ok || semverscip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/semver`/.Comparescip-go gomod golang.org/x/mod v0.35.0 `golang.org/x/mod/semver`/Compare().(dir.versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/directory#version., entry.Versionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#Version.) <= 0 { |
| 99 | // New dir is missing or not more recent; use old entry. |
| 100 | entries = append(entries, entry) |
| 101 | delete(dirByPath, entry.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#ImportPath.) |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Extract symbol information for all the new directories. |
| 107 | newEntries := extractSymbolsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/extractSymbols().(gomodcache, mapsscip-go gomod github.com/golang/go/src go1.25.0 maps/.Valuesscip-go gomod github.com/golang/go/src go1.25.0 maps/Values().(dirByPath)) |
| 108 | entries = append(entries, newEntries...) |
| 109 | slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.SortFuncscip-go gomod github.com/golang/go/src go1.25.0 slices/SortFunc().(entries, func(x, y Entryscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#) int { |
| 110 | if n := 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().(x.PkgNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#PkgName., y.PkgNamescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#PkgName.); n != 0 { |
| 111 | return n |
| 112 | } |
| 113 | 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().(x.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#ImportPath., y.ImportPathscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Entry#ImportPath.) |
| 114 | }) |
| 115 | |
| 116 | return &Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#{ |
| 117 | GOMODCACHEscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#GOMODCACHE.: gomodcache, |
| 118 | ValidAtscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#ValidAt.: now, // time before the directories were scanned |
| 119 | Entriesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/modindex`/Index#Entries.: entries, |
| 120 | }, len(newEntries) > 0, nil |
| 121 | } |