Skip to content

Commit b280e27

Browse files
committed
gopls/internal/lsp/cache: make IsIntermediateTestVariant a method
Metadata.IsIntermediateTestVariant can be derived from PkgPath and ForTest, so make it a method. Along the way, document the easily missed fact that intermediate test variants are not workspace packages. For golang/go#55293 Change-Id: Ie03011aef9c91ebce89e8aad01ef39b65bdde09a Reviewed-on: https://go-review.googlesource.com/c/tools/+/438497 Reviewed-by: Alan Donovan <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent c5514b7 commit b280e27

File tree

3 files changed

+41
-38
lines changed

3 files changed

+41
-38
lines changed

gopls/internal/lsp/cache/load.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import (
1717
"time"
1818

1919
"golang.org/x/tools/go/packages"
20-
"golang.org/x/tools/internal/event"
21-
"golang.org/x/tools/internal/gocommand"
22-
"golang.org/x/tools/internal/event/tag"
2320
"golang.org/x/tools/gopls/internal/lsp/protocol"
2421
"golang.org/x/tools/gopls/internal/lsp/source"
22+
"golang.org/x/tools/internal/event"
23+
"golang.org/x/tools/internal/event/tag"
24+
"golang.org/x/tools/internal/gocommand"
2525
"golang.org/x/tools/internal/packagesinternal"
2626
"golang.org/x/tools/internal/span"
2727
)
@@ -504,12 +504,6 @@ func buildMetadata(ctx context.Context, pkgPath PackagePath, pkg *packages.Packa
504504
}
505505
updates[id] = m
506506

507-
// Identify intermediate test variants for later filtering. See the
508-
// documentation of IsIntermediateTestVariant for more information.
509-
if m.ForTest != "" && m.ForTest != m.PkgPath && m.ForTest+"_test" != m.PkgPath {
510-
m.IsIntermediateTestVariant = true
511-
}
512-
513507
for _, err := range pkg.Errors {
514508
// Filter out parse errors from go list. We'll get them when we
515509
// actually parse, and buggy overlay support may generate spurious
@@ -686,6 +680,9 @@ func computeWorkspacePackagesLocked(s *snapshot, meta *metadataGraph) map[Packag
686680
case m.ForTest == m.PkgPath, m.ForTest+"_test" == m.PkgPath:
687681
// The test variant of some workspace package or its x_test.
688682
// To load it, we need to load the non-test variant with -test.
683+
//
684+
// Notably, this excludes intermediate test variants from workspace
685+
// packages.
689686
workspacePackages[m.ID] = m.ForTest
690687
}
691688
}

gopls/internal/lsp/cache/metadata.go

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,6 @@ type Metadata struct {
3939

4040
// Config is the *packages.Config associated with the loaded package.
4141
Config *packages.Config
42-
43-
// IsIntermediateTestVariant reports whether the given package is an
44-
// intermediate test variant, e.g.
45-
// "golang.org/x/tools/gopls/internal/lsp/cache [golang.org/x/tools/gopls/internal/lsp/source.test]".
46-
//
47-
// Such test variants arise when an x_test package (in this case source_test)
48-
// imports a package (in this case cache) that itself imports the the
49-
// non-x_test package (in this case source).
50-
//
51-
// This is done so that the forward transitive closure of source_test has
52-
// only one package for the "golang.org/x/tools/gopls/internal/lsp/source" import.
53-
// The intermediate test variant exists to hold the test variant import:
54-
//
55-
// golang.org/x/tools/gopls/internal/lsp/source_test [golang.org/x/tools/gopls/internal/lsp/source.test]
56-
// | "golang.org/x/tools/gopls/internal/lsp/cache" -> golang.org/x/tools/gopls/internal/lsp/cache [golang.org/x/tools/gopls/internal/lsp/source.test]
57-
// | "golang.org/x/tools/gopls/internal/lsp/source" -> golang.org/x/tools/gopls/internal/lsp/source [golang.org/x/tools/gopls/internal/lsp/source.test]
58-
// | ...
59-
//
60-
// golang.org/x/tools/gopls/internal/lsp/cache [golang.org/x/tools/gopls/internal/lsp/source.test]
61-
// | "golang.org/x/tools/gopls/internal/lsp/source" -> golang.org/x/tools/gopls/internal/lsp/source [golang.org/x/tools/gopls/internal/lsp/source.test]
62-
// | ...
63-
//
64-
// We filter these variants out in certain places. For example, there is
65-
// generally no reason to run diagnostics or analysis on them.
66-
//
67-
// TODO(rfindley): this can probably just be a method, since it is derived
68-
// from other fields.
69-
IsIntermediateTestVariant bool
7042
}
7143

7244
// Name implements the source.Metadata interface.
@@ -79,6 +51,40 @@ func (m *Metadata) PackagePath() string {
7951
return string(m.PkgPath)
8052
}
8153

54+
// IsIntermediateTestVariant reports whether the given package is an
55+
// intermediate test variant, e.g. "net/http [net/url.test]".
56+
//
57+
// Such test variants arise when an x_test package (in this case net/url_test)
58+
// imports a package (in this case net/http) that itself imports the the
59+
// non-x_test package (in this case net/url).
60+
//
61+
// This is done so that the forward transitive closure of net/url_test has
62+
// only one package for the "net/url" import.
63+
// The intermediate test variant exists to hold the test variant import:
64+
//
65+
// net/url_test [net/url.test]
66+
//
67+
// | "net/http" -> net/http [net/url.test]
68+
// | "net/url" -> net/url [net/url.test]
69+
// | ...
70+
//
71+
// net/http [net/url.test]
72+
//
73+
// | "net/url" -> net/url [net/url.test]
74+
// | ...
75+
//
76+
// This restriction propagates throughout the import graph of net/http: for
77+
// every package imported by net/http that imports net/url, there must be an
78+
// intermediate test variant that instead imports "net/url [net/url.test]".
79+
//
80+
// As one can see from the example of net/url and net/http, intermediate test
81+
// variants can result in many additional packages that are essentially (but
82+
// not quite) identical. For this reason, we filter these variants wherever
83+
// possible.
84+
func (m *Metadata) IsIntermediateTestVariant() bool {
85+
return m.ForTest != "" && m.ForTest != m.PkgPath && m.ForTest+"_test" != m.PkgPath
86+
}
87+
8288
// ModuleInfo implements the source.Metadata interface.
8389
func (m *Metadata) ModuleInfo() *packages.Module {
8490
return m.Module

gopls/internal/lsp/cache/snapshot.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ func (s *snapshot) packageHandlesForFile(ctx context.Context, uri span.URI, mode
695695
for _, id := range knownIDs {
696696
// Filter out any intermediate test variants. We typically aren't
697697
// interested in these packages for file= style queries.
698-
if m := s.getMetadata(id); m != nil && m.IsIntermediateTestVariant && !withIntermediateTestVariants {
698+
if m := s.getMetadata(id); m != nil && m.IsIntermediateTestVariant() && !withIntermediateTestVariants {
699699
continue
700700
}
701701
var parseModes []source.ParseMode

0 commit comments

Comments
 (0)