Marwan Sulaiman | 63da46f | 2020-03-11 00:49:10 -0400 | [diff] [blame] | 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 5 | package lsp |
| 6 | |
| 7 | import ( |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 8 | "bytes" |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 9 | "context" |
Rob Findley | ed71c57 | 2020-08-17 22:50:34 -0400 | [diff] [blame] | 10 | "encoding/json" |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 11 | "errors" |
Rebecca Stambler | 01425d7 | 2020-07-10 20:17:16 -0400 | [diff] [blame] | 12 | "fmt" |
Martin Asquino | 2bc93b1 | 2020-05-05 19:43:51 +0100 | [diff] [blame] | 13 | "io" |
Rob Findley | 463111b | 2020-09-18 17:26:33 -0400 | [diff] [blame] | 14 | "io/ioutil" |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 15 | "os" |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 16 | "os/exec" |
Rob Findley | 463111b | 2020-09-18 17:26:33 -0400 | [diff] [blame] | 17 | "path/filepath" |
Heschi Kreinick | 414ec9c | 2022-02-04 15:23:09 -0500 | [diff] [blame] | 18 | "sort" |
Heschi Kreinick | bd5d160 | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 19 | "strings" |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 20 | |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 21 | "golang.org/x/mod/modfile" |
Heschi Kreinick | 414ec9c | 2022-02-04 15:23:09 -0500 | [diff] [blame] | 22 | "golang.org/x/tools/go/ast/astutil" |
Rebecca Stambler | c80dc57 | 2020-07-15 23:33:55 -0400 | [diff] [blame] | 23 | "golang.org/x/tools/internal/event" |
Heschi Kreinick | 2b84a06 | 2020-10-26 15:13:16 -0400 | [diff] [blame] | 24 | "golang.org/x/tools/internal/gocommand" |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 25 | "golang.org/x/tools/internal/lsp/command" |
Rob Findley | 7c93484 | 2021-04-12 12:38:22 -0400 | [diff] [blame] | 26 | "golang.org/x/tools/internal/lsp/debug" |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 27 | "golang.org/x/tools/internal/lsp/progress" |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 28 | "golang.org/x/tools/internal/lsp/protocol" |
| 29 | "golang.org/x/tools/internal/lsp/source" |
Rebecca Stambler | 6aa8f57 | 2020-06-11 18:25:17 -0400 | [diff] [blame] | 30 | "golang.org/x/tools/internal/span" |
Marwan Sulaiman | 63da46f | 2020-03-11 00:49:10 -0400 | [diff] [blame] | 31 | "golang.org/x/tools/internal/xcontext" |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | func (s *Server) executeCommand(ctx context.Context, params *protocol.ExecuteCommandParams) (interface{}, error) { |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 35 | var found bool |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 36 | for _, name := range s.session.Options().SupportedCommands { |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 37 | if name == params.Command { |
| 38 | found = true |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 39 | break |
| 40 | } |
| 41 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 42 | if !found { |
| 43 | return nil, fmt.Errorf("%s is not a supported command", params.Command) |
Rob Findley | 463111b | 2020-09-18 17:26:33 -0400 | [diff] [blame] | 44 | } |
Heschi Kreinick | a83918f | 2020-11-20 17:12:49 -0500 | [diff] [blame] | 45 | |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 46 | handler := &commandHandler{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 47 | s: s, |
| 48 | params: params, |
Rob Findley | 5bbba66 | 2020-10-28 12:22:52 -0400 | [diff] [blame] | 49 | } |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 50 | return command.Dispatch(ctx, params, handler) |
Rob Findley | ed71c57 | 2020-08-17 22:50:34 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 53 | type commandHandler struct { |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 54 | s *Server |
| 55 | params *protocol.ExecuteCommandParams |
Rob Findley | 60aba8a | 2020-09-16 11:14:28 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 58 | // commandConfig configures common command set-up and execution. |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 59 | type commandConfig struct { |
Marwan Sulaiman | 4061312 | 2021-05-15 10:04:17 -0400 | [diff] [blame] | 60 | async bool // whether to run the command asynchronously. Async commands can only return errors. |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 61 | requireSave bool // whether all files must be saved for the command to work |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 62 | progress string // title to use for progress reporting. If empty, no progress will be reported. |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 63 | forURI protocol.DocumentURI // URI to resolve to a snapshot. If unset, snapshot will be nil. |
| 64 | } |
| 65 | |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 66 | // commandDeps is evaluated from a commandConfig. Note that not all fields may |
| 67 | // be populated, depending on which configuration is set. See comments in-line |
| 68 | // for details. |
| 69 | type commandDeps struct { |
| 70 | snapshot source.Snapshot // present if cfg.forURI was set |
| 71 | fh source.VersionedFileHandle // present if cfg.forURI was set |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 72 | work *progress.WorkDone // present cfg.progress was set |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | type commandFunc func(context.Context, commandDeps) error |
| 76 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 77 | func (c *commandHandler) run(ctx context.Context, cfg commandConfig, run commandFunc) (err error) { |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 78 | if cfg.requireSave { |
Rebecca Stambler | 92b2fbe | 2021-08-06 14:10:12 -0400 | [diff] [blame] | 79 | var unsaved []string |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 80 | for _, overlay := range c.s.session.Overlays() { |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 81 | if !overlay.Saved() { |
Rebecca Stambler | 92b2fbe | 2021-08-06 14:10:12 -0400 | [diff] [blame] | 82 | unsaved = append(unsaved, overlay.URI().Filename()) |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
Rebecca Stambler | 92b2fbe | 2021-08-06 14:10:12 -0400 | [diff] [blame] | 85 | if len(unsaved) > 0 { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 86 | return fmt.Errorf("All files must be saved first (unsaved: %v).", unsaved) |
Rebecca Stambler | 92b2fbe | 2021-08-06 14:10:12 -0400 | [diff] [blame] | 87 | } |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 88 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 89 | var deps commandDeps |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 90 | if cfg.forURI != "" { |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 91 | var ok bool |
| 92 | var release func() |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 93 | deps.snapshot, deps.fh, ok, release, err = c.s.beginFileRequest(ctx, cfg.forURI, source.UnknownKind) |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 94 | defer release() |
| 95 | if !ok { |
Hana | 54af36e | 2022-03-25 11:14:27 -0400 | [diff] [blame] | 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | return fmt.Errorf("invalid file URL: %v", cfg.forURI) |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 100 | } |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 101 | } |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 102 | ctx, cancel := context.WithCancel(xcontext.Detach(ctx)) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 103 | if cfg.progress != "" { |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 104 | deps.work = c.s.progress.Start(ctx, cfg.progress, "Running...", c.params.WorkDoneToken, cancel) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 105 | } |
| 106 | runcmd := func() error { |
| 107 | defer cancel() |
| 108 | err := run(ctx, deps) |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 109 | if deps.work != nil { |
| 110 | switch { |
| 111 | case errors.Is(err, context.Canceled): |
Robert Findley | 76325da | 2022-06-01 16:13:55 -0400 | [diff] [blame] | 112 | deps.work.End(ctx, "canceled") |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 113 | case err != nil: |
| 114 | event.Error(ctx, "command error", err) |
Robert Findley | 76325da | 2022-06-01 16:13:55 -0400 | [diff] [blame] | 115 | deps.work.End(ctx, "failed") |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 116 | default: |
Robert Findley | 76325da | 2022-06-01 16:13:55 -0400 | [diff] [blame] | 117 | deps.work.End(ctx, "completed") |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 118 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 119 | } |
| 120 | return err |
| 121 | } |
| 122 | if cfg.async { |
Marwan Sulaiman | 4061312 | 2021-05-15 10:04:17 -0400 | [diff] [blame] | 123 | go func() { |
| 124 | if err := runcmd(); err != nil { |
| 125 | if showMessageErr := c.s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ |
| 126 | Type: protocol.Error, |
| 127 | Message: err.Error(), |
| 128 | }); showMessageErr != nil { |
| 129 | event.Error(ctx, fmt.Sprintf("failed to show message: %q", err.Error()), showMessageErr) |
| 130 | } |
| 131 | } |
| 132 | }() |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 133 | return nil |
| 134 | } |
| 135 | return runcmd() |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 136 | } |
| 137 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 138 | func (c *commandHandler) ApplyFix(ctx context.Context, args command.ApplyFixArgs) error { |
| 139 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 140 | // Note: no progress here. Applying fixes should be quick. |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 141 | forURI: args.URI, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 142 | }, func(ctx context.Context, deps commandDeps) error { |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 143 | edits, err := source.ApplyFix(ctx, args.Fix, deps.snapshot, deps.fh, args.Range) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 144 | if err != nil { |
Rob Findley | ed71c57 | 2020-08-17 22:50:34 -0400 | [diff] [blame] | 145 | return err |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 146 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 147 | r, err := c.s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{ |
| 148 | Edit: protocol.WorkspaceEdit{ |
| 149 | DocumentChanges: edits, |
| 150 | }, |
| 151 | }) |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | if !r.Applied { |
| 156 | return errors.New(r.FailureReason) |
| 157 | } |
| 158 | return nil |
| 159 | }) |
| 160 | } |
| 161 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 162 | func (c *commandHandler) RegenerateCgo(ctx context.Context, args command.URIArg) error { |
| 163 | return c.run(ctx, commandConfig{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 164 | progress: "Regenerating Cgo", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 165 | }, func(ctx context.Context, deps commandDeps) error { |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 166 | mod := source.FileModification{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 167 | URI: args.URI.SpanURI(), |
Rebecca Stambler | 9267083 | 2020-07-23 23:24:36 -0400 | [diff] [blame] | 168 | Action: source.InvalidateMetadata, |
| 169 | } |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 170 | return c.s.didModifyFiles(ctx, []source.FileModification{mod}, FromRegenerateCgo) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 171 | }) |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 172 | } |
| 173 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 174 | func (c *commandHandler) CheckUpgrades(ctx context.Context, args command.CheckUpgradesArgs) error { |
| 175 | return c.run(ctx, commandConfig{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 176 | forURI: args.URI, |
| 177 | progress: "Checking for upgrades", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 178 | }, func(ctx context.Context, deps commandDeps) error { |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 179 | upgrades, err := c.s.getUpgrades(ctx, deps.snapshot, args.URI.SpanURI(), args.Modules) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 180 | if err != nil { |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 181 | return err |
| 182 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 183 | deps.snapshot.View().RegisterModuleUpgrades(upgrades) |
| 184 | // Re-diagnose the snapshot to publish the new module diagnostics. |
| 185 | c.s.diagnoseSnapshot(deps.snapshot, nil, false) |
| 186 | return nil |
| 187 | }) |
| 188 | } |
| 189 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 190 | func (c *commandHandler) AddDependency(ctx context.Context, args command.DependencyArgs) error { |
| 191 | return c.GoGetModule(ctx, args) |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 192 | } |
| 193 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 194 | func (c *commandHandler) UpgradeDependency(ctx context.Context, args command.DependencyArgs) error { |
| 195 | return c.GoGetModule(ctx, args) |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 198 | func (c *commandHandler) GoGetModule(ctx context.Context, args command.DependencyArgs) error { |
| 199 | return c.run(ctx, commandConfig{ |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 200 | progress: "Running go get", |
| 201 | forURI: args.URI, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 202 | }, func(ctx context.Context, deps commandDeps) error { |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 203 | return c.s.runGoModUpdateCommands(ctx, deps.snapshot, args.URI.SpanURI(), func(invoke func(...string) (*bytes.Buffer, error)) error { |
| 204 | return runGoGetModule(invoke, args.AddRequire, args.GoCmdArgs) |
| 205 | }) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 206 | }) |
| 207 | } |
| 208 | |
| 209 | // TODO(rFindley): UpdateGoSum, Tidy, and Vendor could probably all be one command. |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 210 | func (c *commandHandler) UpdateGoSum(ctx context.Context, args command.URIArgs) error { |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 211 | return c.run(ctx, commandConfig{ |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 212 | progress: "Updating go.sum", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 213 | }, func(ctx context.Context, deps commandDeps) error { |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 214 | for _, uri := range args.URIs { |
| 215 | snapshot, fh, ok, release, err := c.s.beginFileRequest(ctx, uri, source.UnknownKind) |
| 216 | defer release() |
| 217 | if !ok { |
| 218 | return err |
| 219 | } |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 220 | if err := c.s.runGoModUpdateCommands(ctx, snapshot, fh.URI(), func(invoke func(...string) (*bytes.Buffer, error)) error { |
| 221 | _, err := invoke("list", "all") |
| 222 | return err |
| 223 | }); err != nil { |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 224 | return err |
| 225 | } |
| 226 | } |
| 227 | return nil |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 228 | }) |
| 229 | } |
| 230 | |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 231 | func (c *commandHandler) Tidy(ctx context.Context, args command.URIArgs) error { |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 232 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 233 | requireSave: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 234 | progress: "Running go mod tidy", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 235 | }, func(ctx context.Context, deps commandDeps) error { |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 236 | for _, uri := range args.URIs { |
| 237 | snapshot, fh, ok, release, err := c.s.beginFileRequest(ctx, uri, source.UnknownKind) |
| 238 | defer release() |
| 239 | if !ok { |
| 240 | return err |
| 241 | } |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 242 | if err := c.s.runGoModUpdateCommands(ctx, snapshot, fh.URI(), func(invoke func(...string) (*bytes.Buffer, error)) error { |
| 243 | _, err := invoke("mod", "tidy") |
| 244 | return err |
| 245 | }); err != nil { |
Rebecca Stambler | d459050 | 2021-02-04 18:12:25 -0500 | [diff] [blame] | 246 | return err |
| 247 | } |
| 248 | } |
| 249 | return nil |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 250 | }) |
| 251 | } |
| 252 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 253 | func (c *commandHandler) Vendor(ctx context.Context, args command.URIArg) error { |
| 254 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 255 | requireSave: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 256 | progress: "Running go mod vendor", |
| 257 | forURI: args.URI, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 258 | }, func(ctx context.Context, deps commandDeps) error { |
Robert Findley | b2552ef | 2022-04-11 17:17:47 -0400 | [diff] [blame] | 259 | // Use RunGoCommandPiped here so that we don't compete with any other go |
| 260 | // command invocations. go mod vendor deletes modules.txt before recreating |
| 261 | // it, and therefore can run into file locking issues on Windows if that |
| 262 | // file is in use by another process, such as go list. |
| 263 | // |
| 264 | // If golang/go#44119 is resolved, go mod vendor will instead modify |
| 265 | // modules.txt in-place. In that case we could theoretically allow this |
| 266 | // command to run concurrently. |
| 267 | err := deps.snapshot.RunGoCommandPiped(ctx, source.Normal|source.AllowNetwork, &gocommand.Invocation{ |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 268 | Verb: "mod", |
| 269 | Args: []string{"vendor"}, |
| 270 | WorkingDir: filepath.Dir(args.URI.SpanURI().Filename()), |
Robert Findley | b2552ef | 2022-04-11 17:17:47 -0400 | [diff] [blame] | 271 | }, &bytes.Buffer{}, &bytes.Buffer{}) |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 272 | return err |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 273 | }) |
| 274 | } |
| 275 | |
Suzy Mueller | c2ddf3d | 2022-02-18 16:05:08 -0700 | [diff] [blame] | 276 | func (c *commandHandler) EditGoDirective(ctx context.Context, args command.EditGoDirectiveArgs) error { |
| 277 | return c.run(ctx, commandConfig{ |
| 278 | requireSave: true, // if go.mod isn't saved it could cause a problem |
| 279 | forURI: args.URI, |
| 280 | }, func(ctx context.Context, deps commandDeps) error { |
Suzy Mueller | 4a5e7f0 | 2022-03-02 11:22:35 -0700 | [diff] [blame] | 281 | snapshot, fh, ok, release, err := c.s.beginFileRequest(ctx, args.URI, source.UnknownKind) |
| 282 | defer release() |
| 283 | if !ok { |
| 284 | return err |
| 285 | } |
| 286 | if err := c.s.runGoModUpdateCommands(ctx, snapshot, fh.URI(), func(invoke func(...string) (*bytes.Buffer, error)) error { |
| 287 | _, err := invoke("mod", "edit", "-go", args.Version) |
| 288 | return err |
| 289 | }); err != nil { |
| 290 | return err |
| 291 | } |
| 292 | return nil |
Suzy Mueller | c2ddf3d | 2022-02-18 16:05:08 -0700 | [diff] [blame] | 293 | }) |
| 294 | } |
| 295 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 296 | func (c *commandHandler) RemoveDependency(ctx context.Context, args command.RemoveDependencyArgs) error { |
| 297 | return c.run(ctx, commandConfig{ |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 298 | progress: "Removing dependency", |
| 299 | forURI: args.URI, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 300 | }, func(ctx context.Context, deps commandDeps) error { |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 301 | // If the module is tidied apart from the one unused diagnostic, we can |
| 302 | // run `go get module@none`, and then run `go mod tidy`. Otherwise, we |
| 303 | // must make textual edits. |
| 304 | // TODO(rstambler): In Go 1.17+, we will be able to use the go command |
| 305 | // without checking if the module is tidy. |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 306 | if args.OnlyDiagnostic { |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 307 | return c.s.runGoModUpdateCommands(ctx, deps.snapshot, args.URI.SpanURI(), func(invoke func(...string) (*bytes.Buffer, error)) error { |
| 308 | if err := runGoGetModule(invoke, false, []string{args.ModulePath + "@none"}); err != nil { |
| 309 | return err |
| 310 | } |
| 311 | _, err := invoke("mod", "tidy") |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 312 | return err |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 313 | }) |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 314 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 315 | pm, err := deps.snapshot.ParseMod(ctx, deps.fh) |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 316 | if err != nil { |
| 317 | return err |
| 318 | } |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 319 | edits, err := dropDependency(deps.snapshot, pm, args.ModulePath) |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 320 | if err != nil { |
| 321 | return err |
| 322 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 323 | response, err := c.s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{ |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 324 | Edit: protocol.WorkspaceEdit{ |
| 325 | DocumentChanges: []protocol.TextDocumentEdit{{ |
pjw | c3402e3 | 2021-01-24 14:00:12 -0500 | [diff] [blame] | 326 | TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 327 | Version: deps.fh.Version(), |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 328 | TextDocumentIdentifier: protocol.TextDocumentIdentifier{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 329 | URI: protocol.URIFromSpanURI(deps.fh.URI()), |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 330 | }, |
| 331 | }, |
| 332 | Edits: edits, |
| 333 | }}, |
| 334 | }, |
| 335 | }) |
| 336 | if err != nil { |
| 337 | return err |
| 338 | } |
| 339 | if !response.Applied { |
| 340 | return fmt.Errorf("edits not applied because of %s", response.FailureReason) |
| 341 | } |
Rob Findley | d2671c4 | 2021-01-31 16:04:49 -0500 | [diff] [blame] | 342 | return nil |
| 343 | }) |
Rebecca Stambler | 1081e67 | 2019-09-19 01:21:54 -0400 | [diff] [blame] | 344 | } |
Marwan Sulaiman | 63da46f | 2020-03-11 00:49:10 -0400 | [diff] [blame] | 345 | |
Rebecca Stambler | 57089f8 | 2020-12-22 23:58:35 -0500 | [diff] [blame] | 346 | // dropDependency returns the edits to remove the given require from the go.mod |
| 347 | // file. |
| 348 | func dropDependency(snapshot source.Snapshot, pm *source.ParsedModule, modulePath string) ([]protocol.TextEdit, error) { |
| 349 | // We need a private copy of the parsed go.mod file, since we're going to |
| 350 | // modify it. |
| 351 | copied, err := modfile.Parse("", pm.Mapper.Content, nil) |
| 352 | if err != nil { |
| 353 | return nil, err |
| 354 | } |
| 355 | if err := copied.DropRequire(modulePath); err != nil { |
| 356 | return nil, err |
| 357 | } |
| 358 | copied.Cleanup() |
| 359 | newContent, err := copied.Format() |
| 360 | if err != nil { |
| 361 | return nil, err |
| 362 | } |
| 363 | // Calculate the edits to be made due to the change. |
| 364 | diff, err := snapshot.View().Options().ComputeEdits(pm.URI, string(pm.Mapper.Content), string(newContent)) |
| 365 | if err != nil { |
| 366 | return nil, err |
| 367 | } |
| 368 | return source.ToProtocolEdits(pm.Mapper, diff) |
| 369 | } |
| 370 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 371 | func (c *commandHandler) Test(ctx context.Context, uri protocol.DocumentURI, tests, benchmarks []string) error { |
| 372 | return c.RunTests(ctx, command.RunTestsArgs{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 373 | URI: uri, |
| 374 | Tests: tests, |
| 375 | Benchmarks: benchmarks, |
| 376 | }) |
| 377 | } |
| 378 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 379 | func (c *commandHandler) RunTests(ctx context.Context, args command.RunTestsArgs) error { |
| 380 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 381 | async: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 382 | progress: "Running go test", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 383 | requireSave: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 384 | forURI: args.URI, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 385 | }, func(ctx context.Context, deps commandDeps) error { |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 386 | if err := c.runTests(ctx, deps.snapshot, deps.work, args.URI, args.Tests, args.Benchmarks); err != nil { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 387 | return fmt.Errorf("running tests failed: %w", err) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 388 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 389 | return nil |
| 390 | }) |
| 391 | } |
| 392 | |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 393 | func (c *commandHandler) runTests(ctx context.Context, snapshot source.Snapshot, work *progress.WorkDone, uri protocol.DocumentURI, tests, benchmarks []string) error { |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 394 | // TODO: fix the error reporting when this runs async. |
Robert Findley | e5f719f | 2021-09-03 16:03:51 -0400 | [diff] [blame] | 395 | pkgs, err := snapshot.PackagesForFile(ctx, uri.SpanURI(), source.TypecheckWorkspace, false) |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 396 | if err != nil { |
| 397 | return err |
| 398 | } |
| 399 | if len(pkgs) == 0 { |
| 400 | return fmt.Errorf("package could not be found for file: %s", uri.SpanURI().Filename()) |
| 401 | } |
Pontus Leitzler | 368bee8 | 2020-12-06 21:01:19 +0100 | [diff] [blame] | 402 | pkgPath := pkgs[0].ForTest() |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 403 | |
| 404 | // create output |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 405 | buf := &bytes.Buffer{} |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 406 | ew := progress.NewEventWriter(ctx, "test") |
Robert Findley | 459e2b8 | 2022-07-12 17:13:32 -0400 | [diff] [blame] | 407 | out := io.MultiWriter(ew, progress.NewWorkDoneWriter(ctx, work), buf) |
Martin Asquino | 2bc93b1 | 2020-05-05 19:43:51 +0100 | [diff] [blame] | 408 | |
Rob Findley | ed71c57 | 2020-08-17 22:50:34 -0400 | [diff] [blame] | 409 | // Run `go test -run Func` on each test. |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 410 | var failedTests int |
| 411 | for _, funcName := range tests { |
Heschi Kreinick | 2b84a06 | 2020-10-26 15:13:16 -0400 | [diff] [blame] | 412 | inv := &gocommand.Invocation{ |
| 413 | Verb: "test", |
| 414 | Args: []string{pkgPath, "-v", "-count=1", "-run", fmt.Sprintf("^%s$", funcName)}, |
| 415 | WorkingDir: filepath.Dir(uri.SpanURI().Filename()), |
| 416 | } |
Heschi Kreinick | 4972913 | 2020-10-26 17:57:45 -0400 | [diff] [blame] | 417 | if err := snapshot.RunGoCommandPiped(ctx, source.Normal, inv, out, out); err != nil { |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 418 | if errors.Is(err, context.Canceled) { |
| 419 | return err |
| 420 | } |
| 421 | failedTests++ |
Rebecca Stambler | 9fe02d1 | 2020-06-12 14:57:48 -0400 | [diff] [blame] | 422 | } |
Rebecca Stambler | 9fe02d1 | 2020-06-12 14:57:48 -0400 | [diff] [blame] | 423 | } |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 424 | |
Rob Findley | ed71c57 | 2020-08-17 22:50:34 -0400 | [diff] [blame] | 425 | // Run `go test -run=^$ -bench Func` on each test. |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 426 | var failedBenchmarks int |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 427 | for _, funcName := range benchmarks { |
Heschi Kreinick | 2b84a06 | 2020-10-26 15:13:16 -0400 | [diff] [blame] | 428 | inv := &gocommand.Invocation{ |
| 429 | Verb: "test", |
| 430 | Args: []string{pkgPath, "-v", "-run=^$", "-bench", fmt.Sprintf("^%s$", funcName)}, |
| 431 | WorkingDir: filepath.Dir(uri.SpanURI().Filename()), |
| 432 | } |
Heschi Kreinick | 4972913 | 2020-10-26 17:57:45 -0400 | [diff] [blame] | 433 | if err := snapshot.RunGoCommandPiped(ctx, source.Normal, inv, out, out); err != nil { |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 434 | if errors.Is(err, context.Canceled) { |
| 435 | return err |
| 436 | } |
| 437 | failedBenchmarks++ |
| 438 | } |
| 439 | } |
| 440 | |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 441 | var title string |
| 442 | if len(tests) > 0 && len(benchmarks) > 0 { |
| 443 | title = "tests and benchmarks" |
| 444 | } else if len(tests) > 0 { |
| 445 | title = "tests" |
| 446 | } else if len(benchmarks) > 0 { |
| 447 | title = "benchmarks" |
| 448 | } else { |
| 449 | return errors.New("No functions were provided") |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 450 | } |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 451 | message := fmt.Sprintf("all %s passed", title) |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 452 | if failedTests > 0 && failedBenchmarks > 0 { |
| 453 | message = fmt.Sprintf("%d / %d tests failed and %d / %d benchmarks failed", failedTests, len(tests), failedBenchmarks, len(benchmarks)) |
| 454 | } else if failedTests > 0 { |
| 455 | message = fmt.Sprintf("%d / %d tests failed", failedTests, len(tests)) |
| 456 | } else if failedBenchmarks > 0 { |
| 457 | message = fmt.Sprintf("%d / %d benchmarks failed", failedBenchmarks, len(benchmarks)) |
| 458 | } |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 459 | if failedTests > 0 || failedBenchmarks > 0 { |
Rob Findley | 1e23e48 | 2020-08-21 14:18:37 -0400 | [diff] [blame] | 460 | message += "\n" + buf.String() |
| 461 | } |
Brayden Cloud | c7ca526 | 2020-07-30 22:48:51 +0000 | [diff] [blame] | 462 | |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 463 | return c.s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ |
Pontus Leitzler | 69daaf9 | 2020-11-11 13:12:26 +0100 | [diff] [blame] | 464 | Type: protocol.Info, |
Martin Asquino | 2bc93b1 | 2020-05-05 19:43:51 +0100 | [diff] [blame] | 465 | Message: message, |
| 466 | }) |
| 467 | } |
| 468 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 469 | func (c *commandHandler) Generate(ctx context.Context, args command.GenerateArgs) error { |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 470 | title := "Running go generate ." |
| 471 | if args.Recursive { |
| 472 | title = "Running go generate ./..." |
| 473 | } |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 474 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 475 | requireSave: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 476 | progress: title, |
| 477 | forURI: args.Dir, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 478 | }, func(ctx context.Context, deps commandDeps) error { |
Rebecca Stambler | 116feae | 2021-05-12 17:00:35 -0400 | [diff] [blame] | 479 | er := progress.NewEventWriter(ctx, "generate") |
Rebecca Stambler | c80dc57 | 2020-07-15 23:33:55 -0400 | [diff] [blame] | 480 | |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 481 | pattern := "." |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 482 | if args.Recursive { |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 483 | pattern = "./..." |
| 484 | } |
| 485 | inv := &gocommand.Invocation{ |
| 486 | Verb: "generate", |
| 487 | Args: []string{"-x", pattern}, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 488 | WorkingDir: args.Dir.SpanURI().Filename(), |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 489 | } |
Robert Findley | 459e2b8 | 2022-07-12 17:13:32 -0400 | [diff] [blame] | 490 | stderr := io.MultiWriter(er, progress.NewWorkDoneWriter(ctx, deps.work)) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 491 | if err := deps.snapshot.RunGoCommandPiped(ctx, source.Normal, inv, er, stderr); err != nil { |
| 492 | return err |
| 493 | } |
| 494 | return nil |
Heschi Kreinick | bd5d160 | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 495 | }) |
Heschi Kreinick | bd5d160 | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 496 | } |
| 497 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 498 | func (c *commandHandler) GoGetPackage(ctx context.Context, args command.GoGetPackageArgs) error { |
| 499 | return c.run(ctx, commandConfig{ |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 500 | forURI: args.URI, |
| 501 | progress: "Running go get", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 502 | }, func(ctx context.Context, deps commandDeps) error { |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 503 | // Run on a throwaway go.mod, otherwise it'll write to the real one. |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 504 | stdout, err := deps.snapshot.RunGoCommandDirect(ctx, source.WriteTemporaryModFile|source.AllowNetwork, &gocommand.Invocation{ |
| 505 | Verb: "list", |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 506 | Args: []string{"-f", "{{.Module.Path}}@{{.Module.Version}}", args.Pkg}, |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 507 | WorkingDir: filepath.Dir(args.URI.SpanURI().Filename()), |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 508 | }) |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | ver := strings.TrimSpace(stdout.String()) |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 513 | return c.s.runGoModUpdateCommands(ctx, deps.snapshot, args.URI.SpanURI(), func(invoke func(...string) (*bytes.Buffer, error)) error { |
Rebecca Stambler | 9eb3535 | 2021-02-18 11:30:08 -0500 | [diff] [blame] | 514 | if args.AddRequire { |
| 515 | if err := addModuleRequire(invoke, []string{ver}); err != nil { |
| 516 | return err |
| 517 | } |
| 518 | } |
| 519 | _, err := invoke(append([]string{"get", "-d"}, args.Pkg)...) |
| 520 | return err |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 521 | }) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 522 | }) |
| 523 | } |
| 524 | |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 525 | func (s *Server) runGoModUpdateCommands(ctx context.Context, snapshot source.Snapshot, uri span.URI, run func(invoke func(...string) (*bytes.Buffer, error)) error) error { |
| 526 | tmpModfile, newModBytes, newSumBytes, err := snapshot.RunGoCommands(ctx, true, filepath.Dir(uri.Filename()), run) |
| 527 | if err != nil { |
| 528 | return err |
| 529 | } |
| 530 | if !tmpModfile { |
| 531 | return nil |
| 532 | } |
| 533 | modURI := snapshot.GoModForFile(uri) |
| 534 | sumURI := span.URIFromPath(strings.TrimSuffix(modURI.Filename(), ".mod") + ".sum") |
| 535 | modEdits, err := applyFileEdits(ctx, snapshot, modURI, newModBytes) |
| 536 | if err != nil { |
| 537 | return err |
| 538 | } |
| 539 | sumEdits, err := applyFileEdits(ctx, snapshot, sumURI, newSumBytes) |
| 540 | if err != nil { |
| 541 | return err |
| 542 | } |
| 543 | changes := append(sumEdits, modEdits...) |
| 544 | if len(changes) == 0 { |
| 545 | return nil |
| 546 | } |
| 547 | response, err := s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{ |
| 548 | Edit: protocol.WorkspaceEdit{ |
| 549 | DocumentChanges: changes, |
| 550 | }, |
| 551 | }) |
| 552 | if err != nil { |
| 553 | return err |
| 554 | } |
| 555 | if !response.Applied { |
| 556 | return fmt.Errorf("edits not applied because of %s", response.FailureReason) |
| 557 | } |
| 558 | return nil |
| 559 | } |
| 560 | |
| 561 | func applyFileEdits(ctx context.Context, snapshot source.Snapshot, uri span.URI, newContent []byte) ([]protocol.TextDocumentEdit, error) { |
| 562 | fh, err := snapshot.GetVersionedFile(ctx, uri) |
| 563 | if err != nil { |
| 564 | return nil, err |
| 565 | } |
| 566 | oldContent, err := fh.Read() |
| 567 | if err != nil && !os.IsNotExist(err) { |
| 568 | return nil, err |
| 569 | } |
| 570 | if bytes.Equal(oldContent, newContent) { |
| 571 | return nil, nil |
| 572 | } |
| 573 | |
| 574 | // Sending a workspace edit to a closed file causes VS Code to open the |
| 575 | // file and leave it unsaved. We would rather apply the changes directly, |
| 576 | // especially to go.sum, which should be mostly invisible to the user. |
| 577 | if !snapshot.IsOpen(uri) { |
| 578 | err := ioutil.WriteFile(uri.Filename(), newContent, 0666) |
| 579 | return nil, err |
| 580 | } |
| 581 | |
Robert Findley | 9d7bf95 | 2022-05-12 23:23:17 -0400 | [diff] [blame] | 582 | m := protocol.NewColumnMapper(fh.URI(), oldContent) |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 583 | diff, err := snapshot.View().Options().ComputeEdits(uri, string(oldContent), string(newContent)) |
| 584 | if err != nil { |
| 585 | return nil, err |
| 586 | } |
| 587 | edits, err := source.ToProtocolEdits(m, diff) |
| 588 | if err != nil { |
| 589 | return nil, err |
| 590 | } |
| 591 | return []protocol.TextDocumentEdit{{ |
| 592 | TextDocument: protocol.OptionalVersionedTextDocumentIdentifier{ |
| 593 | Version: fh.Version(), |
| 594 | TextDocumentIdentifier: protocol.TextDocumentIdentifier{ |
| 595 | URI: protocol.URIFromSpanURI(uri), |
| 596 | }, |
| 597 | }, |
| 598 | Edits: edits, |
| 599 | }}, nil |
| 600 | } |
| 601 | |
| 602 | func runGoGetModule(invoke func(...string) (*bytes.Buffer, error), addRequire bool, args []string) error { |
Heschi Kreinick | a9763ab | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 603 | if addRequire { |
Rebecca Stambler | 9eb3535 | 2021-02-18 11:30:08 -0500 | [diff] [blame] | 604 | if err := addModuleRequire(invoke, args); err != nil { |
Heschi Kreinick | a9763ab | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 605 | return err |
| 606 | } |
| 607 | } |
Heschi Kreinick | 706a59c | 2021-02-05 22:18:47 -0500 | [diff] [blame] | 608 | _, err := invoke(append([]string{"get", "-d"}, args...)...) |
Heschi Kreinick | a9763ab | 2020-11-10 18:20:37 -0500 | [diff] [blame] | 609 | return err |
| 610 | } |
Heschi Kreinick | f1f686b | 2021-01-25 15:51:00 -0500 | [diff] [blame] | 611 | |
Rebecca Stambler | 9eb3535 | 2021-02-18 11:30:08 -0500 | [diff] [blame] | 612 | func addModuleRequire(invoke func(...string) (*bytes.Buffer, error), args []string) error { |
| 613 | // Using go get to create a new dependency results in an |
| 614 | // `// indirect` comment we may not want. The only way to avoid it |
| 615 | // is to add the require as direct first. Then we can use go get to |
| 616 | // update go.sum and tidy up. |
| 617 | _, err := invoke(append([]string{"mod", "edit", "-require"}, args...)...) |
| 618 | return err |
| 619 | } |
| 620 | |
Heschi Kreinick | f1f686b | 2021-01-25 15:51:00 -0500 | [diff] [blame] | 621 | func (s *Server) getUpgrades(ctx context.Context, snapshot source.Snapshot, uri span.URI, modules []string) (map[string]string, error) { |
| 622 | stdout, err := snapshot.RunGoCommandDirect(ctx, source.Normal|source.AllowNetwork, &gocommand.Invocation{ |
| 623 | Verb: "list", |
| 624 | Args: append([]string{"-m", "-u", "-json"}, modules...), |
| 625 | WorkingDir: filepath.Dir(uri.Filename()), |
Heschi Kreinick | f7e8e24 | 2021-04-20 14:27:24 -0400 | [diff] [blame] | 626 | ModFlag: "readonly", |
Heschi Kreinick | f1f686b | 2021-01-25 15:51:00 -0500 | [diff] [blame] | 627 | }) |
| 628 | if err != nil { |
| 629 | return nil, err |
| 630 | } |
| 631 | |
| 632 | upgrades := map[string]string{} |
| 633 | for dec := json.NewDecoder(stdout); dec.More(); { |
| 634 | mod := &gocommand.ModuleJSON{} |
| 635 | if err := dec.Decode(mod); err != nil { |
| 636 | return nil, err |
| 637 | } |
| 638 | if mod.Update == nil { |
| 639 | continue |
| 640 | } |
| 641 | upgrades[mod.Path] = mod.Update.Version |
| 642 | } |
| 643 | return upgrades, nil |
| 644 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 645 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 646 | func (c *commandHandler) GCDetails(ctx context.Context, uri protocol.DocumentURI) error { |
| 647 | return c.ToggleGCDetails(ctx, command.URIArg{URI: uri}) |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 648 | } |
| 649 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 650 | func (c *commandHandler) ToggleGCDetails(ctx context.Context, args command.URIArg) error { |
| 651 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 652 | requireSave: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 653 | progress: "Toggling GC Details", |
| 654 | forURI: args.URI, |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 655 | }, func(ctx context.Context, deps commandDeps) error { |
Heschi Kreinick | 2ac05c8 | 2021-02-28 00:10:04 -0500 | [diff] [blame] | 656 | pkg, err := deps.snapshot.PackageForFile(ctx, deps.fh.URI(), source.TypecheckWorkspace, source.NarrowestPackage) |
| 657 | if err != nil { |
| 658 | return err |
| 659 | } |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 660 | c.s.gcOptimizationDetailsMu.Lock() |
Heschi Kreinick | 2ac05c8 | 2021-02-28 00:10:04 -0500 | [diff] [blame] | 661 | if _, ok := c.s.gcOptimizationDetails[pkg.ID()]; ok { |
| 662 | delete(c.s.gcOptimizationDetails, pkg.ID()) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 663 | c.s.clearDiagnosticSource(gcDetailsSource) |
| 664 | } else { |
Heschi Kreinick | 2ac05c8 | 2021-02-28 00:10:04 -0500 | [diff] [blame] | 665 | c.s.gcOptimizationDetails[pkg.ID()] = struct{}{} |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 666 | } |
| 667 | c.s.gcOptimizationDetailsMu.Unlock() |
| 668 | c.s.diagnoseSnapshot(deps.snapshot, nil, false) |
| 669 | return nil |
| 670 | }) |
| 671 | } |
| 672 | |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 673 | func (c *commandHandler) GenerateGoplsMod(ctx context.Context, args command.URIArg) error { |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 674 | // TODO: go back to using URI |
Rob Findley | 5fbed49 | 2021-02-09 17:25:52 -0500 | [diff] [blame] | 675 | return c.run(ctx, commandConfig{ |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 676 | requireSave: true, |
Rob Findley | 8aef11f | 2021-02-05 17:55:31 -0500 | [diff] [blame] | 677 | progress: "Generating gopls.mod", |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 678 | }, func(ctx context.Context, deps commandDeps) error { |
| 679 | views := c.s.session.Views() |
| 680 | if len(views) != 1 { |
| 681 | return fmt.Errorf("cannot resolve view: have %d views", len(views)) |
| 682 | } |
| 683 | v := views[0] |
| 684 | snapshot, release := v.Snapshot(ctx) |
| 685 | defer release() |
Rebecca Stambler | 4b484fb | 2021-06-11 00:40:37 -0400 | [diff] [blame] | 686 | modFile, err := snapshot.BuildGoplsMod(ctx) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 687 | if err != nil { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 688 | return fmt.Errorf("getting workspace mod file: %w", err) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 689 | } |
| 690 | content, err := modFile.Format() |
| 691 | if err != nil { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 692 | return fmt.Errorf("formatting mod file: %w", err) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 693 | } |
Alan Donovan | 53ead67 | 2022-07-05 10:30:58 -0400 | [diff] [blame] | 694 | filename := filepath.Join(v.Folder().Filename(), "gopls.mod") |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 695 | if err := ioutil.WriteFile(filename, content, 0644); err != nil { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 696 | return fmt.Errorf("writing mod file: %w", err) |
Rob Findley | a30116d | 2021-02-04 19:35:05 -0500 | [diff] [blame] | 697 | } |
| 698 | return nil |
| 699 | }) |
| 700 | } |
Rob Findley | 8316e56 | 2021-02-10 19:22:19 -0500 | [diff] [blame] | 701 | |
| 702 | func (c *commandHandler) ListKnownPackages(ctx context.Context, args command.URIArg) (command.ListKnownPackagesResult, error) { |
| 703 | var result command.ListKnownPackagesResult |
| 704 | err := c.run(ctx, commandConfig{ |
Marwan Sulaiman | 4061312 | 2021-05-15 10:04:17 -0400 | [diff] [blame] | 705 | progress: "Listing packages", |
| 706 | forURI: args.URI, |
Rob Findley | 8316e56 | 2021-02-10 19:22:19 -0500 | [diff] [blame] | 707 | }, func(ctx context.Context, deps commandDeps) error { |
Marwan Sulaiman | 4061312 | 2021-05-15 10:04:17 -0400 | [diff] [blame] | 708 | var err error |
| 709 | result.Packages, err = source.KnownPackages(ctx, deps.snapshot, deps.fh) |
| 710 | return err |
Rob Findley | 8316e56 | 2021-02-10 19:22:19 -0500 | [diff] [blame] | 711 | }) |
| 712 | return result, err |
| 713 | } |
Heschi Kreinick | 414ec9c | 2022-02-04 15:23:09 -0500 | [diff] [blame] | 714 | |
| 715 | func (c *commandHandler) ListImports(ctx context.Context, args command.URIArg) (command.ListImportsResult, error) { |
| 716 | var result command.ListImportsResult |
| 717 | err := c.run(ctx, commandConfig{ |
| 718 | forURI: args.URI, |
| 719 | }, func(ctx context.Context, deps commandDeps) error { |
| 720 | pkg, err := deps.snapshot.PackageForFile(ctx, args.URI.SpanURI(), source.TypecheckWorkspace, source.NarrowestPackage) |
| 721 | if err != nil { |
| 722 | return err |
| 723 | } |
| 724 | pgf, err := pkg.File(args.URI.SpanURI()) |
| 725 | if err != nil { |
| 726 | return err |
| 727 | } |
| 728 | for _, group := range astutil.Imports(deps.snapshot.FileSet(), pgf.File) { |
| 729 | for _, imp := range group { |
| 730 | if imp.Path == nil { |
| 731 | continue |
| 732 | } |
| 733 | var name string |
| 734 | if imp.Name != nil { |
| 735 | name = imp.Name.Name |
| 736 | } |
| 737 | result.Imports = append(result.Imports, command.FileImport{ |
| 738 | Path: source.ImportPath(imp), |
| 739 | Name: name, |
| 740 | }) |
| 741 | } |
| 742 | } |
| 743 | for _, imp := range pkg.Imports() { |
| 744 | result.PackageImports = append(result.PackageImports, command.PackageImport{ |
| 745 | Path: imp.PkgPath(), // This might be the vendored path under GOPATH vendoring, in which case it's a bug. |
| 746 | }) |
| 747 | } |
| 748 | sort.Slice(result.PackageImports, func(i, j int) bool { |
| 749 | return result.PackageImports[i].Path < result.PackageImports[j].Path |
| 750 | }) |
| 751 | return nil |
| 752 | }) |
| 753 | return result, err |
| 754 | } |
| 755 | |
Marwan Sulaiman | 4061312 | 2021-05-15 10:04:17 -0400 | [diff] [blame] | 756 | func (c *commandHandler) AddImport(ctx context.Context, args command.AddImportArgs) error { |
| 757 | return c.run(ctx, commandConfig{ |
Rob Findley | 8316e56 | 2021-02-10 19:22:19 -0500 | [diff] [blame] | 758 | progress: "Adding import", |
| 759 | forURI: args.URI, |
| 760 | }, func(ctx context.Context, deps commandDeps) error { |
Marwan Sulaiman | 4061312 | 2021-05-15 10:04:17 -0400 | [diff] [blame] | 761 | edits, err := source.AddImport(ctx, deps.snapshot, deps.fh, args.ImportPath) |
| 762 | if err != nil { |
| 763 | return fmt.Errorf("could not add import: %v", err) |
| 764 | } |
| 765 | if _, err := c.s.client.ApplyEdit(ctx, &protocol.ApplyWorkspaceEditParams{ |
| 766 | Edit: protocol.WorkspaceEdit{ |
| 767 | DocumentChanges: documentChanges(deps.fh, edits), |
| 768 | }, |
| 769 | }); err != nil { |
| 770 | return fmt.Errorf("could not apply import edits: %v", err) |
| 771 | } |
Rob Findley | 8316e56 | 2021-02-10 19:22:19 -0500 | [diff] [blame] | 772 | return nil |
| 773 | }) |
Rob Findley | 8316e56 | 2021-02-10 19:22:19 -0500 | [diff] [blame] | 774 | } |
Rob Findley | 6d45e3d | 2021-03-09 14:09:48 -0500 | [diff] [blame] | 775 | |
Rob Findley | 7c93484 | 2021-04-12 12:38:22 -0400 | [diff] [blame] | 776 | func (c *commandHandler) StartDebugging(ctx context.Context, args command.DebuggingArgs) (result command.DebuggingResult, _ error) { |
| 777 | addr := args.Addr |
| 778 | if addr == "" { |
| 779 | addr = "localhost:0" |
| 780 | } |
| 781 | di := debug.GetInstance(ctx) |
| 782 | if di == nil { |
| 783 | return result, errors.New("internal error: server has no debugging instance") |
| 784 | } |
| 785 | listenedAddr, err := di.Serve(ctx, addr) |
| 786 | if err != nil { |
Robert Findley | 37590b3 | 2022-04-19 18:08:06 -0400 | [diff] [blame] | 787 | return result, fmt.Errorf("starting debug server: %w", err) |
Rob Findley | 7c93484 | 2021-04-12 12:38:22 -0400 | [diff] [blame] | 788 | } |
| 789 | result.URLs = []string{"http://" + listenedAddr} |
| 790 | return result, nil |
| 791 | } |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 792 | |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 793 | // Copy of pkgLoadConfig defined in internal/lsp/cmd/vulncheck.go |
| 794 | // TODO(hyangah): decide where to define this. |
| 795 | type pkgLoadConfig struct { |
| 796 | // BuildFlags is a list of command-line flags to be passed through to |
| 797 | // the build system's query tool. |
| 798 | BuildFlags []string |
| 799 | |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 800 | // If Tests is set, the loader includes related test packages. |
| 801 | Tests bool |
| 802 | } |
| 803 | |
| 804 | func (c *commandHandler) RunVulncheckExp(ctx context.Context, args command.VulncheckArgs) error { |
Hana (Hyang-Ah) Kim | b5fd088 | 2022-08-02 22:39:32 -0400 | [diff] [blame] | 805 | if args.URI == "" { |
| 806 | return errors.New("VulncheckArgs is missing URI field") |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 807 | } |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 808 | err := c.run(ctx, commandConfig{ |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 809 | async: true, // need to be async to be cancellable |
Hana (Hyang-Ah) Kim | 81c7dc4 | 2022-08-03 00:01:48 -0400 | [diff] [blame] | 810 | progress: "govulncheck", |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 811 | requireSave: true, |
Hana (Hyang-Ah) Kim | b5fd088 | 2022-08-02 22:39:32 -0400 | [diff] [blame] | 812 | forURI: args.URI, |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 813 | }, func(ctx context.Context, deps commandDeps) error { |
| 814 | view := deps.snapshot.View() |
| 815 | opts := view.Options() |
| 816 | if opts == nil || opts.Hooks.Govulncheck == nil { |
| 817 | return errors.New("vulncheck feature is not available") |
| 818 | } |
| 819 | |
Hana (Hyang-Ah) Kim | af2a0a8 | 2022-08-02 23:28:18 -0400 | [diff] [blame] | 820 | cmd := exec.CommandContext(ctx, os.Args[0], "vulncheck", "-config", args.Pattern) |
Hana (Hyang-Ah) Kim | b5fd088 | 2022-08-02 22:39:32 -0400 | [diff] [blame] | 821 | cmd.Dir = filepath.Dir(args.URI.SpanURI().Filename()) |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 822 | |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 823 | var viewEnv []string |
| 824 | if e := opts.EnvSlice(); e != nil { |
| 825 | viewEnv = append(os.Environ(), e...) |
| 826 | } |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 827 | cmd.Env = viewEnv |
| 828 | |
| 829 | // stdin: gopls vulncheck expects JSON-encoded configuration from STDIN when -config flag is set. |
| 830 | var stdin bytes.Buffer |
| 831 | cmd.Stdin = &stdin |
| 832 | |
| 833 | if err := json.NewEncoder(&stdin).Encode(pkgLoadConfig{ |
| 834 | BuildFlags: opts.BuildFlags, |
| 835 | // TODO(hyangah): add `tests` flag in command.VulncheckArgs |
| 836 | }); err != nil { |
| 837 | return fmt.Errorf("failed to pass package load config: %v", err) |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 838 | } |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 839 | |
| 840 | // stderr: stream gopls vulncheck's STDERR as progress reports |
| 841 | er := progress.NewEventWriter(ctx, "vulncheck") |
| 842 | stderr := io.MultiWriter(er, progress.NewWorkDoneWriter(ctx, deps.work)) |
| 843 | cmd.Stderr = stderr |
| 844 | // TODO: can we stream stdout? |
| 845 | stdout, err := cmd.Output() |
| 846 | if err != nil { |
| 847 | return fmt.Errorf("failed to run govulncheck: %v", err) |
| 848 | } |
| 849 | |
| 850 | var vulns command.VulncheckResult |
| 851 | if err := json.Unmarshal(stdout, &vulns); err != nil { |
| 852 | // TODO: for easy debugging, log the failed stdout somewhere? |
| 853 | return fmt.Errorf("failed to parse govulncheck output: %v", err) |
| 854 | } |
| 855 | |
Hana (Hyang-Ah) Kim | 6c27717 | 2022-08-02 23:15:52 -0400 | [diff] [blame] | 856 | // TODO(jamalc,suzmue): convert the results to diagnostics & code actions. |
| 857 | // Or should we just write to a file (*.vulncheck.json) or text format |
| 858 | // and send "Show Document" request? If *.vulncheck.json is open, |
| 859 | // VSCode Go extension will open its custom editor. |
| 860 | set := make(map[string]bool) |
| 861 | for _, v := range vulns.Vuln { |
| 862 | if len(v.CallStackSummaries) > 0 { |
| 863 | set[v.ID] = true |
| 864 | } |
| 865 | } |
| 866 | if len(set) == 0 { |
| 867 | return c.s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ |
| 868 | Type: protocol.Info, |
| 869 | Message: "No vulnerabilities found", |
| 870 | }) |
| 871 | } |
| 872 | |
| 873 | list := make([]string, 0, len(set)) |
| 874 | for k := range set { |
| 875 | list = append(list, k) |
| 876 | } |
| 877 | sort.Strings(list) |
| 878 | return c.s.client.ShowMessage(ctx, &protocol.ShowMessageParams{ |
| 879 | Type: protocol.Warning, |
| 880 | Message: fmt.Sprintf("Found %v", strings.Join(list, ", ")), |
| 881 | }) |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 882 | }) |
Hana (Hyang-Ah) Kim | bceee4b | 2022-08-02 12:19:22 -0400 | [diff] [blame] | 883 | return err |
Hana | cd31eaa | 2022-03-24 12:24:17 -0400 | [diff] [blame] | 884 | } |