Skip to content

Commit 1d19788

Browse files
committed
internal/lsp/cache: always compute IsIntermediateTestVariant
It is inconsistent to only compute IsIntermediateTestVariant for workspace packages, and could be a source of bugs. Always compute it. Change-Id: I16f40fe44a1145a74ef77fee4b7fd813abe603cb Reviewed-on: https://go-review.googlesource.com/c/tools/+/340732 Reviewed-by: Alan Donovan <adonovan@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
1 parent 4a8620f commit 1d19788

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

internal/lsp/cache/load.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,12 @@ func (s *snapshot) setMetadataLocked(ctx context.Context, pkgPath PackagePath, p
441441
depsErrors: packagesinternal.GetDepsErrors(pkg),
442442
}
443443

444+
// Identify intermediate test variants for later filtering. See the
445+
// documentation of IsIntermediateTestVariant for more information.
446+
if m.ForTest != "" && m.ForTest != m.PkgPath && m.ForTest+"_test" != m.PkgPath {
447+
m.IsIntermediateTestVariant = true
448+
}
449+
444450
for _, err := range pkg.Errors {
445451
// Filter out parse errors from go list. We'll get them when we
446452
// actually parse, and buggy overlay support may generate spurious
@@ -532,9 +538,6 @@ func (s *snapshot) setMetadataLocked(ctx context.Context, pkgPath PackagePath, p
532538
// The test variant of some workspace package or its x_test.
533539
// To load it, we need to load the non-test variant with -test.
534540
s.workspacePackages[m.ID] = m.ForTest
535-
default:
536-
// A test variant of some intermediate package. We don't care about it.
537-
m.IsIntermediateTestVariant = true
538541
}
539542
}
540543
return m, nil

internal/lsp/cache/metadata.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,29 @@ type Metadata struct {
4343
// IsIntermediateTestVariant reports whether the given package is an
4444
// intermediate test variant, e.g.
4545
// "golang.org/x/tools/internal/lsp/cache [golang.org/x/tools/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/internal/lsp/source" import.
53+
// The intermediate test variant exists to hold the test variant import:
54+
//
55+
// golang.org/x/tools/internal/lsp/source_test [golang.org/x/tools/internal/lsp/source.test]
56+
// | "golang.org/x/tools/internal/lsp/cache" -> golang.org/x/tools/internal/lsp/cache [golang.org/x/tools/internal/lsp/source.test]
57+
// | "golang.org/x/tools/internal/lsp/source" -> golang.org/x/tools/internal/lsp/source [golang.org/x/tools/internal/lsp/source.test]
58+
// | ...
59+
//
60+
// golang.org/x/tools/internal/lsp/cache [golang.org/x/tools/internal/lsp/source.test]
61+
// | "golang.org/x/tools/internal/lsp/source" -> golang.org/x/tools/internal/lsp/source [golang.org/x/tools/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.
4669
IsIntermediateTestVariant bool
4770
}
4871

0 commit comments

Comments
 (0)