fixImports formats and adjusts imports for the current AST.
()
| 633 | |
| 634 | // fixImports formats and adjusts imports for the current AST. |
| 635 | func (s *Session) fixImports() error { |
| 636 | // Fix against error: no required module provides package ...; try 'go get -d ...' |
| 637 | for _, path := range s.requiredModules { |
| 638 | cmd := exec.Command("go", "get", "-d", path) |
| 639 | cmd.Dir = s.tempDir |
| 640 | if err := cmd.Run(); err != nil { |
| 641 | debugf("failed to go get -d %q: %s", path, err) |
| 642 | } |
| 643 | } |
| 644 | s.requiredModules = nil |
| 645 | |
| 646 | var buf bytes.Buffer |
| 647 | err := printer.Fprint(&buf, s.fset, s.file) |
| 648 | if err != nil { |
| 649 | return err |
| 650 | } |
| 651 | |
| 652 | formatted, err := imports.Process("", buf.Bytes(), nil) |
| 653 | if err != nil { |
| 654 | return err |
| 655 | } |
| 656 | |
| 657 | s.file, err = parser.ParseFile(s.fset, "", formatted, parser.Mode(0)) |
| 658 | if err != nil { |
| 659 | return err |
| 660 | } |
| 661 | s.mainBody = s.mainFunc().Body |
| 662 | |
| 663 | return nil |
| 664 | } |
| 665 | |
| 666 | func (s *Session) includePackage(path string) error { |
| 667 | pkg, err := build.Import(path, ".", 0) |