Skip to content

Commit

Permalink
loader: handle go list errors inside TinyGo
Browse files Browse the repository at this point in the history
Instead of exiting with an error, handle these errors internally.
This will enable a few improvements in the future.
  • Loading branch information
aykevl committed Jul 13, 2024
1 parent b04b690 commit d7773d3
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 9 deletions.
18 changes: 17 additions & 1 deletion errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
"time"
Expand All @@ -16,6 +17,10 @@ import (
func TestErrors(t *testing.T) {
for _, name := range []string{
"cgo",
"loader-importcycle",
"loader-invaliddep",
"loader-invalidpackage",
"loader-nopackage",
"syntax",
"types",
} {
Expand Down Expand Up @@ -57,11 +62,22 @@ func testErrorMessages(t *testing.T, filename string) {
actual := strings.TrimRight(buf.String(), "\n")

// Check whether the error is as expected.
if actual != expected {
if canonicalizeErrors(actual) != canonicalizeErrors(expected) {
t.Errorf("expected error:\n%s\ngot:\n%s", indentText(expected, "> "), indentText(actual, "> "))
}
}

func canonicalizeErrors(text string) string {
// Fix for Windows: replace all backslashes with forward slashes so that
// paths will be the same as on POSIX systems.
// (It may also change some other backslashes, but since this is only for
// comparing text it should be fine).
if runtime.GOOS == "windows" {
text = strings.ReplaceAll(text, "\\", "/")
}
return text
}

// Indent the given text with a given indentation string.
func indentText(text, indent string) string {
return indent + strings.ReplaceAll(text, "\n", "\n"+indent)
Expand Down
21 changes: 18 additions & 3 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func Load(config *compileopts.Config, inputPkg string, typeChecker types.Config)
}

// List the dependencies of this package, in raw JSON format.
extraArgs := []string{"-json", "-deps"}
extraArgs := []string{"-json", "-deps", "-e"}
if config.TestConfig.CompileTestBinary {
extraArgs = append(extraArgs, "-test")
}
Expand All @@ -149,6 +149,7 @@ func Load(config *compileopts.Config, inputPkg string, typeChecker types.Config)

// Parse the returned json from `go list`.
decoder := json.NewDecoder(buf)
var pkgErrors []error
for {
pkg := &Package{
program: p,
Expand Down Expand Up @@ -188,17 +189,24 @@ func Load(config *compileopts.Config, inputPkg string, typeChecker types.Config)
pos.Filename = strings.Join(fields[:len(fields)-1], ":")
pos.Line, _ = strconv.Atoi(fields[len(fields)-1])
}
if abs, err := filepath.Abs(pos.Filename); err == nil {
// Make the path absolute, so that error messages will be
// prettier (it will be turned back into a relative path
// when printing the error).
pos.Filename = abs
}
pos.Filename = p.getOriginalPath(pos.Filename)
}
err := scanner.Error{
Pos: pos,
Msg: pkg.Error.Err,
}
if len(pkg.Error.ImportStack) != 0 {
return nil, Error{
pkgErrors = append(pkgErrors, Error{
ImportStack: pkg.Error.ImportStack,
Err: err,
}
})
continue
}
return nil, err
}
Expand Down Expand Up @@ -241,6 +249,13 @@ func Load(config *compileopts.Config, inputPkg string, typeChecker types.Config)
p.Packages[pkg.ImportPath] = pkg
}

if len(pkgErrors) != 0 {
// TODO: use errors.Join in Go 1.20.
return nil, Errors{
Errs: pkgErrors,
}
}

if config.TestConfig.CompileTestBinary && !strings.HasSuffix(p.sorted[len(p.sorted)-1].ImportPath, ".test") {
// Trying to compile a test binary but there are no test files in this
// package.
Expand Down
26 changes: 21 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1340,15 +1340,31 @@ func printCompilerError(err error, logln func(...interface{}), wd string) {
}
}
case loader.Errors:
logln("#", err.Pkg.ImportPath)
// Parser errors, typechecking errors, or `go list` errors.
// err.Pkg is nil for `go list` errors.
if err.Pkg != nil {
logln("#", err.Pkg.ImportPath)
}
for _, err := range err.Errs {
printCompilerError(err, logln, wd)
}
case loader.Error:
logln(err.Err.Error())
logln("package", err.ImportStack[0])
for _, pkgPath := range err.ImportStack[1:] {
logln("\timports", pkgPath)
if err.Err.Pos.Filename != "" {
// Probably a syntax error in a dependency.
printCompilerError(err.Err, logln, wd)
} else {
// Probably an "import cycle not allowed" error.
logln("package", err.ImportStack[0])
for i := 1; i < len(err.ImportStack); i++ {
pkgPath := err.ImportStack[i]
if i == len(err.ImportStack)-1 {
// last package
logln("\timports", pkgPath+": "+err.Err.Error())
} else {
// not the last pacakge
logln("\timports", pkgPath)
}
}
}
case *builder.MultiError:
for _, err := range err.Errs {
Expand Down
3 changes: 3 additions & 0 deletions testdata/errors/importcycle/cycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package importcycle

import _ "github.com/tinygo-org/tinygo/testdata/errors/importcycle"
1 change: 1 addition & 0 deletions testdata/errors/invaliddep/invaliddep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ppackage // syntax error
10 changes: 10 additions & 0 deletions testdata/errors/loader-importcycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import _ "github.com/tinygo-org/tinygo/testdata/errors/importcycle"

func main() {
}

// ERROR: package command-line-arguments
// ERROR: imports github.com/tinygo-org/tinygo/testdata/errors/importcycle
// ERROR: imports github.com/tinygo-org/tinygo/testdata/errors/importcycle: import cycle not allowed
8 changes: 8 additions & 0 deletions testdata/errors/loader-invaliddep.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import _ "github.com/tinygo-org/tinygo/testdata/errors/invaliddep"

func main() {
}

// ERROR: invaliddep/invaliddep.go:1:1: expected 'package', found ppackage
3 changes: 3 additions & 0 deletions testdata/errors/loader-invalidpackage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ppackage // syntax error

// ERROR: loader-invalidpackage.go:1:1: expected 'package', found ppackage
14 changes: 14 additions & 0 deletions testdata/errors/loader-nopackage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
_ "github.com/tinygo-org/tinygo/testdata/errors/non-existing-package"
_ "github.com/tinygo-org/tinygo/testdata/errors/non-existing-package-2"
)

func main() {
}

// ERROR: loader-nopackage.go:4:2: no required module provides package github.com/tinygo-org/tinygo/testdata/errors/non-existing-package; to add it:
// ERROR: go get github.com/tinygo-org/tinygo/testdata/errors/non-existing-package
// ERROR: loader-nopackage.go:5:2: no required module provides package github.com/tinygo-org/tinygo/testdata/errors/non-existing-package-2; to add it:
// ERROR: go get github.com/tinygo-org/tinygo/testdata/errors/non-existing-package-2

0 comments on commit d7773d3

Please sign in to comment.