@@ -192,34 +192,35 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
192192 }
193193 // The flow for `go mod tidy` and `go mod vendor` is almost identical,
194194 // so we combine them into one case for convenience.
195- a := "tidy"
195+ action := "tidy"
196196 if command == source .CommandVendor {
197- a = "vendor"
197+ action = "vendor"
198198 }
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 })
200205 case source .CommandAddDependency , source .CommandUpgradeDependency , source .CommandRemoveDependency :
201206 var uri protocol.DocumentURI
202207 var goCmdArgs []string
203208 var addRequire bool
204209 if err := source .UnmarshalArgs (args , & uri , & addRequire , & goCmdArgs ); err != nil {
205210 return err
206211 }
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
215216 }
216- return s .directGoModCommand (ctx , uri , "get" , append ([] string { "-d" } , goCmdArgs ... ) ... )
217+ return s .runGoGetModule (ctx , snapshot , uri . SpanURI (), addRequire , goCmdArgs )
217218 case source .CommandToggleDetails :
218- var fileURI span. URI
219+ var fileURI protocol. DocumentURI
219220 if err := source .UnmarshalArgs (args , & fileURI ); err != nil {
220221 return err
221222 }
222- pkgDir := span .URIFromPath (filepath .Dir (fileURI .Filename ()))
223+ pkgDir := span .URIFromPath (filepath .Dir (fileURI .SpanURI (). Filename ()))
223224 s .gcOptimizationDetailsMu .Lock ()
224225 if _ , ok := s .gcOptimizationDetails [pkgDir ]; ok {
225226 delete (s .gcOptimizationDetails , pkgDir )
@@ -229,12 +230,11 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
229230 s .gcOptimizationDetailsMu .Unlock ()
230231 // need to recompute diagnostics.
231232 // 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 {
234236 return err
235237 }
236- snapshot , release := sv .Snapshot (ctx )
237- defer release ()
238238 s .diagnoseSnapshot (snapshot , nil , false )
239239 case source .CommandGenerateGoplsMod :
240240 var v source.View
@@ -275,21 +275,6 @@ func (s *Server) runCommand(ctx context.Context, work *workDone, command *source
275275 return nil
276276}
277277
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-
293278func (s * Server ) runTests (ctx context.Context , snapshot source.Snapshot , uri protocol.DocumentURI , work * workDone , tests , benchmarks []string ) error {
294279 pkgs , err := snapshot .PackagesForFile (ctx , uri .SpanURI (), source .TypecheckWorkspace )
295280 if err != nil {
@@ -387,3 +372,25 @@ func (s *Server) runGoGenerate(ctx context.Context, snapshot source.Snapshot, di
387372 }
388373 return nil
389374}
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