Skip to content

Commit e79e423

Browse files
committed
gopls/internal/vulncheck: handle package errors
x/tools/go/packages.Load can succeed even when the packages have errors. vulncheck analysis requires clean build for the analyzed code. So, check returned packages and stop if any of them has errors. Change-Id: I25a272631aa80f0c22abb00cf52776db2640cb80 Reviewed-on: https://go-review.googlesource.com/c/tools/+/454437 gopls-CI: kokoro <[email protected]> Reviewed-by: Alan Donovan <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent c5ce806 commit e79e423

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

gopls/internal/regtest/misc/vuln_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,40 @@ package foo
5656
})
5757
}
5858

59+
func TestRunGovulncheckError2(t *testing.T) {
60+
const files = `
61+
-- go.mod --
62+
module mod.com
63+
64+
go 1.12
65+
-- foo.go --
66+
package foo
67+
68+
func F() { // build error incomplete
69+
`
70+
WithOptions(
71+
EnvVars{
72+
"_GOPLS_TEST_BINARY_RUN_AS_GOPLS": "true", // needed to run `gopls vulncheck`.
73+
},
74+
Settings{
75+
"codelenses": map[string]bool{
76+
"run_govulncheck": true,
77+
},
78+
},
79+
).Run(t, files, func(t *testing.T, env *Env) {
80+
env.OpenFile("go.mod")
81+
var result command.RunVulncheckResult
82+
env.ExecuteCodeLensCommand("go.mod", command.RunGovulncheck, &result)
83+
env.Await(
84+
OnceMet(
85+
CompletedProgress(result.Token),
86+
// TODO(hyangah): find a way to inspect $/progress 'report' message.
87+
LogMatching(protocol.Info, "failed to load packages due to errors", 1, false),
88+
),
89+
)
90+
})
91+
}
92+
5993
const vulnsData = `
6094
-- GO-2022-01.yaml --
6195
modules:

gopls/internal/vulncheck/command.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package vulncheck
1010
import (
1111
"context"
1212
"encoding/json"
13+
"errors"
1314
"fmt"
1415
"log"
1516
"os"
@@ -91,7 +92,6 @@ func (c *Cmd) Run(ctx context.Context, cfg *packages.Config, patterns ...string)
9192
logger.Printf("%v", err)
9293
return nil, fmt.Errorf("package load failed")
9394
}
94-
9595
logger.Printf("analyzing %d packages...\n", len(loadedPkgs))
9696

9797
r, err := vulncheck.Source(ctx, loadedPkgs, &vulncheck.Config{Client: c.Client, SourceGoVersion: goVersion()})
@@ -236,6 +236,11 @@ func init() {
236236
logf("Failed to load packages: %v", err)
237237
return err
238238
}
239+
if n := packages.PrintErrors(pkgs); n > 0 {
240+
err := errors.New("failed to load packages due to errors")
241+
logf("%v", err)
242+
return err
243+
}
239244
logf("Loaded %d packages and their dependencies", len(pkgs))
240245
cli, err := client.NewClient(findGOVULNDB(cfg.Env), client.Options{
241246
HTTPCache: govulncheck.DefaultCache(),

0 commit comments

Comments
 (0)