Skip to content

Commit ddb71b0

Browse files
committed
gopls/internal/cache: remove findWorkspaceModFile
With zero-config gopls (golang/go#57979), this function is no longer used. Change-Id: Ie59a7d39c62eab340fb6e44ddd9b7a0b1cabd92e Reviewed-on: https://go-review.googlesource.com/c/tools/+/560467 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 365517a commit ddb71b0

File tree

3 files changed

+0
-145
lines changed

3 files changed

+0
-145
lines changed

gopls/internal/cache/view.go

-37
Original file line numberDiff line numberDiff line change
@@ -1101,43 +1101,6 @@ func loadGoEnv(ctx context.Context, dir string, configEnv []string, runner *goco
11011101
return nil
11021102
}
11031103

1104-
// findWorkspaceModFile searches for a single go.mod file relative to the given
1105-
// folder URI, using the following algorithm:
1106-
// 1. if there is a go.mod file in a parent directory, return it
1107-
// 2. else, if there is exactly one nested module, return it
1108-
// 3. else, return ""
1109-
func findWorkspaceModFile(ctx context.Context, folderURI protocol.DocumentURI, fs file.Source, excludePath func(string) bool) (protocol.DocumentURI, error) {
1110-
match, err := findRootPattern(ctx, folderURI, "go.mod", fs)
1111-
if err != nil {
1112-
if ctxErr := ctx.Err(); ctxErr != nil {
1113-
return "", ctxErr
1114-
}
1115-
return "", err
1116-
}
1117-
if match != "" {
1118-
return match, nil
1119-
}
1120-
1121-
// ...else we should check if there's exactly one nested module.
1122-
all, err := findModules(folderURI, excludePath, 2)
1123-
if err == errExhausted {
1124-
// Fall-back behavior: if we don't find any modules after searching 10000
1125-
// files, assume there are none.
1126-
event.Log(ctx, fmt.Sprintf("stopped searching for modules after %d files", fileLimit))
1127-
return "", nil
1128-
}
1129-
if err != nil {
1130-
return "", err
1131-
}
1132-
if len(all) == 1 {
1133-
// range to access first element.
1134-
for uri := range all {
1135-
return uri, nil
1136-
}
1137-
}
1138-
return "", nil
1139-
}
1140-
11411104
// findRootPattern looks for files with the given basename in dir or any parent
11421105
// directory of dir, using the provided FileSource. It returns the first match,
11431106
// starting from dir and search parents.

gopls/internal/cache/view_test.go

-59
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44
package cache
55

66
import (
7-
"context"
87
"os"
98
"path/filepath"
109
"testing"
1110

1211
"golang.org/x/tools/gopls/internal/protocol"
13-
"golang.org/x/tools/gopls/internal/test/integration/fake"
1412
)
1513

1614
func TestCaseInsensitiveFilesystem(t *testing.T) {
@@ -44,63 +42,6 @@ func TestCaseInsensitiveFilesystem(t *testing.T) {
4442
}
4543
}
4644

47-
func TestFindWorkspaceModFile(t *testing.T) {
48-
workspace := `
49-
-- a/go.mod --
50-
module a
51-
-- a/x/x.go
52-
package x
53-
-- a/x/y/y.go
54-
package x
55-
-- b/go.mod --
56-
module b
57-
-- b/c/go.mod --
58-
module bc
59-
-- d/gopls.mod --
60-
module d-goplsworkspace
61-
-- d/e/go.mod --
62-
module de
63-
-- f/g/go.mod --
64-
module fg
65-
`
66-
dir, err := fake.Tempdir(fake.UnpackTxt(workspace))
67-
if err != nil {
68-
t.Fatal(err)
69-
}
70-
defer os.RemoveAll(dir)
71-
72-
tests := []struct {
73-
folder, want string
74-
}{
75-
{"", ""}, // no module at root, and more than one nested module
76-
{"a", "a/go.mod"},
77-
{"a/x", "a/go.mod"},
78-
{"a/x/y", "a/go.mod"},
79-
{"b/c", "b/c/go.mod"},
80-
{"d", "d/e/go.mod"},
81-
{"d/e", "d/e/go.mod"},
82-
{"f", "f/g/go.mod"},
83-
}
84-
85-
for _, test := range tests {
86-
ctx := context.Background()
87-
rel := fake.RelativeTo(dir)
88-
folderURI := protocol.URIFromPath(rel.AbsPath(test.folder))
89-
excludeNothing := func(string) bool { return false }
90-
got, err := findWorkspaceModFile(ctx, folderURI, New(nil), excludeNothing)
91-
if err != nil {
92-
t.Fatal(err)
93-
}
94-
want := protocol.DocumentURI("")
95-
if test.want != "" {
96-
want = protocol.URIFromPath(rel.AbsPath(test.want))
97-
}
98-
if got != want {
99-
t.Errorf("findWorkspaceModFile(%q) = %q, want %q", test.folder, got, want)
100-
}
101-
}
102-
}
103-
10445
func TestInVendor(t *testing.T) {
10546
for _, tt := range []struct {
10647
path string

gopls/internal/cache/workspace.go

-49
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11-
"io/fs"
1211
"path/filepath"
13-
"strings"
1412

1513
"golang.org/x/mod/modfile"
1614
"golang.org/x/tools/gopls/internal/file"
@@ -74,50 +72,3 @@ var errExhausted = errors.New("exhausted")
7472
// Note: per golang/go#56496, the previous limit of 1M files was too slow, at
7573
// which point this limit was decreased to 100K.
7674
const fileLimit = 100_000
77-
78-
// findModules recursively walks the root directory looking for go.mod files,
79-
// returning the set of modules it discovers. If modLimit is non-zero,
80-
// searching stops once modLimit modules have been found.
81-
//
82-
// TODO(rfindley): consider overlays.
83-
func findModules(root protocol.DocumentURI, excludePath func(string) bool, modLimit int) (map[protocol.DocumentURI]struct{}, error) {
84-
// Walk the view's folder to find all modules in the view.
85-
modFiles := make(map[protocol.DocumentURI]struct{})
86-
searched := 0
87-
errDone := errors.New("done")
88-
err := filepath.WalkDir(root.Path(), func(path string, info fs.DirEntry, err error) error {
89-
if err != nil {
90-
// Probably a permission error. Keep looking.
91-
return filepath.SkipDir
92-
}
93-
// For any path that is not the workspace folder, check if the path
94-
// would be ignored by the go command. Vendor directories also do not
95-
// contain workspace modules.
96-
if info.IsDir() && path != root.Path() {
97-
suffix := strings.TrimPrefix(path, root.Path())
98-
switch {
99-
case checkIgnored(suffix),
100-
strings.Contains(filepath.ToSlash(suffix), "/vendor/"),
101-
excludePath(suffix):
102-
return filepath.SkipDir
103-
}
104-
}
105-
// We're only interested in go.mod files.
106-
uri := protocol.URIFromPath(path)
107-
if isGoMod(uri) {
108-
modFiles[uri] = struct{}{}
109-
}
110-
if modLimit > 0 && len(modFiles) >= modLimit {
111-
return errDone
112-
}
113-
searched++
114-
if fileLimit > 0 && searched >= fileLimit {
115-
return errExhausted
116-
}
117-
return nil
118-
})
119-
if err == errDone {
120-
return modFiles, nil
121-
}
122-
return modFiles, err
123-
}

0 commit comments

Comments
 (0)