@@ -13,7 +13,6 @@ import (
13
13
"go/types"
14
14
"path"
15
15
"path/filepath"
16
- "regexp"
17
16
"sort"
18
17
"strings"
19
18
"sync"
@@ -22,7 +21,6 @@ import (
22
21
"golang.org/x/tools/go/ast/astutil"
23
22
"golang.org/x/tools/go/packages"
24
23
"golang.org/x/tools/internal/event"
25
- "golang.org/x/tools/internal/lsp/command"
26
24
"golang.org/x/tools/internal/lsp/debug/tag"
27
25
"golang.org/x/tools/internal/lsp/protocol"
28
26
"golang.org/x/tools/internal/lsp/source"
@@ -509,44 +507,10 @@ func typeCheck(ctx context.Context, snapshot *snapshot, m *metadata, mode source
509
507
return nil , err
510
508
}
511
509
pkg .diagnostics = append (pkg .diagnostics , depsErrors ... )
512
- if err := addGoGetFixes (ctx , snapshot , pkg ); err != nil {
513
- return nil , err
514
- }
515
510
516
511
return pkg , nil
517
512
}
518
513
519
- var importErrorRe = regexp .MustCompile (`could not import ([^\s]+)` )
520
- var missingModuleErrorRe = regexp .MustCompile (`cannot find module providing package ([^\s:]+)` )
521
-
522
- func addGoGetFixes (ctx context.Context , snapshot source.Snapshot , pkg * pkg ) error {
523
- if len (pkg .compiledGoFiles ) == 0 || snapshot .GoModForFile (pkg .compiledGoFiles [0 ].URI ) == "" {
524
- // Go get only supports module mode for now.
525
- return nil
526
- }
527
- for _ , diag := range pkg .diagnostics {
528
- matches := importErrorRe .FindStringSubmatch (diag .Message )
529
- if len (matches ) == 0 {
530
- matches = missingModuleErrorRe .FindStringSubmatch (diag .Message )
531
- }
532
- if len (matches ) == 0 {
533
- continue
534
- }
535
- direct := ! strings .Contains (diag .Message , "error while importing" )
536
- title := fmt .Sprintf ("go get package %v" , matches [1 ])
537
- cmd , err := command .NewGoGetPackageCommand (title , command.GoGetPackageArgs {
538
- URI : protocol .URIFromSpanURI (pkg .compiledGoFiles [0 ].URI ),
539
- AddRequire : direct ,
540
- Pkg : matches [1 ],
541
- })
542
- if err != nil {
543
- return err
544
- }
545
- diag .SuggestedFixes = append (diag .SuggestedFixes , source .SuggestedFixFromCommand (cmd ))
546
- }
547
- return nil
548
- }
549
-
550
514
func (s * snapshot ) depsErrors (ctx context.Context , pkg * pkg ) ([]* source.Diagnostic , error ) {
551
515
// Select packages that can't be found, and were imported in non-workspace packages.
552
516
// Workspace packages already show their own errors.
@@ -592,28 +556,34 @@ func (s *snapshot) depsErrors(ctx context.Context, pkg *pkg) ([]*source.Diagnost
592
556
}
593
557
}
594
558
595
- // Apply a diagnostic to any import involved in the error, stopping after
559
+ // Apply a diagnostic to any import involved in the error, stopping once
596
560
// we reach the workspace.
597
561
var errors []* source.Diagnostic
598
562
for _ , depErr := range relevantErrors {
599
563
for i := len (depErr .ImportStack ) - 1 ; i >= 0 ; i -- {
600
564
item := depErr .ImportStack [i ]
565
+ if _ , ok := s .isWorkspacePackage (packageID (item )); ok {
566
+ break
567
+ }
568
+
601
569
for _ , imp := range allImports [item ] {
602
570
rng , err := source .NewMappedRange (s .FileSet (), imp .cgf .Mapper , imp .imp .Pos (), imp .imp .End ()).Range ()
603
571
if err != nil {
604
572
return nil , err
605
573
}
574
+ fixes , err := goGetQuickFixes (s , imp .cgf .URI , item )
575
+ if err != nil {
576
+ return nil , err
577
+ }
606
578
errors = append (errors , & source.Diagnostic {
607
- URI : imp .cgf .URI ,
608
- Range : rng ,
609
- Severity : protocol .SeverityError ,
610
- Source : source .TypeError ,
611
- Message : fmt .Sprintf ("error while importing %v: %v" , item , depErr .Err ),
579
+ URI : imp .cgf .URI ,
580
+ Range : rng ,
581
+ Severity : protocol .SeverityError ,
582
+ Source : source .TypeError ,
583
+ Message : fmt .Sprintf ("error while importing %v: %v" , item , depErr .Err ),
584
+ SuggestedFixes : fixes ,
612
585
})
613
586
}
614
- if _ , ok := s .isWorkspacePackage (packageID (item )); ok {
615
- break
616
- }
617
587
}
618
588
}
619
589
@@ -651,12 +621,17 @@ func (s *snapshot) depsErrors(ctx context.Context, pkg *pkg) ([]*source.Diagnost
651
621
if err != nil {
652
622
return nil , err
653
623
}
624
+ fixes , err := goGetQuickFixes (s , pm .URI , item )
625
+ if err != nil {
626
+ return nil , err
627
+ }
654
628
errors = append (errors , & source.Diagnostic {
655
- URI : pm .URI ,
656
- Range : rng ,
657
- Severity : protocol .SeverityError ,
658
- Source : source .TypeError ,
659
- Message : fmt .Sprintf ("error while importing %v: %v" , item , depErr .Err ),
629
+ URI : pm .URI ,
630
+ Range : rng ,
631
+ Severity : protocol .SeverityError ,
632
+ Source : source .TypeError ,
633
+ Message : fmt .Sprintf ("error while importing %v: %v" , item , depErr .Err ),
634
+ SuggestedFixes : fixes ,
660
635
})
661
636
break
662
637
}
0 commit comments