loader: better error message on import cycles

This commit is contained in:
Konstantin Yegupov
2019-01-24 17:25:16 +10:00
committed by Ayke van Laethem
parent 4f4d7976c6
commit e6d90d89fa
2 changed files with 24 additions and 7 deletions
+17 -5
View File
@@ -1,5 +1,10 @@
package loader
import (
"go/token"
"strings"
)
// Errors contains a list of parser errors or a list of typechecker errors for
// the given package.
type Errors struct {
@@ -15,13 +20,20 @@ func (e Errors) Error() string {
// packages is a list from the root package to the leaf package that imports one
// of the packages in the list.
type ImportCycleError struct {
Packages []string
Packages []string
ImportPositions []token.Position
}
func (e *ImportCycleError) Error() string {
msg := "import cycle: " + e.Packages[0]
for _, path := range e.Packages[1:] {
msg += " → " + path
var msg strings.Builder
msg.WriteString("import cycle:\n\t")
msg.WriteString(strings.Join(e.Packages, "\n\t"))
msg.WriteString("\n at ")
for i, pos := range e.ImportPositions {
if i > 0 {
msg.WriteString(", ")
}
msg.WriteString(pos.String())
}
return msg
return msg.String()
}