| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"context" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"path/filepath" |
| 12 |
|
| 13 |
"golang.org/x/mod/modfile" |
| 14 |
"golang.org/x/tools/gopls/internal/file" |
| 15 |
"golang.org/x/tools/gopls/internal/protocol" |
| 16 |
"golang.org/x/tools/gopls/internal/test/integration/fake/glob" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
func isGoWork(uri protocol.DocumentURI) bool { |
| 21 |
return uri.Base() == "go.work" |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
func goWorkModules(ctx context.Context, gowork protocol.DocumentURI, fs file.Source) (map[protocol.DocumentURI]unit, error) { |
| 26 |
fh, err := fs.ReadFile(ctx, gowork) |
| 27 |
if err != nil { |
| 28 |
return nil, err |
| 29 |
} |
| 30 |
content, err := fh.Content() |
| 31 |
if err != nil { |
| 32 |
return nil, err |
| 33 |
} |
| 34 |
filename := gowork.Path() |
| 35 |
dir := filepath.Dir(filename) |
| 36 |
workFile, err := modfile.ParseWork(filename, content, nil) |
| 37 |
if err != nil { |
| 38 |
return nil, fmt.Errorf("parsing go.work: %w", err) |
| 39 |
} |
| 40 |
var usedDirs []string |
| 41 |
for _, use := range workFile.Use { |
| 42 |
usedDirs = append(usedDirs, use.Path) |
| 43 |
} |
| 44 |
return localModFiles(dir, usedDirs), nil |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func localModFiles(relativeTo string, goWorkOrModPaths []string) map[protocol.DocumentURI]unit { |
| 52 |
modFiles := make(map[protocol.DocumentURI]unit) |
| 53 |
for _, path := range goWorkOrModPaths { |
| 54 |
modDir := filepath.FromSlash(path) |
| 55 |
if !filepath.IsAbs(modDir) { |
| 56 |
modDir = filepath.Join(relativeTo, modDir) |
| 57 |
} |
| 58 |
modURI := protocol.URIFromPath(filepath.Join(modDir, "go.mod")) |
| 59 |
modFiles[modURI] = unit{} |
| 60 |
} |
| 61 |
return modFiles |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
func isGoMod(uri protocol.DocumentURI) bool { |
| 66 |
return uri.Base() == "go.mod" |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
func isWorkspaceFile(uri protocol.DocumentURI, workspaceFiles []string) bool { |
| 71 |
for _, workspaceFile := range workspaceFiles { |
| 72 |
g, err := glob.Parse(workspaceFile) |
| 73 |
if err != nil { |
| 74 |
continue |
| 75 |
} |
| 76 |
|
| 77 |
if g.Match(uri.Path()) { |
| 78 |
return true |
| 79 |
} |
| 80 |
} |
| 81 |
return false |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
func goModModules(ctx context.Context, gomod protocol.DocumentURI, fs file.Source) (map[protocol.DocumentURI]unit, error) { |
| 88 |
fh, err := fs.ReadFile(ctx, gomod) |
| 89 |
if err != nil { |
| 90 |
return nil, err |
| 91 |
} |
| 92 |
content, err := fh.Content() |
| 93 |
if err != nil { |
| 94 |
return nil, err |
| 95 |
} |
| 96 |
filename := gomod.Path() |
| 97 |
dir := filepath.Dir(filename) |
| 98 |
modFile, err := modfile.Parse(filename, content, nil) |
| 99 |
if err != nil { |
| 100 |
return nil, err |
| 101 |
} |
| 102 |
var localReplaces []string |
| 103 |
for _, replace := range modFile.Replace { |
| 104 |
if modfile.IsDirectoryPath(replace.New.Path) { |
| 105 |
localReplaces = append(localReplaces, replace.New.Path) |
| 106 |
} |
| 107 |
} |
| 108 |
modFiles := localModFiles(dir, localReplaces) |
| 109 |
modFiles[gomod] = unit{} |
| 110 |
return modFiles, nil |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func fileExists(fh file.Handle) bool { |
| 116 |
_, err := fh.Content() |
| 117 |
return err == nil |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
var errExhausted = errors.New("exhausted") |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
const fileLimit = 100_000 |