Skip to content

Commit e8f417a

Browse files
committed
gopls/internal/lsp/cache: cleanups to active packages
This CL makes a number of preparatory cleanups before I tackle golang/go#59674: - (*snapshot).dumpWorkspace deleted (obsolete) - cache.newMemoizedFS deleted (dead code) - snapshot.workspaceMetadata deleted (use Snapshot.WorkspaceMetadata instead). - Snapshot.ActiveMetadata renamed WorkspaceMetadata and obsolete comment updated - (*snapshot).isWorkspacePackage deleted (dead code) [Interestingly this method didn't show up in the output of Rob's script.] - Snapshot: begin to group methods in a meaningful way. - Remove Get prefix from Snapshot.CriticalError - terminological tweaks. Updates golang/go#59674 Change-Id: I9c0fcd6073270f75dad0f2e91ac6b3864358a5f1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/487016 TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Alan Donovan <[email protected]>
1 parent 82c8a38 commit e8f417a

File tree

12 files changed

+54
-86
lines changed

12 files changed

+54
-86
lines changed

gopls/internal/lsp/cache/debug.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package cache
77
import (
88
"fmt"
99
"os"
10-
"sort"
1110
)
1211

1312
// This file contains helpers that can be used to instrument code while
@@ -34,29 +33,3 @@ func assert(cond bool, msg string) {
3433
panic(msg)
3534
}
3635
}
37-
38-
// If debugEnabled is true, dumpWorkspace prints a summary of workspace
39-
// packages to stderr. If debugEnabled is false, it is a no-op.
40-
//
41-
// TODO(rfindley): this has served its purpose. Delete.
42-
func (s *snapshot) dumpWorkspace(context string) {
43-
if !debugEnabled {
44-
return
45-
}
46-
47-
debugf("workspace (after %s):", context)
48-
var ids []PackageID
49-
for id := range s.workspacePackages {
50-
ids = append(ids, id)
51-
}
52-
53-
sort.Slice(ids, func(i, j int) bool {
54-
return ids[i] < ids[j]
55-
})
56-
57-
for _, id := range ids {
58-
pkgPath := s.workspacePackages[id]
59-
_, ok := s.meta.metadata[id]
60-
debugf(" %s:%s (metadata: %t)", id, pkgPath, ok)
61-
}
62-
}

gopls/internal/lsp/cache/fs_memoized.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ type memoizedFS struct {
2727
filesByID map[robustio.FileID][]*DiskFile
2828
}
2929

30-
func newMemoizedFS() *memoizedFS {
31-
return &memoizedFS{filesByID: make(map[robustio.FileID][]*DiskFile)}
32-
}
33-
3430
// A DiskFile is a file on the filesystem, or a failure to read one.
3531
// It implements the source.FileHandle interface.
3632
type DiskFile struct {

gopls/internal/lsp/cache/load.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
244244
s.workspacePackages = workspacePackages
245245
s.resetActivePackagesLocked()
246246

247-
s.dumpWorkspace("load")
248247
s.mu.Unlock()
249248

250249
// Opt: preLoad files in parallel.

gopls/internal/lsp/cache/mod_tidy.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *snapshot) ModTidy(ctx context.Context, pm *source.ParsedModule) (*sourc
6161
}
6262
}
6363

