aboutsummaryrefslogtreecommitdiffhomepage
path: root/main.go
diff options
context:
space:
mode:
authorAyke van Laethem <[email protected]>2024-07-09 17:16:32 +0200
committerAyke <[email protected]>2024-07-13 13:26:26 +0200
commit8a357af3d81b86ada93a91fbd3b57d9c5fb34fc6 (patch)
tree08578e4fce0419eed350b33de51b0d5eef32d34e /main.go
parent7ac1ca0ae2a727329550eecf71814f08dccf0700 (diff)
downloadtinygo-8a357af3d81b86ada93a91fbd3b57d9c5fb34fc6.tar.gz
tinygo-8a357af3d81b86ada93a91fbd3b57d9c5fb34fc6.zip
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.
Diffstat (limited to 'main.go')
-rw-r--r--main.go40
1 files changed, 22 insertions, 18 deletions
diff --git a/main.go b/main.go
index 4a23fa8cf..e18f12f1f 100644
--- a/main.go
+++ b/main.go
@@ -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 {