Skip to content

Commit e02e98a

Browse files
committed
internal/lsp/cache: allow network whenever reloading the workspace
Per the explanation at golang/go#54069, allow network access whenever loading the workspace. Enable one test that exercises this behavior. More tests will be added in subsequent CLs. Updates golang/go#47540 Updates golang/go#53676 Updates golang/go#54069 Change-Id: I9c3bb19d36702bc6b8051bee6b7cddaec5b97c0c Reviewed-on: https://go-review.googlesource.com/c/tools/+/419500 Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent b52794a commit e02e98a

File tree

4 files changed

+24
-36
lines changed

4 files changed

+24
-36
lines changed

gopls/internal/regtest/modfile/modfile_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,9 @@ func TestUnknownRevision(t *testing.T) {
576576
t.Skipf("skipping test that fails for unknown reasons on plan9; see https://go.dev/issue/50477")
577577
}
578578

579-
testenv.NeedsGo1Point(t, 14)
579+
// This test fails at go1.14 and go1.15 due to differing Go command behavior.
580+
// This was not significantly investigated.
581+
testenv.NeedsGo1Point(t, 16)
580582

581583
const unknown = `
582584
-- a/go.mod --

gopls/internal/regtest/workspace/broken_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package workspace
66

77
import (
8+
"strings"
89
"testing"
910

1011
"golang.org/x/tools/internal/lsp"
@@ -96,25 +97,21 @@ const CompleteMe = 222
9697
OutstandingWork(lsp.WorkspaceLoadFailure, `found module "example.com/foo" multiple times in the workspace`),
9798
)
9899

99-
/* TODO(golang/go#54069): once we allow network when reinitializing the
100-
* workspace, we should be able to fix the error here.
101-
102-
// Remove the redundant vendored copy of example.com.
103-
env.WriteWorkspaceFile("go.work", `go 1.18
100+
// Remove the redundant vendored copy of example.com.
101+
env.WriteWorkspaceFile("go.work", `go 1.18
104102
use (
105103
./package1
106104
./package2
107105
./package2/vendor/example.com/foo
108106
)
109107
`)
110-
env.Await(NoOutstandingWork())
111-
112-
// Check that definitions in package1 go to the copy vendored in package2.
113-
location, _ := env.GoToDefinition("package1/main.go", env.RegexpSearch("package1/main.go", "CompleteMe"))
114-
const wantLocation = "package2/vendor/example.com/foo/foo.go"
115-
if !strings.HasSuffix(location, wantLocation) {
116-
t.Errorf("got definition of CompleteMe at %q, want %q", location, wantLocation)
117-
}
118-
*/
108+
env.Await(NoOutstandingWork())
109+
110+
// Check that definitions in package1 go to the copy vendored in package2.
111+
location, _ := env.GoToDefinition("package1/main.go", env.RegexpSearch("package1/main.go", "CompleteMe"))
112+
const wantLocation = "package2/vendor/example.com/foo/foo.go"
113+
if !strings.HasSuffix(location, wantLocation) {
114+
t.Errorf("got definition of CompleteMe at %q, want %q", location, wantLocation)
115+
}
119116
})
120117
}

gopls/internal/regtest/workspace/workspace_test.go

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -576,21 +576,16 @@ require (
576576
replace a.com => %s/moda/a
577577
replace b.com => %s/modb
578578
`, workdir, workdir))
579-
env.Await(env.DoneWithChangeWatchedFiles())
580-
// Check that go.mod diagnostics picked up the newly active mod file.
581-
// The local version of modb has an extra dependency we need to download.
582-
env.OpenFile("modb/go.mod")
583-
env.Await(env.DoneWithOpen())
584579

585-
var d protocol.PublishDiagnosticsParams
580+
// As of golang/go#54069, writing a gopls.mod to the workspace triggers a
581+
// workspace reload.
586582
env.Await(
587583
OnceMet(
588-
env.DiagnosticAtRegexpWithMessage("modb/go.mod", `require example.com v1.2.3`, "has not been downloaded"),
589-
ReadDiagnostics("modb/go.mod", &d),
584+
env.DoneWithChangeWatchedFiles(),
585+
env.DiagnosticAtRegexp("modb/b/b.go", "x"),
590586
),
591587
)
592-
env.ApplyQuickFixes("modb/go.mod", d.Diagnostics)
593-
env.Await(env.DiagnosticAtRegexp("modb/b/b.go", "x"))
588+
594589
// Jumping to definition should now go to b.com in the workspace.
595590
if err := checkHelloLocation("modb/b/b.go"); err != nil {
596591
t.Fatal(err)
@@ -717,21 +712,15 @@ use (
717712
./modb
718713
)
719714
`)
720-
env.Await(env.DoneWithChangeWatchedFiles())
721-
// Check that go.mod diagnostics picked up the newly active mod file.
722-
// The local version of modb has an extra dependency we need to download.
723-
env.OpenFile("modb/go.mod")
724-
env.Await(env.DoneWithOpen())
725715

726-
var d protocol.PublishDiagnosticsParams
716+
// As of golang/go#54069, writing go.work to the workspace triggers a
717+
// workspace reload.
727718
env.Await(
728719
OnceMet(
729-
env.DiagnosticAtRegexpWithMessage("modb/go.mod", `require example.com v1.2.3`, "has not been downloaded"),
730-
ReadDiagnostics("modb/go.mod", &d),
720+
env.DoneWithChangeWatchedFiles(),
721+
env.DiagnosticAtRegexp("modb/b/b.go", "x"),
731722
),
732723
)
733-
env.ApplyQuickFixes("modb/go.mod", d.Diagnostics)
734-
env.Await(env.DiagnosticAtRegexp("modb/b/b.go", "x"))
735724

736725
// Jumping to definition should now go to b.com in the workspace.
737726
if err := checkHelloLocation("modb/b/b.go"); err != nil {

internal/lsp/cache/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ func (s *snapshot) loadWorkspace(ctx context.Context, firstAttempt bool) {
700700
if len(scopes) > 0 {
701701
scopes = append(scopes, PackagePath("builtin"))
702702
}
703-
err := s.load(ctx, firstAttempt, scopes...)
703+
err := s.load(ctx, true, scopes...)
704704

705705
// If the context is canceled on the first attempt, loading has failed
706706
// because the go command has timed out--that should be a critical error.

0 commit comments

Comments
 (0)