Skip to content

Commit

Permalink
gopls/internal/cache: remove findWorkspaceModFile
Browse files Browse the repository at this point in the history
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 <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
  • Loading branch information
findleyr committed Feb 5, 2024
1 parent 365517a commit ddb71b0
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 145 deletions.
37 changes: 0 additions & 37 deletions gopls/internal/cache/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,43 +1101,6 @@ func loadGoEnv(ctx context.Context, dir string, configEnv []string, runner *goco
return nil
}

// findWorkspaceModFile searches for a single go.mod file relative to the given
// folder URI, using the following algorithm:
// 1. if there is a go.mod file in a parent directory, return it
// 2. else, if there is exactly one nested module, return it
// 3. else, return ""
func findWorkspaceModFile(ctx context.Context, folderURI protocol.DocumentURI, fs file.Source, excludePath func(string) bool) (protocol.DocumentURI, error) {
match, err := findRootPattern(ctx, folderURI, "go.mod", fs)
if err != nil {
if ctxErr := ctx.Err(); ctxErr != nil {
return "", ctxErr
}
return "", err
}
if match != "" {
return match, nil
}

// ...else we should check if there's exactly one nested module.
all, err := findModules(folderURI, excludePath, 2)
if err == errExhausted {
// Fall-back behavior: if we don't find any modules after searching 10000
// files, assume there are none.
event.Log(ctx, fmt.Sprintf("stopped searching for modules after %d files", fileLimit))
return "", nil
}
if err != nil {
return "", err
}
if len(all) == 1 {
// range to access first element.
for uri := range all {
return uri, nil
}
}
return "", nil
}

// findRootPattern looks for files with the given basename in dir or any parent
// directory of dir, using the provided FileSource. It returns the first match,
// starting from dir and search parents.
Expand Down
59 changes: 0 additions & 59 deletions gopls/internal/cache/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package cache

import (
"context"
"os"
"path/filepath"
"testing"

"golang.org/x/tools/gopls/internal/protocol"
"golang.org/x/tools/gopls/internal/test/integration/fake"
)

func TestCaseInsensitiveFilesystem(t *testing.T) {
Expand Down Expand Up @@ -44,63 +42,6 @@ func TestCaseInsensitiveFilesystem(t *testing.T) {
}
}

func TestFindWorkspaceModFile(t *testing.T) {
workspace := `
-- a/go.mod --
module a
-- a/x/x.go
package x
-- a/x/y/y.go
package x
-- b/go.mod --
module b
-- b/c/go.mod --
module bc
-- d/gopls.mod --
module d-goplsworkspace
-- d/e/go.mod --
module de
-- f/g/go.mod --
module fg
`
dir, err := fake.Tempdir(fake.UnpackTxt(workspace))
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

tests := []struct {
folder, want string
}{
{"", ""}, // no module at root, and more than one nested module
{"a", "a/go.mod"},
{"a/x", "a/go.mod"},
{"a/x/y", "a/go.mod"},
{"b/c", "b/c/go.mod"},
{"d", "d/e/go.mod"},
{"d/e", "d/e/go.mod"},
{"f", "f/g/go.mod"},
}

for _, test := range tests {
ctx := context.Background()
rel := fake.RelativeTo(dir)
folderURI := protocol.URIFromPath(rel.AbsPath(test.folder))
excludeNothing := func(string) bool { return false }
got, err := findWorkspaceModFile(ctx, folderURI, New(nil), excludeNothing)
if err != nil {
t.Fatal(err)
}
want := protocol.DocumentURI("")
if test.want != "" {
want = protocol.URIFromPath(rel.AbsPath(test.want))
}
if got != want {
t.Errorf("findWorkspaceModFile(%q) = %q, want %q", test.folder, got, want)
}
}
}

func TestInVendor(t *testing.T) {
for _, tt := range []struct {
path string
Expand Down
49 changes: 0 additions & 49 deletions gopls/internal/cache/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ import (
"context"
"errors"
"fmt"
"io/fs"
"path/filepath"
"strings"

"golang.org/x/mod/modfile"
"golang.org/x/tools/gopls/internal/file"
Expand Down Expand Up @@ -74,50 +72,3 @@ var errExhausted = errors.New("exhausted")
// Note: per golang/go#56496, the previous limit of 1M files was too slow, at
// which point this limit was decreased to 100K.
const fileLimit = 100_000

// findModules recursively walks the root directory looking for go.mod files,
// returning the set of modules it discovers. If modLimit is non-zero,
// searching stops once modLimit modules have been found.
//
// TODO(rfindley): consider overlays.
func findModules(root protocol.DocumentURI, excludePath func(string) bool, modLimit int) (map[protocol.DocumentURI]struct{}, error) {
// Walk the view's folder to find all modules in the view.
modFiles := make(map[protocol.DocumentURI]struct{})
searched := 0
errDone := errors.New("done")
err := filepath.WalkDir(root.Path(), func(path string, info fs.DirEntry, err error) error {
if err != nil {
// Probably a permission error. Keep looking.
return filepath.SkipDir
}
// For any path that is not the workspace folder, check if the path
// would be ignored by the go command. Vendor directories also do not
// contain workspace modules.
if info.IsDir() && path != root.Path() {
suffix := strings.TrimPrefix(path, root.Path())
switch {
case checkIgnored(suffix),
strings.Contains(filepath.ToSlash(suffix), "/vendor/"),
excludePath(suffix):
return filepath.SkipDir
}
}
// We're only interested in go.mod files.
uri := protocol.URIFromPath(path)
if isGoMod(uri) {
modFiles[uri] = struct{}{}
}
if modLimit > 0 && len(modFiles) >= modLimit {
return errDone
}
searched++
if fileLimit > 0 && searched >= fileLimit {
return errExhausted
}
return nil
})
if err == errDone {
return modFiles, nil
}
return modFiles, err
}

0 comments on commit ddb71b0

Please sign in to comment.