64-
if criticalErr := s.GetCriticalError(ctx); criticalErr != nil {
64+
if criticalErr := s.CriticalError(ctx); criticalErr != nil {
6565
return &source.TidiedModule{
6666
Diagnostics: criticalErr.Diagnostics,
6767
}, nil
@@ -192,10 +192,15 @@ func modTidyDiagnostics(ctx context.Context, snapshot *snapshot, pm *source.Pars
192192
missingModuleFixes[req] = srcDiag.SuggestedFixes
193193
diagnostics = append(diagnostics, srcDiag)
194194
}
195+
195196
// Add diagnostics for missing modules anywhere they are imported in the
196197
// workspace.
198+
metas, err := snapshot.WorkspaceMetadata(ctx)
199+
if err != nil {
200+
return nil, err
201+
}
197202
// TODO(adonovan): opt: opportunities for parallelism abound.
198-
for _, m := range snapshot.workspaceMetadata() {
203+
for _, m := range metas {
199204
// Read both lists of files of this package.
200205
//
201206
// Parallelism is not necessary here as the files will have already been

gopls/internal/lsp/cache/snapshot.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -859,21 +859,15 @@ func (s *snapshot) ReverseDependencies(ctx context.Context, id PackageID, transi
859859
return rdeps, nil
860860
}
861861

862-
func (s *snapshot) workspaceMetadata() (meta []*source.Metadata) {
863-
s.mu.Lock()
864-
defer s.mu.Unlock()
865-
866-
for id := range s.workspacePackages {
867-
meta = append(meta, s.meta.metadata[id])
868-
}
869-
return meta
870-
}
871-
872862
// -- Active package tracking --
873863
//
874-
// We say a package is "active" if any of its files are open. After
875-
// type-checking we keep active packages in memory. The activePackages
876-
// peristent map does bookkeeping for the set of active packages.
864+
// We say a package is "active" if any of its files are open.
865+
// This is an optimization: the "active" concept is an
866+
// implementation detail of the cache and is not exposed
867+
// in the source or Snapshot API.
868+
// After type-checking we keep active packages in memory.
869+
// The activePackages persistent map does bookkeeping for
870+
// the set of active packages.
877871

878872
// getActivePackage returns a the memoized active package for id, if it exists.
879873
// If id is not active or has not yet been type-checked, it returns nil.
@@ -1100,11 +1094,19 @@ func (s *snapshot) knownFilesInDir(ctx context.Context, dir span.URI) []span.URI
11001094
return files
11011095
}
11021096

1103-
func (s *snapshot) ActiveMetadata(ctx context.Context) ([]*source.Metadata, error) {
1097+
func (s *snapshot) WorkspaceMetadata(ctx context.Context) ([]*source.Metadata, error) {
11041098
if err := s.awaitLoaded(ctx); err != nil {
11051099
return nil, err
11061100
}
1107-
return s.workspaceMetadata(), nil
1101+
1102+
s.mu.Lock()
1103+
defer s.mu.Unlock()
1104+
1105+
meta := make([]*source.Metadata, 0, len(s.workspacePackages))
1106+
for id := range s.workspacePackages {
1107+
meta = append(meta, s.meta.metadata[id])
1108+
}
1109+
return meta, nil
11081110
}
11091111

11101112
// Symbols extracts and returns symbol information for every file contained in
@@ -1257,14 +1259,6 @@ func (s *snapshot) noValidMetadataForURILocked(uri span.URI) bool {
12571259
return true
12581260
}
12591261

1260-
func (s *snapshot) isWorkspacePackage(id PackageID) bool {
1261-
s.mu.Lock()
1262-
defer s.mu.Unlock()
1263-
1264-
_, ok := s.workspacePackages[id]
1265-
return ok
1266-
}
1267-
12681262
func (s *snapshot) FindFile(uri span.URI) source.FileHandle {
12691263
s.view.markKnown(uri)
12701264

@@ -1383,7 +1377,7 @@ func (s *snapshot) awaitLoaded(ctx context.Context) error {
13831377
return nil
13841378
}
13851379

1386-
func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
1380+
func (s *snapshot) CriticalError(ctx context.Context) *source.CriticalError {
13871381
// If we couldn't compute workspace mod files, then the load below is
13881382
// invalid.
13891383
//
@@ -1400,7 +1394,7 @@ func (s *snapshot) GetCriticalError(ctx context.Context) *source.CriticalError {
14001394
// Even if packages didn't fail to load, we still may want to show
14011395
// additional warnings.
14021396
if loadErr == nil {
1403-
active, _ := s.ActiveMetadata(ctx)
1397+
active, _ := s.WorkspaceMetadata(ctx)
14041398
if msg := shouldShowAdHocPackagesWarning(s, active); msg != "" {
14051399
return &source.CriticalError{
14061400
MainError: errors.New(msg),
@@ -2079,7 +2073,6 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
20792073
if workspaceModeChanged {
20802074
result.workspacePackages = map[PackageID]PackagePath{}
20812075
}
2082-
result.dumpWorkspace("clone")
20832076
return result, release
20842077
}
20852078

gopls/internal/lsp/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ func collectViewStats(ctx context.Context, view *cache.View) (command.ViewStats,
990990
}
991991
allPackages := collectPackageStats(allMD)
992992

993-
wsMD, err := s.ActiveMetadata(ctx)
993+
wsMD, err := s.WorkspaceMetadata(ctx)
994994
if err != nil {
995995
return command.ViewStats{}, err
996996
}

gopls/internal/lsp/diagnostics.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@ func (s *Server) diagnose(ctx context.Context, snapshot source.Snapshot, analyze
323323
}
324324
store(modVulncheckSource, "diagnosing vulnerabilities", vulnReports, vulnErr, false)
325325

326-
activeMetas, activeErr := snapshot.ActiveMetadata(ctx)
326+
activeMetas, activeErr := snapshot.WorkspaceMetadata(ctx)
327327
if s.shouldIgnoreError(ctx, snapshot, activeErr) {
328328
return
329329
}
330-
criticalErr := snapshot.GetCriticalError(ctx)
330+
criticalErr := snapshot.CriticalError(ctx)
331331
if ctx.Err() != nil { // must check ctx after GetCriticalError
332332
return
333333
}

gopls/internal/lsp/lsp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func testLSP(t *testing.T, datum *tests.Data) {
7676
datum.ModfileFlagAvailable = len(snapshot.ModFiles()) > 0 && testenv.Go1Point() >= 14
7777
release()
7878

79-
// Open all files for performance reasons. This is done because gopls only
80-
// keeps active packages in memory for open files.
79+
// Open all files for performance reasons, because gopls only
80+
// keeps active packages (those with open files) in memory.
8181
//
8282
// In practice clients will only send document-oriented requests for open
8383
// files.

gopls/internal/lsp/source/completion/package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func (c *completer) packageNameCompletions(ctx context.Context, fileURI span.URI
203203
// file. This also includes test packages for these packages (<pkg>_test) and
204204
// the directory name itself.
205205
func packageSuggestions(ctx context.Context, snapshot source.Snapshot, fileURI span.URI, prefix string) (packages []candidate, err error) {
206-
active, err := snapshot.ActiveMetadata(ctx)
206+
active, err := snapshot.WorkspaceMetadata(ctx)
207207
if err != nil {
208208
return nil, err
209209
}

gopls/internal/lsp/source/rename.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,9 @@ func renamePackageName(ctx context.Context, s Snapshot, f FileHandle, newName Pa
631631
// Update any affected replace directives in go.mod files.
632632
// TODO(adonovan): extract into its own function.
633633
//
634-
// TODO: should this operate on all go.mod files, irrespective of whether they are included in the workspace?
635-
// Get all active mod files in the workspace
634+
// Get all workspace modules.
635+
// TODO(adonovan): should this operate on all go.mod files,
636+
// irrespective of whether they are included in the workspace?
636637
modFiles := s.ModFiles()
637638
for _, m := range modFiles {
638639
fh, err := s.ReadFile(ctx, m)

gopls/internal/lsp/source/view.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,29 @@ type Snapshot interface {
153153
// IsBuiltin reports whether uri is part of the builtin package.
154154
IsBuiltin(ctx context.Context, uri span.URI) bool
155155

156+
// CriticalError returns any critical errors in the workspace.
157+
//
158+
// A nil result may mean success, or context cancellation.
159+
CriticalError(ctx context.Context) *CriticalError
160+
161+
// Symbols returns all symbols in the snapshot.
162+
Symbols(ctx context.Context) (map[span.URI][]Symbol, error)
163+
164+
// -- package metadata --
165+
156166
// ReverseDependencies returns a new mapping whose entries are
157167
// the ID and Metadata of each package in the workspace that
158168
// directly or transitively depend on the package denoted by id,
159169
// excluding id itself.
160170
ReverseDependencies(ctx context.Context, id PackageID, transitive bool) (map[PackageID]*Metadata, error)
161171

162-
// ActiveMetadata returns a new, unordered slice containing
163-
// metadata for all packages considered 'active' in the workspace.
164-
//
165-
// In normal memory mode, this is all workspace packages. In degraded memory
166-
// mode, this is just the reverse transitive closure of open packages.
167-
ActiveMetadata(ctx context.Context) ([]*Metadata, error)
172+
// WorkspaceMetadata returns a new, unordered slice containing
173+
// metadata for all packages in the workspace.
174+
WorkspaceMetadata(ctx context.Context) ([]*Metadata, error)
168175

169176
// AllMetadata returns a new unordered array of metadata for all packages in the workspace.
170177
AllMetadata(ctx context.Context) ([]*Metadata, error)
171178

172-
// Symbols returns all symbols in the snapshot.
173-
Symbols(ctx context.Context) (map[span.URI][]Symbol, error)
174-
175179
// Metadata returns the metadata for the specified package,
176180
// or nil if it was not found.
177181
Metadata(id PackageID) *Metadata
@@ -185,6 +189,8 @@ type Snapshot interface {
185189
// It returns an error if the context was cancelled.
186190
MetadataForFile(ctx context.Context, uri span.URI) ([]*Metadata, error)
187191

192+
// -- package type-checking --
193+
188194
// TypeCheck parses and type-checks the specified packages,
189195
// and returns them in the same order as the ids.
190196
// The resulting packages' types may belong to different importers,
@@ -215,11 +221,6 @@ type Snapshot interface {
215221
// If these indexes cannot be loaded from cache, the requested packages may
216222
// be type-checked.
217223
MethodSets(ctx context.Context, ids ...PackageID) ([]*methodsets.Index, error)
218-
219-
// GetCriticalError returns any critical errors in the workspace.
220-
//
221-
// A nil result may mean success, or context cancellation.
222-
GetCriticalError(ctx context.Context) *CriticalError
223224
}
224225

225226
// NarrowestMetadataForFile returns metadata for the narrowest package

gopls/internal/lsp/source/workspace_symbol.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,12 @@ func matchFile(store *symbolStore, symbolizer symbolizer, matcher matcherFunc, r
467467
// All factors are multiplicative, meaning if more than one applies they are
468468
// multiplied together.
469469
const (
470-
// nonWorkspaceFactor is applied to symbols outside of any active
471-
// workspace. Developers are less likely to want to jump to code that they
470+
// nonWorkspaceFactor is applied to symbols outside the workspace.
471+
// Developers are less likely to want to jump to code that they
472472
// are not actively working on.
473473
nonWorkspaceFactor = 0.5
474-
// nonWorkspaceUnexportedFactor is applied to unexported symbols outside of
475-
// any active workspace. Since one wouldn't usually jump to unexported
474+
// nonWorkspaceUnexportedFactor is applied to unexported symbols outside
475+
// the workspace. Since one wouldn't usually jump to unexported
476476
// symbols to understand a package API, they are particularly irrelevant.
477477
nonWorkspaceUnexportedFactor = 0.5
478478
// every field or method nesting level to access the field decreases

0 commit comments

Comments
 (0)