@@ -192,34 +192,35 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
192
192
}
193
193
// The flow for `go mod tidy` and `go mod vendor` is almost identical,
194
194
// so we combine them into one case for convenience.
195
- a := "tidy"
195
+ action := "tidy"
196
196
if command == source .CommandVendor {
197
- a = "vendor"
197
+ action = "vendor"
198
198
}
199
- return s .directGoModCommand (ctx , uri , "mod" , a )
199
+ snapshot , _ , ok , release , err := s .beginFileRequest (ctx , uri , source .UnknownKind )
200
+ defer release ()
201
+ if ! ok {
202
+ return err
203
+ }
204
+ return runSimpleGoCommand (ctx , snapshot , source .UpdateUserModFile , uri .SpanURI (), "mod" , []string {action })
200
205
case source .CommandAddDependency , source .CommandUpgradeDependency , source .CommandRemoveDependency :
201
206
var uri protocol.DocumentURI
202
207
var goCmdArgs []string
203
208
var addRequire bool
204
209
if err := source .UnmarshalArgs (args , & uri , & addRequire , & goCmdArgs ); err != nil {
205
210
return err
206
211
}
207
- if addRequire {
208
- // Using go get to create a new dependency results in an
209
- // `// indirect` comment we may not want. The only way to avoid it
210
- // is to add the require as direct first. Then we can use go get to
211
- // update go.sum and tidy up.
212
- if err := s .directGoModCommand (ctx , uri , "mod" , append ([]string {"edit" , "-require" }, goCmdArgs ... )... ); err != nil {
213
- return err
214
- }
212
+ snapshot , _ , ok , release , err := s .beginFileRequest (ctx , uri , source .UnknownKind )
213
+ defer release ()
214
+ if ! ok {
215
+ return err
215
216
}
216
- return s .directGoModCommand (ctx , uri , "get" , append ([] string { "-d" } , goCmdArgs ... ) ... )
217
+ return s .runGoGetModule (ctx , snapshot , uri . SpanURI (), addRequire , goCmdArgs )
217
218
case source .CommandToggleDetails :
218
- var fileURI span. URI
219
+ var fileURI protocol. DocumentURI
219
220
if err := source .UnmarshalArgs (args , & fileURI ); err != nil {
220
221
return err
221
222
}
222
- pkgDir := span .URIFromPath (filepath .Dir (fileURI .Filename ()))
223
+ pkgDir := span .URIFromPath (filepath .Dir (fileURI .SpanURI (). Filename ()))
223
224
s .gcOptimizationDetailsMu .Lock ()
224
225
if _ , ok := s .gcOptimizationDetails [pkgDir ]; ok {
225
226
delete (s .gcOptimizationDetails , pkgDir )
@@ -229,12 +230,11 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
229
230
s .gcOptimizationDetailsMu .Unlock ()
230
231
// need to recompute diagnostics.
231
232
// so find the snapshot
232
- sv , err := s .session .ViewOf (fileURI )
233
- if err != nil {
233
+ snapshot , _ , ok , release , err := s .beginFileRequest (ctx , fileURI , source .UnknownKind )
234
+ defer release ()
235
+ if ! ok {
234
236
return err
235
237
}
236
- snapshot , release := sv .Snapshot (ctx )
237
- defer release ()
238
238
s .diagnoseSnapshot (snapshot , nil , false )
239
239
case source .CommandGenerateGoplsMod :
240
240
var v source.View
@@ -275,21 +275,6 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
275
275
return nil
276
276
}
277
277
278
- func (s * Server ) directGoModCommand (ctx context.Context , uri protocol.DocumentURI , verb string , args ... string ) error {
279
- view , err := s .session .ViewOf (uri .SpanURI ())
280
- if err != nil {
281
- return err
282
- }
283
- snapshot , release := view .Snapshot (ctx )
284
- defer release ()
285
- _ , err = snapshot .RunGoCommandDirect (ctx , source .UpdateUserModFile , & gocommand.Invocation {
286
- Verb : verb ,
287
- Args : args ,
288
- WorkingDir : filepath .Dir (uri .SpanURI ().Filename ()),
289
- })
290
- return err
291
- }
292
-
293
278
func (s * Server ) runTests (ctx context.Context , snapshot source.Snapshot , uri protocol.DocumentURI , work * workDone , tests , benchmarks []string ) error {
294
279
pkgs , err := snapshot .PackagesForFile (ctx , uri .SpanURI (), source .TypecheckWorkspace )
295
280
if err != nil {
@@ -387,3 +372,25 @@ func (s *Server) runGoGenerate(ctx context.Context, snapshot source.Snapshot, di
387
372
}
388
373
return nil
389
374
}
375
+
376
+ func (s * Server ) runGoGetModule (ctx context.Context , snapshot source.Snapshot , uri span.URI , addRequire bool , args []string ) error {
377
+ if addRequire {
378
+ // Using go get to create a new dependency results in an
379
+ // `// indirect` comment we may not want. The only way to avoid it
380
+ // is to add the require as direct first. Then we can use go get to
381
+ // update go.sum and tidy up.
382
+ if err := runSimpleGoCommand (ctx , snapshot , source .UpdateUserModFile , uri , "mod" , append ([]string {"edit" , "-require" }, args ... )); err != nil {
383
+ return err
384
+ }
385
+ }
386
+ return runSimpleGoCommand (ctx , snapshot , source .UpdateUserModFile , uri , "get" , append ([]string {"-d" }, args ... ))
387
+ }
388
+
389
+ func runSimpleGoCommand (ctx context.Context , snapshot source.Snapshot , mode source.InvocationMode , uri span.URI , verb string , args []string ) error {
390
+ _ , err := snapshot .RunGoCommandDirect (ctx , mode , & gocommand.Invocation {
391
+ Verb : verb ,
392
+ Args : args ,
393
+ WorkingDir : filepath .Dir (uri .Filename ()),
394
+ })
395
+ return err
396
+ }
0 commit comments