Debian Codegraph @

gopls/internal/cache/workspace.go

raw symbols SCIP
1 // Copyright 2020 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 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 // isGoWork reports if uri is a go.work file.
20 func isGoWork(uri protocol.DocumentURI) bool {
21 return uri.Base() == "go.work"
22 }
23
24 // goWorkModules returns the URIs of go.mod files named by the go.work file.
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 // canceled
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 // localModFiles builds a set of local go.mod files referenced by
48 // goWorkOrModPaths, which is a slice of paths as contained in a go.work 'use'
49 // directive or go.mod 'replace' directive (and which therefore may use either
50 // '/' or '\' as a path separator).
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 // isGoMod reports if uri is a go.mod file.
65 func isGoMod(uri protocol.DocumentURI) bool {
66 return uri.Base() == "go.mod"
67 }
68
69 // isWorkspaceFile reports if uri matches a set of globs defined in workspaceFiles
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 // goModModules returns the URIs of "workspace" go.mod files defined by a
85 // go.mod file. This set is defined to be the given go.mod file itself, as well
86 // as the modfiles of any locally replaced modules in the go.mod file.
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 // canceled
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 // fileExists reports whether the file has a Content (which may be empty).
114 // An overlay exists even if it is not reflected in the file system.
115 func fileExists(fh file.Handle) bool {
116 _, err := fh.Content()
117 return err == nil
118 }
119
120 // errExhausted is returned by findModules if the file scan limit is reached.
121 var errExhausted = errors.New("exhausted")
122
123 // Limit go.mod search to 1 million files. As a point of reference,
124 // Kubernetes has 22K files (as of 2020-11-24).
125 //
126 // Note: per golang/go#56496, the previous limit of 1M files was too slow, at
127 // which point this limit was decreased to 100K.
128 const fileLimit = 100_000