diff options
author | Ayke van Laethem <[email protected]> | 2019-12-31 17:44:34 +0100 |
---|---|---|
committer | Ron Evans <[email protected]> | 2020-01-04 00:01:07 +0100 |
commit | 69c1d802e13155c016657de9b29849eae00d3153 (patch) | |
tree | 225d48ff907cfb4f40d666d018e96bb6b117e671 /loader | |
parent | b424056721de48b883ba8172e989386c5d471abc (diff) | |
download | tinygo-69c1d802e13155c016657de9b29849eae00d3153.tar.gz tinygo-69c1d802e13155c016657de9b29849eae00d3153.zip |
loader: improve error messages for failed imports
Add location information (whenever possible) to failed imports. This
helps in debugging where an incorrect import came from.
For example, show the following error message:
/home/ayke/src/github.com/tinygo-org/tinygo/src/machine/machine.go:5:8: cannot find package "foobar" in any of:
/usr/local/go/src/foobar (from $GOROOT)
/home/ayke/src/foobar (from $GOPATH)
Instead of the following:
error: cannot find package "foobar" in any of:
/usr/local/go/src/foobar (from $GOROOT)
/home/ayke/src/foobar (from $GOPATH)
Diffstat (limited to 'loader')
-rw-r--r-- | loader/loader.go | 17 |
1 files changed, 14 insertions, 3 deletions
diff --git a/loader/loader.go b/loader/loader.go index 430e73d96..8c13cb813 100644 --- a/loader/loader.go +++ b/loader/loader.go @@ -6,6 +6,7 @@ import ( "go/ast" "go/build" "go/parser" + "go/scanner" "go/token" "go/types" "os" @@ -46,7 +47,7 @@ type Package struct { // Import loads the given package relative to srcDir (for the vendor directory). // It only loads the current package without recursion. -func (p *Program) Import(path, srcDir string) (*Package, error) { +func (p *Program) Import(path, srcDir string, pos token.Position) (*Package, error) { if p.Packages == nil { p.Packages = make(map[string]*Package) } @@ -59,7 +60,10 @@ func (p *Program) Import(path, srcDir string) (*Package, error) { } buildPkg, err := ctx.Import(path, srcDir, build.ImportComment) if err != nil { - return nil, err + return nil, scanner.Error{ + Pos: pos, + Msg: err.Error(), // TODO: define a new error type that will wrap the inner error + } } if existingPkg, ok := p.Packages[buildPkg.ImportPath]; ok { // Already imported, or at least started the import. @@ -467,7 +471,14 @@ func (p *Package) importRecursively(includeTests bool) error { if _, ok := p.Imports[to]; ok { continue } - importedPkg, err := p.Program.Import(to, p.Package.Dir) + // Find error location. + var pos token.Position + if len(p.Package.ImportPos[to]) > 0 { + pos = p.Package.ImportPos[to][0] + } else { + pos = token.Position{Filename: p.Package.ImportPath} + } + importedPkg, err := p.Program.Import(to, p.Package.Dir, pos) if err != nil { if err, ok := err.(*ImportCycleError); ok { err.Packages = append([]string{p.ImportPath}, err.Packages...) |