Skip to content

Commit b5fd088

Browse files
committed
internal/lsp/command: replace VulncheckArgs Dir with URI
commandHandler.run expects a valid file for forURI and forURI is necessary to fully populate commandDeps. Our use of directory URI does not work. We plan to use this custom command triggered through codelenses on documents (e.g. go.mod), so we use the document's URI. Change-Id: I4de6d488dec5a4b71716499e7fc5e8328ecf3e49 Reviewed-on: https://go-review.googlesource.com/c/tools/+/420994 gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Reviewed-by: Suzy Mueller <[email protected]> Run-TryBot: Hyang-Ah Hana Kim <[email protected]>
1 parent 99fd76f commit b5fd088

File tree

9 files changed

+22
-23
lines changed

9 files changed

+22
-23
lines changed

gopls/doc/commands.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ Args:
274274

275275
```
276276
{
277-
// Dir is the directory from which vulncheck will run from.
278-
"Dir": string,
277+
// Any document in the directory from which govulncheck will run.
278+
"URI": string,
279279
// Package pattern. E.g. "", ".", "./...".
280280
"Pattern": string,
281281
}

gopls/internal/regtest/misc/vuln_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ package foo
2626
`
2727
Run(t, files, func(t *testing.T, env *Env) {
2828
cmd, err := command.NewRunVulncheckExpCommand("Run Vulncheck Exp", command.VulncheckArgs{
29-
Dir: "/invalid/file/url", // invalid arg
29+
URI: "/invalid/file/url", // invalid arg
3030
})
3131
if err != nil {
3232
t.Fatal(err)
@@ -81,7 +81,8 @@ func main() {
8181
},
8282
).Run(t, files, func(t *testing.T, env *Env) {
8383
cmd, err := command.NewRunVulncheckExpCommand("Run Vulncheck Exp", command.VulncheckArgs{
84-
Dir: env.Sandbox.Workdir.RootURI(),
84+
URI: protocol.URIFromPath(env.Sandbox.Workdir.AbsPath("go.mod")),
85+
Pattern: "./...",
8586
})
8687
if err != nil {
8788
t.Fatal(err)

gopls/internal/vulncheck/command.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ func init() {
2626
Govulncheck = govulncheck
2727
}
2828

29-
func govulncheck(ctx context.Context, cfg *packages.Config, args command.VulncheckArgs) (res command.VulncheckResult, _ error) {
30-
if args.Pattern == "" {
31-
args.Pattern = "."
29+
func govulncheck(ctx context.Context, cfg *packages.Config, patterns string) (res command.VulncheckResult, _ error) {
30+
if patterns == "" {
31+
patterns = "."
3232
}
3333

3434
dbClient, err := client.NewClient(findGOVULNDB(cfg), client.Options{HTTPCache: gvc.DefaultCache()})
@@ -37,7 +37,7 @@ func govulncheck(ctx context.Context, cfg *packages.Config, args command.Vulnche
3737
}
3838

3939
c := cmd{Client: dbClient}
40-
vulns, err := c.Run(ctx, cfg, args.Pattern)
40+
vulns, err := c.Run(ctx, cfg, patterns)
4141
if err != nil {
4242
return res, err
4343
}

gopls/internal/vulncheck/vulncheck.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ import (
1818

1919
// Govulncheck runs the in-process govulncheck implementation.
2020
// With go1.18+, this is swapped with the real implementation.
21-
var Govulncheck = func(ctx context.Context, cfg *packages.Config, args command.VulncheckArgs) (res command.VulncheckResult, _ error) {
21+
var Govulncheck = func(ctx context.Context, cfg *packages.Config, patterns string) (res command.VulncheckResult, _ error) {
2222
return res, errors.New("not implemented")
2323
}

internal/lsp/cmd/vulncheck.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"os"
1313

1414
"golang.org/x/tools/go/packages"
15-
"golang.org/x/tools/internal/lsp/command"
16-
"golang.org/x/tools/internal/lsp/protocol"
1715
"golang.org/x/tools/internal/lsp/source"
1816
"golang.org/x/tools/internal/tool"
1917
)
@@ -90,12 +88,10 @@ func (v *vulncheck) Run(ctx context.Context, args ...string) error {
9088
Tests: cfg.Tests,
9189
BuildFlags: cfg.BuildFlags,
9290
Env: cfg.Env,
91+
Dir: cwd,
9392
}
9493

95-
res, err := opts.Hooks.Govulncheck(ctx, loadCfg, command.VulncheckArgs{
96-
Dir: protocol.URIFromPath(cwd),
97-
Pattern: pattern,
98-
})
94+
res, err := opts.Hooks.Govulncheck(ctx, loadCfg, pattern)
9995
if err != nil {
10096
return tool.CommandLineErrorf("govulncheck failed: %v", err)
10197
}

internal/lsp/command.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,14 +807,14 @@ type pkgLoadConfig struct {
807807
}
808808

809809
func (c *commandHandler) RunVulncheckExp(ctx context.Context, args command.VulncheckArgs) error {
810-
if args.Dir == "" {
811-
return errors.New("VulncheckArgs is missing Dir field")
810+
if args.URI == "" {
811+
return errors.New("VulncheckArgs is missing URI field")
812812
}
813813
err := c.run(ctx, commandConfig{
814814
async: true, // need to be async to be cancellable
815815
progress: "Checking vulnerability",
816816
requireSave: true,
817-
forURI: args.Dir, // Will dir work?
817+
forURI: args.URI,
818818
}, func(ctx context.Context, deps commandDeps) error {
819819
view := deps.snapshot.View()
820820
opts := view.Options()
@@ -823,7 +823,9 @@ func (c *commandHandler) RunVulncheckExp(ctx context.Context, args command.Vulnc
823823
}
824824

825825
cmd := exec.Command(os.Args[0], "vulncheck", "-config", args.Pattern)
826-
cmd.Dir = args.Dir.SpanURI().Filename()
826+
// TODO(hyangah): if args.URI is not go.mod file, we need to
827+
// adjust the directory accordingly.
828+
cmd.Dir = filepath.Dir(args.URI.SpanURI().Filename())
827829

828830
var viewEnv []string
829831
if e := opts.EnvSlice(); e != nil {

internal/lsp/command/interface.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ type DebuggingResult struct {
314314
}
315315

316316
type VulncheckArgs struct {
317-
// Dir is the directory from which vulncheck will run from.
318-
Dir protocol.DocumentURI
317+
// Any document in the directory from which govulncheck will run.
318+
URI protocol.DocumentURI
319319

320320
// Package pattern. E.g. "", ".", "./...".
321321
Pattern string

internal/lsp/source/api_json.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/lsp/source/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ type Hooks struct {
514514
StaticcheckAnalyzers map[string]*Analyzer
515515

516516
// Govulncheck is the implementation of the Govulncheck gopls command.
517-
Govulncheck func(context.Context, *packages.Config, command.VulncheckArgs) (command.VulncheckResult, error)
517+
Govulncheck func(context.Context, *packages.Config, string) (command.VulncheckResult, error)
518518
}
519519

520520
// InternalOptions contains settings that are not intended for use by the

0 commit comments

Comments
 (0)