|
| 1 | +// Package diagnostics formats compiler errors and prints them in a consistent |
| 2 | +// way. |
| 3 | +package diagnostics |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "fmt" |
| 8 | + "go/scanner" |
| 9 | + "go/token" |
| 10 | + "go/types" |
| 11 | + "io" |
| 12 | + "path/filepath" |
| 13 | + "strings" |
| 14 | + |
| 15 | + "github.com/tinygo-org/tinygo/builder" |
| 16 | + "github.com/tinygo-org/tinygo/goenv" |
| 17 | + "github.com/tinygo-org/tinygo/interp" |
| 18 | + "github.com/tinygo-org/tinygo/loader" |
| 19 | +) |
| 20 | + |
| 21 | +// A single diagnostic. |
| 22 | +type Diagnostic struct { |
| 23 | + Pos token.Position |
| 24 | + Msg string |
| 25 | +} |
| 26 | + |
| 27 | +// One or multiple errors of a particular package. |
| 28 | +// It can also represent whole-program errors (like linker errors) that can't |
| 29 | +// easily be connected to a single package. |
| 30 | +type PackageDiagnostic struct { |
| 31 | + ImportPath string // the same ImportPath as in `go list -json` |
| 32 | + Diagnostics []Diagnostic |
| 33 | +} |
| 34 | + |
| 35 | +// Diagnostics of a whole program. This can include errors belonging to multiple |
| 36 | +// packages, or just a single package. |
| 37 | +type ProgramDiagnostic []PackageDiagnostic |
| 38 | + |
| 39 | +// CreateDiagnostics reads the underlying errors in the error object and creates |
| 40 | +// a set of diagnostics that's sorted and can be readily printed. |
| 41 | +func CreateDiagnostics(err error) ProgramDiagnostic { |
| 42 | + if err == nil { |
| 43 | + return nil |
| 44 | + } |
| 45 | + switch err := err.(type) { |
| 46 | + case *builder.MultiError: |
| 47 | + var diags ProgramDiagnostic |
| 48 | + for _, err := range err.Errs { |
| 49 | + diags = append(diags, createPackageDiagnostic(err)) |
| 50 | + } |
| 51 | + return diags |
| 52 | + default: |
| 53 | + return ProgramDiagnostic{ |
| 54 | + createPackageDiagnostic(err), |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +// Create diagnostics for a single package (though, in practice, it may also be |
| 60 | +// used for whole-program diagnostics in some cases). |
| 61 | +func createPackageDiagnostic(err error) PackageDiagnostic { |
| 62 | + var pkgDiag PackageDiagnostic |
| 63 | + switch err := err.(type) { |
| 64 | + case loader.Errors: |
| 65 | + if err.Pkg != nil { |
| 66 | + pkgDiag.ImportPath = err.Pkg.ImportPath |
| 67 | + } |
| 68 | + for _, err := range err.Errs { |
| 69 | + diags := createDiagnostics(err) |
| 70 | + pkgDiag.Diagnostics = append(pkgDiag.Diagnostics, diags...) |
| 71 | + } |
| 72 | + case *interp.Error: |
| 73 | + pkgDiag.ImportPath = err.ImportPath |
| 74 | + w := &bytes.Buffer{} |
| 75 | + fmt.Fprintln(w, err.Error()) |
| 76 | + if len(err.Inst) != 0 { |
| 77 | + fmt.Fprintln(w, err.Inst) |
| 78 | + } |
| 79 | + if len(err.Traceback) > 0 { |
| 80 | + fmt.Fprintln(w, "\ntraceback:") |
| 81 | + for _, line := range err.Traceback { |
| 82 | + fmt.Fprintln(w, line.Pos.String()+":") |
| 83 | + fmt.Fprintln(w, line.Inst) |
| 84 | + } |
| 85 | + } |
| 86 | + pkgDiag.Diagnostics = append(pkgDiag.Diagnostics, Diagnostic{ |
| 87 | + Msg: w.String(), |
| 88 | + }) |
| 89 | + default: |
| 90 | + pkgDiag.Diagnostics = createDiagnostics(err) |
| 91 | + } |
| 92 | + // TODO: sort |
| 93 | + return pkgDiag |
| 94 | +} |
| 95 | + |
| 96 | +// Extract diagnostics from the given error message and return them as a slice |
| 97 | +// of errors (which in many cases will just be a single diagnostic). |
| 98 | +func createDiagnostics(err error) []Diagnostic { |
| 99 | + switch err := err.(type) { |
| 100 | + case types.Error: |
| 101 | + return []Diagnostic{ |
| 102 | + { |
| 103 | + Pos: err.Fset.Position(err.Pos), |
| 104 | + Msg: err.Msg, |
| 105 | + }, |
| 106 | + } |
| 107 | + case scanner.Error: |
| 108 | + return []Diagnostic{ |
| 109 | + { |
| 110 | + Pos: err.Pos, |
| 111 | + Msg: err.Msg, |
| 112 | + }, |
| 113 | + } |
| 114 | + case scanner.ErrorList: |
| 115 | + var diags []Diagnostic |
| 116 | + for _, err := range err { |
| 117 | + diags = append(diags, createDiagnostics(*err)...) |
| 118 | + } |
| 119 | + return diags |
| 120 | + case loader.Error: |
| 121 | + if err.Err.Pos.Filename != "" { |
| 122 | + // Probably a syntax error in a dependency. |
| 123 | + return createDiagnostics(err.Err) |
| 124 | + } else { |
| 125 | + // Probably an "import cycle not allowed" error. |
| 126 | + buf := &bytes.Buffer{} |
| 127 | + fmt.Fprintln(buf, "package", err.ImportStack[0]) |
| 128 | + for i := 1; i < len(err.ImportStack); i++ { |
| 129 | + pkgPath := err.ImportStack[i] |
| 130 | + if i == len(err.ImportStack)-1 { |
| 131 | + // last package |
| 132 | + fmt.Fprintln(buf, "\timports", pkgPath+": "+err.Err.Error()) |
| 133 | + } else { |
| 134 | + // not the last pacakge |
| 135 | + fmt.Fprintln(buf, "\timports", pkgPath) |
| 136 | + } |
| 137 | + } |
| 138 | + return []Diagnostic{ |
| 139 | + {Msg: buf.String()}, |
| 140 | + } |
| 141 | + } |
| 142 | + default: |
| 143 | + return []Diagnostic{ |
| 144 | + {Msg: err.Error()}, |
| 145 | + } |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +// Write program diagnostics to the given writer with 'wd' as the relative |
| 150 | +// working directory. |
| 151 | +func (progDiag ProgramDiagnostic) WriteTo(w io.Writer, wd string) { |
| 152 | + for _, pkgDiag := range progDiag { |
| 153 | + pkgDiag.WriteTo(w, wd) |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// Write package diagnostics to the given writer with 'wd' as the relative |
| 158 | +// working directory. |
| 159 | +func (pkgDiag PackageDiagnostic) WriteTo(w io.Writer, wd string) { |
| 160 | + if pkgDiag.ImportPath != "" { |
| 161 | + fmt.Fprintln(w, "#", pkgDiag.ImportPath) |
| 162 | + } |
| 163 | + for _, diag := range pkgDiag.Diagnostics { |
| 164 | + diag.WriteTo(w, wd) |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +// Write this diagnostic to the given writer with 'wd' as the relative working |
| 169 | +// directory. |
| 170 | +func (diag Diagnostic) WriteTo(w io.Writer, wd string) { |
| 171 | + if diag.Pos == (token.Position{}) { |
| 172 | + fmt.Fprintln(w, diag.Msg) |
| 173 | + return |
| 174 | + } |
| 175 | + pos := diag.Pos // make a copy |
| 176 | + if !strings.HasPrefix(pos.Filename, filepath.Join(goenv.Get("GOROOT"), "src")) && !strings.HasPrefix(pos.Filename, filepath.Join(goenv.Get("TINYGOROOT"), "src")) { |
| 177 | + // This file is not from the standard library (either the GOROOT or the |
| 178 | + // TINYGOROOT). Make the path relative, for easier reading. Ignore any |
| 179 | + // errors in the process (falling back to the absolute path). |
| 180 | + pos.Filename = tryToMakePathRelative(pos.Filename, wd) |
| 181 | + } |
| 182 | + fmt.Fprintf(w, "%s: %s\n", pos, diag.Msg) |
| 183 | +} |
| 184 | + |
| 185 | +// try to make the path relative to the current working directory. If any error |
| 186 | +// occurs, this error is ignored and the absolute path is returned instead. |
| 187 | +func tryToMakePathRelative(dir, wd string) string { |
| 188 | + if wd == "" { |
| 189 | + return dir // working directory not found |
| 190 | + } |
| 191 | + relpath, err := filepath.Rel(wd, dir) |
| 192 | + if err != nil { |
| 193 | + return dir |
| 194 | + } |
| 195 | + return relpath |
| 196 | +} |
0 commit comments