Skip to content

Commit ce39741

Browse files
committed
[gopls-release-branch.0.9] internal/lsp/cmd: fix vulncheck error handling
Fix 1 - print usage info only when appropriate: tool.CommandLineError is a special error that makes a command exits with full usage documentation. When Govulncheck hook runs and fails, it's unlikely the failure was from command line misuse and the extra usage doc will distract users from the root cause. Let's stop that by returning a plain error. Fix 2 - stop printing package loading errors twice: Package loading error was printed by the logger in gopls/internal/vulncheck.cmd and once more by the gopls command line handler. Print it once. For testing, added back the replace statement to go.mod. We will update go.mod back after this commit is merged. Change-Id: Ifaf569a31bbbc84d7b84e1b6d90a8fa0b27ac758 Reviewed-on: https://go-review.googlesource.com/c/tools/+/429515 Reviewed-by: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 58b9e2e commit ce39741

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

gopls/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ require (
2525
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4
2626
golang.org/x/text v0.3.7 // indirect
2727
)
28+
29+
replace golang.org/x/tools => ../

gopls/internal/vulncheck/command.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package vulncheck
99

1010
import (
1111
"context"
12+
"fmt"
1213
"log"
1314
"os"
1415
"sort"
@@ -78,8 +79,8 @@ func (c *cmd) Run(ctx context.Context, cfg *packages.Config, patterns ...string)
7879
logger.Println("loading packages...")
7980
loadedPkgs, err := gvc.LoadPackages(cfg, patterns...)
8081
if err != nil {
81-
logger.Printf("package load failed: %v", err)
82-
return nil, err
82+
logger.Printf("%v", err)
83+
return nil, fmt.Errorf("package load failed")
8384
}
8485

8586
logger.Printf("analyzing %d packages...\n", len(loadedPkgs))

internal/lsp/cmd/vulncheck.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error {
7171
opts := source.DefaultOptions().Clone()
7272
v.app.options(opts) // register hook
7373
if opts == nil || opts.Hooks.Govulncheck == nil {
74-
return tool.CommandLineErrorf("vulncheck feature is not available")
74+
return fmt.Errorf("vulncheck feature is not available")
7575
}
7676

7777
loadCfg := &packages.Config{
@@ -83,11 +83,11 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error {
8383

8484
res, err := opts.Hooks.Govulncheck(ctx, loadCfg, pattern)
8585
if err != nil {
86-
return tool.CommandLineErrorf("govulncheck failed: %v", err)
86+
return fmt.Errorf("vulncheck failed: %v", err)
8787
}
8888
data, err := json.MarshalIndent(res, " ", " ")
8989
if err != nil {
90-
return tool.CommandLineErrorf("failed to decode results: %v", err)
90+
return fmt.Errorf("vulncheck failed to encode result: %v", err)
9191
}
9292
fmt.Printf("%s", data)
9393
return nil

0 commit comments

Comments
 (0)