Skip to content

Commit 629a7be

Browse files
committed
go/analysis/analysistest: stricter errors and GOWORK setting
This change causes analysistest to report complete failure to load one or more of the packages named by the load patterns. (This is indicated by a missing Package.Name.) Previously, these errors were silently ignored, meaning that tests were providing less coverage than they seemed. In particular, the stdversion test was unable to load the specified modules because there was no GOWORK file to locate them. (Worse: the user's GOWORK environment was affecting the test.) So, we now allow tests to specify a go.work file in the root of the test tree. If present, we honor it, and, crucially, if absent, we set GOWORK=off. Also, two other tests in gopls/internal/analysis were silently doing nothing (!). Change-Id: I4ee7ae2a636497a64f1e43eb05a4a414ceaa5f4a Reviewed-on: https://go-review.googlesource.com/c/tools/+/582595 Auto-Submit: Alan Donovan <adonovan@google.com> Reviewed-by: Tim King <taking@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 4db1697 commit 629a7be

File tree

6 files changed

+28
-5
lines changed

6 files changed

+28
-5
lines changed

go/analysis/analysistest/analysistest.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,11 +369,16 @@ type Result = checker.TestAnalyzerResult
369369
// loadPackages returns an error if any package had an error, or the pattern
370370
// matched no packages.
371371
func loadPackages(a *analysis.Analyzer, dir string, patterns ...string) ([]*packages.Package, error) {
372-
env := []string{"GOPATH=" + dir, "GO111MODULE=off"} // GOPATH mode
372+
env := []string{"GOPATH=" + dir, "GO111MODULE=off", "GOWORK=off"} // GOPATH mode
373373

374374
// Undocumented module mode. Will be replaced by something better.
375375
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
376-
env = []string{"GO111MODULE=on", "GOPROXY=off"} // module mode
376+
gowork := filepath.Join(dir, "go.work")
377+
if _, err := os.Stat(gowork); err != nil {
378+
gowork = "off"
379+
}
380+
381+
env = []string{"GO111MODULE=on", "GOPROXY=off", "GOWORK=" + gowork} // module mode
377382
}
378383

379384
// packages.Load loads the real standard library, not a minimal
@@ -397,12 +402,23 @@ func loadPackages(a *analysis.Analyzer, dir string, patterns ...string) ([]*pack
397402
return nil, err
398403
}
399404

405+
// If any named package couldn't be loaded at all
406+
// (e.g. the Name field is unset), fail fast.
407+
for _, pkg := range pkgs {
408+
if pkg.Name == "" {
409+
return nil, fmt.Errorf("failed to load %q: Errors=%v",
410+
pkg.PkgPath, pkg.Errors)
411+
}
412+
}
413+
400414
// Do NOT print errors if the analyzer will continue running.
401415
// It is incredibly confusing for tests to be printing to stderr
402416
// willy-nilly instead of their test logs, especially when the
403417
// errors are expected and are going to be fixed.
404418
if !a.RunDespiteErrors {
405-
packages.PrintErrors(pkgs)
419+
if packages.PrintErrors(pkgs) > 0 {
420+
return nil, fmt.Errorf("there were package loading errors (and RunDespiteErrors is false)")
421+
}
406422
}
407423

408424
if len(pkgs) == 0 {

go/analysis/passes/stdversion/testdata/test.txtar

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ See also gopls/internal/test/marker/testdata/diagnostics/stdversion.txt
88
which runs the same test within the gopls analysis driver, to ensure
99
coverage of per-file Go version support.
1010

11+
-- go.work --
12+
go 1.21
13+
14+
use .
15+
use sub
16+
use old
17+
1118
-- go.mod --
1219
module example.com
1320

gopls/internal/analysis/stubmethods/stubmethods_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ import (
1313

1414
func Test(t *testing.T) {
1515
testdata := analysistest.TestData()
16-
analysistest.Run(t, testdata, stubmethods.Analyzer, "a")
16+
analysistest.Run(t, testdata, stubmethods.Analyzer, "typeparams")
1717
}

gopls/internal/analysis/stubmethods/testdata/src/typeparams/implement.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
package stubmethods
66

7-
var _ I = Y{} // want "Implement I"
7+
var _ I = Y{} // want "does not implement I"
88

99
type I interface{ F() }
1010

0 commit comments

Comments
 (0)