Skip to content
This repository has been archived by the owner on Oct 27, 2021. It is now read-only.

Commit

Permalink
use source importer when default importer fails
Browse files Browse the repository at this point in the history
  • Loading branch information
stamblerre committed Oct 14, 2018
1 parent ca89792 commit a07fefd
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion internal/gbimporter/gbimporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gbimporter
import (
"fmt"
"go/build"
goimporter "go/importer"
"go/types"
"path/filepath"
"strings"
Expand All @@ -13,6 +14,8 @@ import (
// intended, so use a lock to protect against concurrent accesses.
var buildDefaultLock sync.Mutex

var srcImporter types.ImporterFrom = goimporter.For("source", nil).(types.ImporterFrom)

// importer implements types.ImporterFrom and provides transparent
// support for gb-based projects.
type importer struct {
Expand Down Expand Up @@ -81,7 +84,12 @@ func (i *importer) ImportFrom(path, srcDir string, mode types.ImportMode) (*type
def.SplitPathList = i.splitPathList
def.JoinPath = i.joinPath

return i.underlying.ImportFrom(path, srcDir, mode)
pkg, err := i.underlying.ImportFrom(path, srcDir, mode)
if pkg == nil {
// If importing fails, try importing with source importer.
pkg, _ = srcImporter.ImportFrom(path, srcDir, mode)
}
return pkg, err
}

func (i *importer) splitPathList(list string) []string {
Expand Down

0 comments on commit a07fefd

Please sign in to comment.