Skip to content

Commit

Permalink
Falling back to the old solution on error, due to the fact that types…
Browse files Browse the repository at this point in the history
….Importer does not support locally vendored packages
  • Loading branch information
Lars Gohr committed Aug 7, 2018
1 parent bbf96a5 commit dbb9f05
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions locator/locator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

"github.com/maxbrunsfeld/counterfeiter/model"
"go/build"
"path"
)

func GetInterfaceFromFilePath(interfaceName, filePath string) (*model.InterfaceToFake, error) {
Expand Down Expand Up @@ -224,6 +225,7 @@ func getImports(file *ast.File, fset *token.FileSet) map[string]*ast.ImportSpec
ast.Inspect(file, func(node ast.Node) bool {
if importSpec, ok := node.(*ast.ImportSpec); ok {
if importSpec.Name == nil {
result[path.Base(strings.Trim(importSpec.Path.Value, `"`))] = importSpec
importForeignPackages(file, importSpec, fset, result)
} else {
result[importSpec.Name.Name] = importSpec
Expand All @@ -236,15 +238,9 @@ func getImports(file *ast.File, fset *token.FileSet) map[string]*ast.ImportSpec

func importForeignPackages(file *ast.File, importSpec *ast.ImportSpec, fset *token.FileSet, result map[string]*ast.ImportSpec) {
files := []*ast.File{file}
conf := types.Config{
Importer: importer.For("source", nil),
}
pkg, err := conf.Check(importSpec.Path.Value, fset, files, nil)
if err != nil {
// ignoring corrupted packages instead of failing
fmt.Printf("could not get imports for: %v (%v) - %v", file.Name.Name, importSpec.Path.Value, err)
return
}
conf := types.Config{Importer: importer.For("source", nil)}

pkg, _ := conf.Check(importSpec.Path.Value, fset, files, nil)
for _, pkg := range pkg.Imports() {
if !existsInResult(result, pkg) {
result[pkg.Name()] = importSpec
Expand Down

0 comments on commit dbb9f05

Please sign in to comment.