// A PackageError describes an error loading information about a package.
type PackageError struct {
ImportStack []string // shortest path from package named on command line to this one
+ Pos string // position of error
Err string // the error itself
}
func (p *PackageError) Error() string {
+ if p.Pos != "" {
+ return strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Pos + ": " + p.Err
+ }
return strings.Join(p.ImportStack, "\n\timports ") + ": " + p.Err
}
}
deps[path] = true
p1 := loadPackage(path, stk)
+ if p1.Error != nil {
+ if info.ImportPos != nil && len(info.ImportPos[path]) > 0 {
+ pos := info.ImportPos[path][0]
+ p1.Error.Pos = pos.String()
+ }
+ }
imports = append(imports, p1)
for _, dep := range p1.Deps {
deps[dep] = true
t.Errorf("ScanDir(%#q): %v", tt.dir, err)
continue
}
+ // Don't bother testing import positions.
+ tt.info.ImportPos, tt.info.TestImportPos = info.ImportPos, info.TestImportPos
if !reflect.DeepEqual(info, tt.info) {
t.Errorf("ScanDir(%#q) = %#v, want %#v\n", tt.dir, info, tt.info)
continue
}
type DirInfo struct {
- Package string // Name of package in dir
- PackageComment *ast.CommentGroup // Package comments from GoFiles
- ImportPath string // Import path of package in dir
- Imports []string // All packages imported by GoFiles
+ Package string // Name of package in dir
+ PackageComment *ast.CommentGroup // Package comments from GoFiles
+ ImportPath string // Import path of package in dir
+ Imports []string // All packages imported by GoFiles
+ ImportPos map[string][]token.Position // Source code location of imports
// Source files
GoFiles []string // .go files in dir (excluding CgoFiles, TestGoFiles, XTestGoFiles)
CgoLDFLAGS []string // Cgo LDFLAGS directives
// Test information
- TestGoFiles []string // _test.go files in package
- XTestGoFiles []string // _test.go files outside package
- TestImports []string // All packages imported by (X)TestGoFiles
+ TestGoFiles []string // _test.go files in package
+ XTestGoFiles []string // _test.go files outside package
+ TestImports []string // All packages imported by (X)TestGoFiles
+ TestImportPos map[string][]token.Position
}
func (d *DirInfo) IsCommand() bool {
var Sfiles []string // files with ".S" (capital S)
var di DirInfo
- imported := make(map[string]bool)
- testImported := make(map[string]bool)
+ imported := make(map[string][]token.Position)
+ testImported := make(map[string][]token.Position)
fset := token.NewFileSet()
for _, d := range dirs {
if d.IsDir() {
log.Panicf("%s: parser returned invalid quoted string: <%s>", filename, quoted)
}
if isTest {
- testImported[path] = true
+ testImported[path] = append(testImported[path], fset.Position(spec.Pos()))
} else {
- imported[path] = true
+ imported[path] = append(imported[path], fset.Position(spec.Pos()))
}
if path == "C" {
if isTest {
return nil, fmt.Errorf("%s: no Go source files", dir)
}
di.Imports = make([]string, len(imported))
+ di.ImportPos = imported
i := 0
for p := range imported {
di.Imports[i] = p
i++
}
di.TestImports = make([]string, len(testImported))
+ di.TestImportPos = testImported
i = 0
for p := range testImported {
di.TestImports[i] = p