Skip to content

Commit 7347766

Browse files
findleyrgopherbot
authored andcommitted
gopls/internal/test: fix failures when running tests with GOTOOLCHAIN
Gopls integration tests want to use the ambient Go toolchain, to test integration with older Go commands. But GOTOOLCHAIN injects the toolchain binary into PATH, so gopls must remove this injected path element before it runs the go command. Unfortunately, if GOTOOLCHAIN=go1.N.P explicitly, those tests will also try to *download* the explicit toolchain and fail because we have set GOPROXY to a file based proxy. Fix this by first adding a check that the initial workspace load did not fail, as well as other related error annotations such that the failure message more accurately identifies the problem. Additionally, the preceding CL improved the integration test framework to better surface such errors. Then, actually fix the problem by setting GOTOOLCHAIN=local in our integration test sandbox. Change-Id: I8c7e9f10d1c17143f10be42476caf29021ab63e0 Reviewed-on: https://go-review.googlesource.com/c/tools/+/651418 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Robert Findley <[email protected]>
1 parent f2beb33 commit 7347766

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

gopls/internal/golang/completion/completion.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -668,7 +668,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
668668

669669
err = c.collectCompletions(ctx)
670670
if err != nil {
671-
return nil, nil, err
671+
return nil, nil, fmt.Errorf("failed to collect completions: %v", err)
672672
}
673673

674674
// Deep search collected candidates and their members for more candidates.
@@ -688,7 +688,7 @@ func Completion(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle, p
688688
for _, callback := range c.completionCallbacks {
689689
if deadline == nil || time.Now().Before(*deadline) {
690690
if err := c.snapshot.RunProcessEnvFunc(ctx, callback); err != nil {
691-
return nil, nil, err
691+
return nil, nil, fmt.Errorf("failed to run goimports callback: %v", err)
692692
}
693693
}
694694
}
@@ -989,7 +989,10 @@ func (c *completer) populateImportCompletions(searchImport *ast.ImportSpec) erro
989989
}
990990

991991
c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error {
992-
return imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.Types().Name(), opts.Env)
992+
if err := imports.GetImportPaths(ctx, searchImports, prefix, c.filename, c.pkg.Types().Name(), opts.Env); err != nil {
993+
return fmt.Errorf("getting import paths: %v", err)
994+
}
995+
return nil
993996
})
994997
return nil
995998
}
@@ -1529,7 +1532,10 @@ func (c *completer) selector(ctx context.Context, sel *ast.SelectorExpr) error {
15291532

15301533
c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error {
15311534
defer cancel()
1532-
return imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.Types().Name(), opts.Env)
1535+
if err := imports.GetPackageExports(ctx, add, id.Name, c.filename, c.pkg.Types().Name(), opts.Env); err != nil {
1536+
return fmt.Errorf("getting package exports: %v", err)
1537+
}
1538+
return nil
15331539
})
15341540
return nil
15351541
}
@@ -1916,7 +1922,10 @@ func (c *completer) unimportedPackages(ctx context.Context, seen map[string]stru
19161922
}
19171923

19181924
c.completionCallbacks = append(c.completionCallbacks, func(ctx context.Context, opts *imports.Options) error {
1919-
return imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.Types().Name(), opts.Env)
1925+
if err := imports.GetAllCandidates(ctx, add, prefix, c.filename, c.pkg.Types().Name(), opts.Env); err != nil {
1926+
return fmt.Errorf("getting completion candidates: %v", err)
1927+
}
1928+
return nil
19201929
})
19211930

19221931
return nil

gopls/internal/test/integration/fake/sandbox.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ func (sb *Sandbox) GoEnv() map[string]string {
208208
"GO111MODULE": "",
209209
"GOSUMDB": "off",
210210
"GOPACKAGESDRIVER": "off",
211+
"GOTOOLCHAIN": "local", // tests should not download toolchains
211212
}
212213
if testenv.Go1Point() >= 5 {
213214
vars["GOMODCACHE"] = ""

gopls/internal/test/marker/marker_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,10 @@ func newEnv(t *testing.T, cache *cache.Cache, files, proxyFiles map[string][]byt
971971
sandbox.Close() // ignore error
972972
t.Fatal(err)
973973
}
974-
if err := awaiter.Await(ctx, integration.InitialWorkspaceLoad); err != nil {
974+
if err := awaiter.Await(ctx, integration.OnceMet(
975+
integration.InitialWorkspaceLoad,
976+
integration.NoShownMessage(""),
977+
)); err != nil {
975978
sandbox.Close() // ignore error
976979
t.Fatal(err)
977980
}

internal/imports/fix.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,7 @@ func (e *ProcessEnv) GetResolver() (Resolver, error) {
10301030
//
10311031
// For gopls, we can optionally explicitly choose a resolver type, since we
10321032
// already know the view type.
1033-
if len(e.Env["GOMOD"]) == 0 && len(e.Env["GOWORK"]) == 0 {
1033+
if e.Env["GOMOD"] == "" && (e.Env["GOWORK"] == "" || e.Env["GOWORK"] == "off") {
10341034
e.resolver = newGopathResolver(e)
10351035
e.logf("created gopath resolver")
10361036
} else if r, err := newModuleResolver(e, e.ModCache); err != nil {

0 commit comments

Comments
 (0)