Skip to content

Commit ad497c6

Browse files
committed
internal/lsp/cmd: add -config option to gopls vulncheck
If -config=true, gopls vulncheck reads the package load configuration JSON (build flags, env, tests, and later maybe overlay info) from stdin. And, add some logging to gopls/internal/vulncheck/command.go that helps measuring the package loading overhead. Update golang/go#50577 Change-Id: I5c1ce145b07f2bed03911613f42c09a3d6be6c28 Reviewed-on: https://go-review.googlesource.com/c/tools/+/404575 Reviewed-by: Robert Findley <[email protected]>
1 parent 62d837c commit ad497c6

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

gopls/internal/vulncheck/command.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package vulncheck
1010
import (
1111
"context"
1212
"fmt"
13+
"log"
1314
"os"
1415
"strings"
1516

@@ -83,10 +84,15 @@ func (c *cmd) run(ctx context.Context, packagesCfg *packages.Config, patterns []
8384
packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes |
8485
packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps
8586

87+
log.Println("loading packages...")
88+
8689
loadedPkgs, err := packages.Load(packagesCfg, patterns...)
8790
if err != nil {
91+
log.Printf("package load failed: %v", err)
8892
return nil, err
8993
}
94+
log.Printf("loaded %d packages\n", len(loadedPkgs))
95+
9096
pkgs := vulncheck.Convert(loadedPkgs)
9197
res, err := vulncheck.Source(ctx, pkgs, &vulncheck.Config{
9298
Client: c.Client,

internal/lsp/cmd/usage/vulncheck.hlp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Usage:
88
By default, the command outputs a JSON-encoded
99
golang.org/x/tools/internal/lsp/command.VulncheckResult
1010
message.
11-
1211
Example:
1312
$ gopls vulncheck <packages>
13+
14+
-config
15+
If true, the command reads a JSON-encoded package load configuration from stdin

internal/lsp/cmd/vulncheck.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,25 @@ import (
2020

2121
// vulncheck implements the vulncheck command.
2222
type vulncheck struct {
23-
app *Application
23+
Config bool `flag:"config" help:"If true, the command reads a JSON-encoded package load configuration from stdin"`
24+
app *Application
2425
}
2526

27+
type pkgLoadConfig struct {
28+
// BuildFlags is a list of command-line flags to be passed through to
29+
// the build system's query tool.
30+
BuildFlags []string
31+
32+
// Env is the environment to use when invoking the build system's query tool.
33+
// If Env is nil, the current environment is used.
34+
Env []string
35+
36+
// If Tests is set, the loader includes related test packages.
37+
Tests bool
38+
}
39+
40+
// TODO(hyangah): document pkgLoadConfig
41+
2642
func (v *vulncheck) Name() string { return "vulncheck" }
2743
func (v *vulncheck) Parent() string { return v.app.Name() }
2844
func (v *vulncheck) Usage() string { return "" }
@@ -36,9 +52,9 @@ func (v *vulncheck) DetailedHelp(f *flag.FlagSet) {
3652
By default, the command outputs a JSON-encoded
3753
golang.org/x/tools/internal/lsp/command.VulncheckResult
3854
message.
39-
4055
Example:
4156
$ gopls vulncheck <packages>
57+
4258
`)
4359
printFlagDefaults(f)
4460
}
@@ -56,6 +72,12 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error {
5672
if err != nil {
5773
return tool.CommandLineErrorf("failed to get current directory: %v", err)
5874
}
75+
var cfg pkgLoadConfig
76+
if v.Config {
77+
if err := json.NewDecoder(os.Stdin).Decode(&cfg); err != nil {
78+
return tool.CommandLineErrorf("failed to parse cfg: %v", err)
79+
}
80+
}
5981

6082
opts := source.DefaultOptions().Clone()
6183
v.app.options(opts) // register hook
@@ -64,19 +86,22 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error {
6486
}
6587

6688
loadCfg := &packages.Config{
67-
Context: ctx,
89+
Context: ctx,
90+
Tests: cfg.Tests,
91+
BuildFlags: cfg.BuildFlags,
92+
Env: cfg.Env,
6893
}
6994

7095
res, err := opts.Hooks.Govulncheck(ctx, loadCfg, command.VulncheckArgs{
7196
Dir: protocol.URIFromPath(cwd),
7297
Pattern: pattern,
7398
})
7499
if err != nil {
75-
return err
100+
return tool.CommandLineErrorf("govulncheck failed: %v", err)
76101
}
77102
data, err := json.MarshalIndent(res, " ", " ")
78103
if err != nil {
79-
return fmt.Errorf("failed to decode results: %v", err)
104+
return tool.CommandLineErrorf("failed to decode results: %v", err)
80105
}
81106
fmt.Printf("%s", data)
82107
return nil

0 commit comments

Comments
 (0)