Skip to content

Commit 88ea935

Browse files
committed
gopls/internal/lsp/cache: don't scan for modules when defining a view
With zero-config gopls, we no longer need to scan for modules when defining the default view for a folder. If there is no go.mod or go.work file in a parent directory, just use an ad-hoc view until the first file is opened. Delete tests that were explicitly testing the view narrowing logic, and so no longer make sense. For golang/go#57979 Change-Id: Ib2ff96068b2e17d652f24d5ec05e1f2335a7f222 Reviewed-on: https://go-review.googlesource.com/c/tools/+/553096 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 2e53332 commit 88ea935

File tree

4 files changed

+7
-117
lines changed

4 files changed

+7
-117
lines changed

gopls/internal/lsp/cache/view.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -883,22 +883,9 @@ func defineView(ctx context.Context, fs file.Source, folder *Folder, forFile fil
883883

884884
// When deriving the best view for a given file, we only want to search
885885
// up the directory hierarchy for modfiles.
886-
//
887-
// If forURI is unset, we still use the legacy heuristic of scanning for
888-
// nested modules (this will be removed as part of golang/go#57979).
889-
if forFile != nil {
890-
def.gomod, err = findRootPattern(ctx, dirURI, "go.mod", fs)
891-
if err != nil {
892-
return nil, err
893-
}
894-
} else {
895-
// filterFunc is the path filter function for this workspace folder. Notably,
896-
// it is relative to folder (which is specified by the user), not root.
897-
filterFunc := relPathExcludedByFilterFunc(folder.Dir.Path(), folder.Env.GOMODCACHE, folder.Options.DirectoryFilters)
898-
def.gomod, err = findWorkspaceModFile(ctx, folder.Dir, fs, filterFunc)
899-
if err != nil {
900-
return nil, err
901-
}
886+
def.gomod, err = findRootPattern(ctx, dirURI, "go.mod", fs)
887+
if err != nil {
888+
return nil, err
902889
}
903890

904891
// Determine how we load and where to load package information for this view
@@ -1345,18 +1332,6 @@ func allFilesExcluded(files []string, filterFunc func(protocol.DocumentURI) bool
13451332
return true
13461333
}
13471334

1348-
// relPathExcludedByFilterFunc returns a func that filters paths relative to the
1349-
// given folder according the given GOMODCACHE value and directory filters (see
1350-
// settings.BuildOptions.DirectoryFilters).
1351-
//
1352-
// The resulting func returns true if the directory should be skipped.
1353-
func relPathExcludedByFilterFunc(folder, gomodcache string, directoryFilters []string) func(string) bool {
1354-
filterer := buildFilterer(folder, gomodcache, directoryFilters)
1355-
return func(path string) bool {
1356-
return relPathExcludedByFilter(path, filterer)
1357-
}
1358-
}
1359-
13601335
func relPathExcludedByFilter(path string, filterer *Filterer) bool {
13611336
path = strings.TrimPrefix(filepath.ToSlash(path), "/")
13621337
return filterer.Disallow(path)

gopls/internal/test/integration/modfile/modfile_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"testing"
1212

1313
"golang.org/x/tools/gopls/internal/hooks"
14-
. "golang.org/x/tools/gopls/internal/test/integration"
1514
"golang.org/x/tools/gopls/internal/test/compare"
15+
. "golang.org/x/tools/gopls/internal/test/integration"
1616
"golang.org/x/tools/gopls/internal/util/bug"
1717

1818
"golang.org/x/tools/gopls/internal/lsp/protocol"
@@ -427,8 +427,9 @@ func main() {
427427
{"default", WithOptions(ProxyFiles(proxy), WorkspaceFolders("a"))},
428428
{"nested", WithOptions(ProxyFiles(proxy))},
429429
}.Run(t, mod, func(t *testing.T, env *Env) {
430-
env.OnceMet(
431-
InitialWorkspaceLoad,
430+
// With zero-config gopls, we must open a/main.go to have a View including a/go.mod.
431+
env.OpenFile("a/main.go")
432+
env.AfterChange(
432433
Diagnostics(env.AtRegexp("a/go.mod", "require")),
433434
)
434435
env.RunGoCommandInDir("a", "mod", "tidy")

gopls/internal/test/integration/workspace/directoryfilters_test.go

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -90,54 +90,6 @@ const X = 1
9090
})
9191
}
9292

93-
func TestDirectoryFiltersWorkspaceModules(t *testing.T) {
94-
// Define a module include.com which should be in the workspace, plus a
95-
// module exclude.com which should be excluded and therefore come from
96-
// the proxy.
97-
const files = `
98-
-- include/go.mod --
99-
module include.com
100-
101-
go 1.12
102-
103-
require exclude.com v1.0.0
104-
105-
-- include/go.sum --
106-
exclude.com v1.0.0 h1:Q5QSfDXY5qyNCBeUiWovUGqcLCRZKoTs9XdBeVz+w1I=
107-
exclude.com v1.0.0/go.mod h1:hFox2uDlNB2s2Jfd9tHlQVfgqUiLVTmh6ZKat4cvnj4=
108-
109-
-- include/include.go --
110-
package include
111-
112-
import "exclude.com"
113-
114-
var _ = exclude.X // satisfied only by the workspace version
115-
-- exclude/go.mod --
116-
module exclude.com
117-
118-
go 1.12
119-
-- exclude/exclude.go --
120-
package exclude
121-
122-
const X = 1
123-
`
124-
const proxy = `
125-
-- [email protected]/go.mod --
126-
module exclude.com
127-
128-
go 1.12
129-
-- [email protected]/exclude.go --
130-
package exclude
131-
`
132-
WithOptions(
133-
Modes(Experimental),
134-
ProxyFiles(proxy),
135-
Settings{"directoryFilters": []string{"-exclude"}},
136-
).Run(t, files, func(t *testing.T, env *Env) {
137-
env.Await(Diagnostics(env.AtRegexp("include/include.go", `exclude.(X)`)))
138-
})
139-
}
140-
14193
// Test for golang/go#46438: support for '**' in directory filters.
14294
func TestDirectoryFilters_Wildcard(t *testing.T) {
14395
filters := []string{"-**/bye"}

gopls/internal/test/integration/workspace/workspace_test.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package workspace
77
import (
88
"context"
99
"fmt"
10-
"path/filepath"
1110
"strings"
1211
"testing"
1312

@@ -975,43 +974,6 @@ func main() {
975974
})
976975
}
977976

978-
// Sometimes users may have their module cache within the workspace.
979-
// We shouldn't consider any module in the module cache to be in the workspace.
980-
func TestGOMODCACHEInWorkspace(t *testing.T) {
981-
const mod = `
982-
-- a/go.mod --
983-
module a.com
984-
985-
go 1.12
986-
-- a/a.go --
987-
package a
988-
989-
func _() {}
990-
-- a/c/c.go --
991-
package c
992-
-- gopath/src/b/b.go --
993-
package b
994-
-- gopath/pkg/mod/example.com/go.mod --
995-
module example.com
996-
997-
go 1.12
998-
-- gopath/pkg/mod/example.com/main.go --
999-
package main
1000-
`
1001-
WithOptions(
1002-
EnvVars{"GOPATH": filepath.FromSlash("$SANDBOX_WORKDIR/gopath")},
1003-
Modes(Default),
1004-
).Run(t, mod, func(t *testing.T, env *Env) {
1005-
// Because logs are asynchronous, this test can't use OnceMet.
1006-
env.Await(
1007-
// Confirm that the build configuration is seen as valid,
1008-
// even though there are technically multiple go.mod files in the
1009-
// worskpace.
1010-
LogMatching(protocol.Info, ".*view type GoModView.*", 1, false),
1011-
)
1012-
})
1013-
}
1014-
1015977
// Tests the fix for golang/go#52500.
1016978
func TestChangeTestVariant_Issue52500(t *testing.T) {
1017979
const src = `

0 commit comments

Comments
 (0)