Skip to content

Commit 85bf7a8

Browse files
committed
gopls/internal/lsp/source: eliminate Metadata interface
This change merges the source.Metadata interface with its sole implementation, cache.Metadata. One possible concern: the struct cannot have private fields used only by the cache-package logic that constructs these structs. We are ok with that. Change-Id: I93c112f92dc812bd0da07d36e7244d5d77978312 Reviewed-on: https://go-review.googlesource.com/c/tools/+/452035 Run-TryBot: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 2592a85 commit 85bf7a8

File tree

10 files changed

+130
-171
lines changed

10 files changed

+130
-171
lines changed

gopls/internal/lsp/cache/check.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type packageHandle struct {
4646
promise *memoize.Promise // [typeCheckResult]
4747

4848
// m is the metadata associated with the package.
49-
m *Metadata
49+
m *source.Metadata
5050

5151
// key is the hashed key for the package.
5252
//
@@ -184,7 +184,7 @@ func (s *snapshot) buildPackageHandle(ctx context.Context, id PackageID, mode so
184184

185185
// readGoFiles reads the content of Metadata.GoFiles and
186186
// Metadata.CompiledGoFiles, in parallel.
187-
func readGoFiles(ctx context.Context, s *snapshot, m *Metadata) (goFiles, compiledGoFiles []source.FileHandle, err error) {
187+
func readGoFiles(ctx context.Context, s *snapshot, m *source.Metadata) (goFiles, compiledGoFiles []source.FileHandle, err error) {
188188
var group errgroup.Group
189189
getFileHandles := func(files []span.URI) []source.FileHandle {
190190
fhs := make([]source.FileHandle, len(files))
@@ -221,7 +221,7 @@ func (s *snapshot) workspaceParseMode(id PackageID) source.ParseMode {
221221
// computePackageKey returns a key representing the act of type checking
222222
// a package named id containing the specified files, metadata, and
223223
// combined dependency hash.
224-
func computePackageKey(id PackageID, files []source.FileHandle, m *Metadata, depsKey source.Hash, mode source.ParseMode, experimentalKey bool) packageHandleKey {
224+
func computePackageKey(id PackageID, files []source.FileHandle, m *source.Metadata, depsKey source.Hash, mode source.ParseMode, experimentalKey bool) packageHandleKey {
225225
// TODO(adonovan): opt: no need to materalize the bytes; hash them directly.
226226
// Also, use field separators to avoid spurious collisions.
227227
b := bytes.NewBuffer(nil)
@@ -304,7 +304,7 @@ func (ph *packageHandle) cached() (*pkg, error) {
304304
// typeCheckImpl type checks the parsed source files in compiledGoFiles.
305305
// (The resulting pkg also holds the parsed but not type-checked goFiles.)
306306
// deps holds the future results of type-checking the direct dependencies.
307-
func typeCheckImpl(ctx context.Context, snapshot *snapshot, goFiles, compiledGoFiles []source.FileHandle, m *Metadata, mode source.ParseMode, deps map[PackageID]*packageHandle) (*pkg, error) {
307+
func typeCheckImpl(ctx context.Context, snapshot *snapshot, goFiles, compiledGoFiles []source.FileHandle, m *source.Metadata, mode source.ParseMode, deps map[PackageID]*packageHandle) (*pkg, error) {
308308
// Start type checking of direct dependencies,
309309
// in parallel and asynchronously.
310310
// As the type checker imports each of these
@@ -439,7 +439,7 @@ func typeCheckImpl(ctx context.Context, snapshot *snapshot, goFiles, compiledGoF
439439

440440
var goVersionRx = regexp.MustCompile(`^go([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)
441441

442-
func doTypeCheck(ctx context.Context, snapshot *snapshot, goFiles, compiledGoFiles []source.FileHandle, m *Metadata, mode source.ParseMode, deps map[PackageID]*packageHandle, astFilter *unexportedFilter) (*pkg, error) {
442+
func doTypeCheck(ctx context.Context, snapshot *snapshot, goFiles, compiledGoFiles []source.FileHandle, m *source.Metadata, mode source.ParseMode, deps map[PackageID]*packageHandle, astFilter *unexportedFilter) (*pkg, error) {
443443
ctx, done := event.Start(ctx, "cache.typeCheck", tag.Package.Of(string(m.ID)))
444444
defer done()
445445

@@ -628,7 +628,7 @@ func (s *snapshot) depsErrors(ctx context.Context, pkg *pkg) ([]*source.Diagnost
628628
// Select packages that can't be found, and were imported in non-workspace packages.
629629
// Workspace packages already show their own errors.
630630
var relevantErrors []*packagesinternal.PackageError
631-
for _, depsError := range pkg.m.depsErrors {
631+
for _, depsError := range pkg.m.DepsErrors {
632632
// Up to Go 1.15, the missing package was included in the stack, which
633633
// was presumably a bug. We want the next one up.
634634
directImporterIdx := len(depsError.ImportStack) - 1

gopls/internal/lsp/cache/graph.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
// graph of Go packages, as obtained from go/packages.
1616
type metadataGraph struct {
1717
// metadata maps package IDs to their associated metadata.
18-
metadata map[PackageID]*Metadata
18+
metadata map[PackageID]*source.Metadata
1919

2020
// importedBy maps package IDs to the list of packages that import them.
2121
importedBy map[PackageID][]PackageID
@@ -27,12 +27,12 @@ type metadataGraph struct {
2727

2828
// Clone creates a new metadataGraph, applying the given updates to the
2929
// receiver.
30-
func (g *metadataGraph) Clone(updates map[PackageID]*Metadata) *metadataGraph {
30+
func (g *metadataGraph) Clone(updates map[PackageID]*source.Metadata) *metadataGraph {
3131
if len(updates) == 0 {
3232
// Optimization: since the graph is immutable, we can return the receiver.
3333
return g
3434
}
35-
result := &metadataGraph{metadata: make(map[PackageID]*Metadata, len(g.metadata))}
35+
result := &metadataGraph{metadata: make(map[PackageID]*source.Metadata, len(g.metadata))}
3636
// Copy metadata.
3737
for id, m := range g.metadata {
3838
result.metadata[id] = m

gopls/internal/lsp/cache/load.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
157157

158158
moduleErrs := make(map[string][]packages.Error) // module path -> errors
159159
filterer := buildFilterer(s.view.rootURI.Filename(), s.view.gomodcache, s.view.Options())
160-
newMetadata := make(map[PackageID]*Metadata)
160+
newMetadata := make(map[PackageID]*source.Metadata)
161161
for _, pkg := range pkgs {
162162
// The Go command returns synthetic list results for module queries that
163163
// encountered module errors.
@@ -222,7 +222,7 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...loadSc
222222
//
223223
// TODO(rfindley): perform a sanity check that metadata matches here. If not,
224224
// we have an invalidation bug elsewhere.
225-
updates := make(map[PackageID]*Metadata)
225+
updates := make(map[PackageID]*source.Metadata)
226226
var updatedIDs []PackageID
227227
for _, m := range newMetadata {
228228
if existing := s.meta.metadata[m.ID]; existing == nil {
@@ -475,7 +475,7 @@ func makeWorkspaceDir(ctx context.Context, workspace *workspace, fs source.FileS
475475
// buildMetadata populates the updates map with metadata updates to
476476
// apply, based on the given pkg. It recurs through pkg.Imports to ensure that
477477
// metadata exists for all dependencies.
478-
func buildMetadata(ctx context.Context, pkg *packages.Package, cfg *packages.Config, query []string, updates map[PackageID]*Metadata, path []PackageID) error {
478+
func buildMetadata(ctx context.Context, pkg *packages.Package, cfg *packages.Config, query []string, updates map[PackageID]*source.Metadata, path []PackageID) error {
479479
// Allow for multiple ad-hoc packages in the workspace (see #47584).
480480
pkgPath := PackagePath(pkg.PkgPath)
481481
id := PackageID(pkg.ID)
@@ -507,15 +507,15 @@ func buildMetadata(ctx context.Context, pkg *packages.Package, cfg *packages.Con
507507
}
508508

509509
// Recreate the metadata rather than reusing it to avoid locking.
510-
m := &Metadata{
510+
m := &source.Metadata{
511511
ID: id,
512512
PkgPath: pkgPath,
513513
Name: PackageName(pkg.Name),
514514
ForTest: PackagePath(packagesinternal.GetForTest(pkg)),
515515
TypesSizes: pkg.TypesSizes,
516516
Config: cfg,
517517
Module: pkg.Module,
518-
depsErrors: packagesinternal.GetDepsErrors(pkg),
518+
DepsErrors: packagesinternal.GetDepsErrors(pkg),
519519
}
520520
updates[id] = m
521521

@@ -606,7 +606,7 @@ func buildMetadata(ctx context.Context, pkg *packages.Package, cfg *packages.Con
606606
// snapshot s.
607607
//
608608
// s.mu must be held while calling this function.
609-
func containsPackageLocked(s *snapshot, m *Metadata) bool {
609+
func containsPackageLocked(s *snapshot, m *source.Metadata) bool {
610610
// In legacy workspace mode, or if a package does not have an associated
611611
// module, a package is considered inside the workspace if any of its files
612612
// are under the workspace root (and not excluded).
@@ -647,7 +647,7 @@ func containsPackageLocked(s *snapshot, m *Metadata) bool {
647647
// the snapshot s.
648648
//
649649
// s.mu must be held while calling this function.
650-
func containsOpenFileLocked(s *snapshot, m *Metadata) bool {
650+
func containsOpenFileLocked(s *snapshot, m *source.Metadata) bool {
651651
uris := map[span.URI]struct{}{}
652652
for _, uri := range m.CompiledGoFiles {
653653
uris[uri] = struct{}{}
@@ -668,7 +668,7 @@ func containsOpenFileLocked(s *snapshot, m *Metadata) bool {
668668
// workspace of the snapshot s.
669669
//
670670
// s.mu must be held while calling this function.
671-
func containsFileInWorkspaceLocked(s *snapshot, m *Metadata) bool {
671+
func containsFileInWorkspaceLocked(s *snapshot, m *source.Metadata) bool {
672672
uris := map[span.URI]struct{}{}
673673
for _, uri := range m.CompiledGoFiles {
674674
uris[uri] = struct{}{}
@@ -738,7 +738,7 @@ func computeWorkspacePackagesLocked(s *snapshot, meta *metadataGraph) map[Packag
738738
// function returns false.
739739
//
740740
// If m is not a command-line-arguments package, this is trivially true.
741-
func allFilesHaveRealPackages(g *metadataGraph, m *Metadata) bool {
741+
func allFilesHaveRealPackages(g *metadataGraph, m *source.Metadata) bool {
742742
n := len(m.CompiledGoFiles)
743743
checkURIs:
744744
for _, uri := range append(m.CompiledGoFiles[0:n:n], m.GoFiles...) {

gopls/internal/lsp/cache/metadata.go

-88
This file was deleted.

gopls/internal/lsp/cache/pkg.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,17 @@ import (
1818
"golang.org/x/tools/internal/memoize"
1919
)
2020

21+
// Convenient local aliases for typed strings.
22+
type (
23+
PackageID = source.PackageID
24+
PackagePath = source.PackagePath
25+
PackageName = source.PackageName
26+
ImportPath = source.ImportPath
27+
)
28+
2129
// pkg contains the type information needed by the source package.
2230
type pkg struct {
23-
m *Metadata
31+
m *source.Metadata
2432
mode source.ParseMode
2533
fset *token.FileSet // for now, same as the snapshot's FileSet
2634
goFiles []*source.ParsedGoFile

gopls/internal/lsp/cache/snapshot.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1128,12 +1128,12 @@ func (s *snapshot) Symbols(ctx context.Context) map[span.URI][]source.Symbol {
11281128
return result
11291129
}
11301130

1131-
func (s *snapshot) MetadataForFile(ctx context.Context, uri span.URI) ([]source.Metadata, error) {
1131+
func (s *snapshot) MetadataForFile(ctx context.Context, uri span.URI) ([]*source.Metadata, error) {
11321132
knownIDs, err := s.getOrLoadIDsForURI(ctx, uri)
11331133
if err != nil {
11341134
return nil, err
11351135
}
1136-
var mds []source.Metadata
1136+
var mds []*source.Metadata
11371137
for _, id := range knownIDs {
11381138
md := s.getMetadata(id)
11391139
// TODO(rfindley): knownIDs and metadata should be in sync, but existing
@@ -1168,15 +1168,15 @@ func (s *snapshot) KnownPackages(ctx context.Context) ([]source.Package, error)
11681168
return pkgs, nil
11691169
}
11701170

1171-
func (s *snapshot) AllValidMetadata(ctx context.Context) ([]source.Metadata, error) {
1171+
func (s *snapshot) AllValidMetadata(ctx context.Context) ([]*source.Metadata, error) {
11721172
if err := s.awaitLoaded(ctx); err != nil {
11731173
return nil, err
11741174
}
11751175

11761176
s.mu.Lock()
11771177
defer s.mu.Unlock()
11781178

1179-
var meta []source.Metadata
1179+
var meta []*source.Metadata
11801180
for _, m := range s.meta.metadata {
11811181
meta = append(meta, m)
11821182
}
@@ -1233,7 +1233,7 @@ func moduleForURI(modFiles map[span.URI]struct{}, uri span.URI) span.URI {
12331233
return match
12341234
}
12351235

1236-
func (s *snapshot) getMetadata(id PackageID) *Metadata {
1236+
func (s *snapshot) getMetadata(id PackageID) *source.Metadata {
12371237
s.mu.Lock()
12381238
defer s.mu.Unlock()
12391239

@@ -1922,7 +1922,7 @@ func (s *snapshot) clone(ctx, bgCtx context.Context, changes map[span.URI]*fileC
19221922
// Compute which metadata updates are required. We only need to invalidate
19231923
// packages directly containing the affected file, and only if it changed in
19241924
// a relevant way.
1925-
metadataUpdates := make(map[PackageID]*Metadata)
1925+
metadataUpdates := make(map[PackageID]*source.Metadata)
19261926
for k, v := range s.meta.metadata {
19271927
invalidateMetadata := idsToInvalidate[k]
19281928

gopls/internal/lsp/source/format.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func Format(ctx context.Context, snapshot Snapshot, fh FileHandle) ([]protocol.T
7474
var langVersion, modulePath string
7575
mds, err := snapshot.MetadataForFile(ctx, fh.URI())
7676
if err == nil && len(mds) > 0 {
77-
if mi := mds[0].ModuleInfo(); mi != nil {
77+
if mi := mds[0].Module; mi != nil {
7878
langVersion = mi.GoVersion
7979
modulePath = mi.Path
8080
}

0 commit comments

Comments
 (0)