mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-12 23:13:40 +00:00
all: add testing for compiler error messages
This is needed for some improvements I'm going to make next. This commit also refactors error handling slightly to make it more easily testable, this should hopefully not result in any actual changes in behavior.
This commit is contained in:
@@ -1293,10 +1293,9 @@ func usage(command string) {
|
||||
|
||||
// try to make the path relative to the current working directory. If any error
|
||||
// occurs, this error is ignored and the absolute path is returned instead.
|
||||
func tryToMakePathRelative(dir string) string {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
return dir
|
||||
func tryToMakePathRelative(dir, wd string) string {
|
||||
if wd == "" {
|
||||
return dir // working directory not found
|
||||
}
|
||||
relpath, err := filepath.Rel(wd, dir)
|
||||
if err != nil {
|
||||
@@ -1307,28 +1306,25 @@ func tryToMakePathRelative(dir string) string {
|
||||
|
||||
// printCompilerError prints compiler errors using the provided logger function
|
||||
// (similar to fmt.Println).
|
||||
//
|
||||
// There is one exception: interp errors may print to stderr unconditionally due
|
||||
// to limitations in the LLVM bindings.
|
||||
func printCompilerError(logln func(...interface{}), err error) {
|
||||
func printCompilerError(err error, logln func(...interface{}), wd string) {
|
||||
switch err := err.(type) {
|
||||
case types.Error:
|
||||
printCompilerError(logln, scanner.Error{
|
||||
printCompilerError(scanner.Error{
|
||||
Pos: err.Fset.Position(err.Pos),
|
||||
Msg: err.Msg,
|
||||
})
|
||||
}, logln, wd)
|
||||
case scanner.Error:
|
||||
if !strings.HasPrefix(err.Pos.Filename, filepath.Join(goenv.Get("GOROOT"), "src")) && !strings.HasPrefix(err.Pos.Filename, filepath.Join(goenv.Get("TINYGOROOT"), "src")) {
|
||||
// This file is not from the standard library (either the GOROOT or
|
||||
// the TINYGOROOT). Make the path relative, for easier reading.
|
||||
// Ignore any errors in the process (falling back to the absolute
|
||||
// path).
|
||||
err.Pos.Filename = tryToMakePathRelative(err.Pos.Filename)
|
||||
err.Pos.Filename = tryToMakePathRelative(err.Pos.Filename, wd)
|
||||
}
|
||||
logln(err)
|
||||
case scanner.ErrorList:
|
||||
for _, scannerErr := range err {
|
||||
printCompilerError(logln, *scannerErr)
|
||||
printCompilerError(*scannerErr, logln, wd)
|
||||
}
|
||||
case *interp.Error:
|
||||
logln("#", err.ImportPath)
|
||||
@@ -1346,7 +1342,7 @@ func printCompilerError(logln func(...interface{}), err error) {
|
||||
case loader.Errors:
|
||||
logln("#", err.Pkg.ImportPath)
|
||||
for _, err := range err.Errs {
|
||||
printCompilerError(logln, err)
|
||||
printCompilerError(err, logln, wd)
|
||||
}
|
||||
case loader.Error:
|
||||
logln(err.Err.Error())
|
||||
@@ -1356,7 +1352,7 @@ func printCompilerError(logln func(...interface{}), err error) {
|
||||
}
|
||||
case *builder.MultiError:
|
||||
for _, err := range err.Errs {
|
||||
printCompilerError(logln, err)
|
||||
printCompilerError(err, logln, wd)
|
||||
}
|
||||
default:
|
||||
logln("error:", err)
|
||||
@@ -1365,9 +1361,13 @@ func printCompilerError(logln func(...interface{}), err error) {
|
||||
|
||||
func handleCompilerError(err error) {
|
||||
if err != nil {
|
||||
printCompilerError(func(args ...interface{}) {
|
||||
wd, getwdErr := os.Getwd()
|
||||
if getwdErr != nil {
|
||||
wd = ""
|
||||
}
|
||||
printCompilerError(err, func(args ...interface{}) {
|
||||
fmt.Fprintln(os.Stderr, args...)
|
||||
}, err)
|
||||
}, wd)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -1769,9 +1769,13 @@ func main() {
|
||||
stderr := (*testStderr)(buf)
|
||||
passed, err := Test(pkgName, stdout, stderr, options, outpath)
|
||||
if err != nil {
|
||||
printCompilerError(func(args ...interface{}) {
|
||||
wd, getwdErr := os.Getwd()
|
||||
if getwdErr != nil {
|
||||
wd = ""
|
||||
}
|
||||
printCompilerError(err, func(args ...interface{}) {
|
||||
fmt.Fprintln(stderr, args...)
|
||||
}, err)
|
||||
}, wd)
|
||||
}
|
||||
if !passed {
|
||||
select {
|
||||
|
||||
Reference in New Issue
Block a user