Skip to content

Commit e6fd7f4

Browse files
committed
gopls/internal/lsp/cache: limit module scan to 100K files
When no go.work or go.mod file is found, gopls searches to see if there is exactly one module in a nested directory, in which case it narrows the workspace to this one module. This is a legacy workaround for polyglot repositories, and will be made obsolete by golang/go#57979. However, in the meantime this feature is still necessary, and is the last remaining place where we walk the workspace looking for modules. As reported in golang/go#56496, this search can be expensive in very large directories. Reduce the search limit 10x, from 1M->100K, and use the more efficient filepath.WalkDir. Fixes golang/go#56496 Change-Id: Ia46dd90ac2220b09debc68742dd882885c38eb42 Reviewed-on: https://go-review.googlesource.com/c/tools/+/496880 Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent 9ca66ba commit e6fd7f4

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

gopls/internal/lsp/cache/workspace.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"context"
99
"errors"
1010
"fmt"
11+
"io/fs"
1112
"os"
1213
"path/filepath"
1314
"sort"
@@ -127,7 +128,10 @@ var errExhausted = errors.New("exhausted")
127128

128129
// Limit go.mod search to 1 million files. As a point of reference,
129130
// Kubernetes has 22K files (as of 2020-11-24).
130-
const fileLimit = 1000000
131+
//
132+
// Note: per golang/go#56496, the previous limit of 1M files was too slow, at
133+
// which point this limit was decreased to 100K.
134+
const fileLimit = 100_000
131135

132136
// findModules recursively walks the root directory looking for go.mod files,
133137
// returning the set of modules it discovers. If modLimit is non-zero,
@@ -139,7 +143,7 @@ func findModules(root span.URI, excludePath func(string) bool, modLimit int) (ma
139143
modFiles := make(map[span.URI]struct{})
140144
searched := 0
141145
errDone := errors.New("done")
142-
err := filepath.Walk(root.Filename(), func(path string, info os.FileInfo, err error) error {
146+
err := filepath.WalkDir(root.Filename(), func(path string, info fs.DirEntry, err error) error {
143147
if err != nil {
144148
// Probably a permission error. Keep looking.
145149
return filepath.SkipDir

0 commit comments

Comments
 (0)