diff --git a/gopls/internal/vulncheck/command.go b/gopls/internal/vulncheck/command.go index 1cd56b48ce..7040ae70fc 100644 --- a/gopls/internal/vulncheck/command.go +++ b/gopls/internal/vulncheck/command.go @@ -10,6 +10,7 @@ package vulncheck import ( "context" "fmt" + "log" "os" "strings" @@ -83,10 +84,15 @@ func (c *cmd) run(ctx context.Context, packagesCfg *packages.Config, patterns [] packages.NeedCompiledGoFiles | packages.NeedImports | packages.NeedTypes | packages.NeedTypesSizes | packages.NeedSyntax | packages.NeedTypesInfo | packages.NeedDeps + log.Println("loading packages...") + loadedPkgs, err := packages.Load(packagesCfg, patterns...) if err != nil { + log.Printf("package load failed: %v", err) return nil, err } + log.Printf("loaded %d packages\n", len(loadedPkgs)) + pkgs := vulncheck.Convert(loadedPkgs) res, err := vulncheck.Source(ctx, pkgs, &vulncheck.Config{ Client: c.Client, diff --git a/internal/lsp/cmd/usage/vulncheck.hlp b/internal/lsp/cmd/usage/vulncheck.hlp index 99fe13c709..19a674b2ea 100644 --- a/internal/lsp/cmd/usage/vulncheck.hlp +++ b/internal/lsp/cmd/usage/vulncheck.hlp @@ -8,6 +8,8 @@ Usage: By default, the command outputs a JSON-encoded golang.org/x/tools/internal/lsp/command.VulncheckResult message. - Example: $ gopls vulncheck + + -config + If true, the command reads a JSON-encoded package load configuration from stdin diff --git a/internal/lsp/cmd/vulncheck.go b/internal/lsp/cmd/vulncheck.go index 81737783c0..4d245cecb6 100644 --- a/internal/lsp/cmd/vulncheck.go +++ b/internal/lsp/cmd/vulncheck.go @@ -20,9 +20,25 @@ import ( // vulncheck implements the vulncheck command. type vulncheck struct { - app *Application + Config bool `flag:"config" help:"If true, the command reads a JSON-encoded package load configuration from stdin"` + app *Application } +type pkgLoadConfig struct { + // BuildFlags is a list of command-line flags to be passed through to + // the build system's query tool. + BuildFlags []string + + // Env is the environment to use when invoking the build system's query tool. + // If Env is nil, the current environment is used. + Env []string + + // If Tests is set, the loader includes related test packages. + Tests bool +} + +// TODO(hyangah): document pkgLoadConfig + func (v *vulncheck) Name() string { return "vulncheck" } func (v *vulncheck) Parent() string { return v.app.Name() } func (v *vulncheck) Usage() string { return "" } @@ -36,9 +52,9 @@ func (v *vulncheck) DetailedHelp(f *flag.FlagSet) { By default, the command outputs a JSON-encoded golang.org/x/tools/internal/lsp/command.VulncheckResult message. - Example: $ gopls vulncheck + `) printFlagDefaults(f) } @@ -56,6 +72,12 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error { if err != nil { return tool.CommandLineErrorf("failed to get current directory: %v", err) } + var cfg pkgLoadConfig + if v.Config { + if err := json.NewDecoder(os.Stdin).Decode(&cfg); err != nil { + return tool.CommandLineErrorf("failed to parse cfg: %v", err) + } + } opts := source.DefaultOptions().Clone() v.app.options(opts) // register hook @@ -64,7 +86,10 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error { } loadCfg := &packages.Config{ - Context: ctx, + Context: ctx, + Tests: cfg.Tests, + BuildFlags: cfg.BuildFlags, + Env: cfg.Env, } res, err := opts.Hooks.Govulncheck(ctx, loadCfg, command.VulncheckArgs{ @@ -72,11 +97,11 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error { Pattern: pattern, }) if err != nil { - return err + return tool.CommandLineErrorf("govulncheck failed: %v", err) } data, err := json.MarshalIndent(res, " ", " ") if err != nil { - return fmt.Errorf("failed to decode results: %v", err) + return tool.CommandLineErrorf("failed to decode results: %v", err) } fmt.Printf("%s", data) return nil