Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: printing all the errors from goparser #2011

Merged
merged 7 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 39 additions & 33 deletions gnovm/cmd/gno/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"errors"
"flag"
"fmt"
"go/scanner"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -68,10 +70,6 @@
}

hasError := false
addIssue := func(issue lintIssue) {
hasError = true
fmt.Fprint(io.Err(), issue.String()+"\n")
}

for _, pkgPath := range pkgPaths {
if verbose {
Expand All @@ -81,16 +79,18 @@
// Check if 'gno.mod' exists
gnoModPath := filepath.Join(pkgPath, "gno.mod")
if !osm.FileExists(gnoModPath) {
addIssue(lintIssue{
hasError = true
issue := lintIssue{
Code: lintNoGnoMod,
Confidence: 1,
Location: pkgPath,
Msg: "missing 'gno.mod' file",
})
}
fmt.Fprint(io.Err(), issue.String()+"\n")
}

// Handle runtime errors
catchRuntimeError(pkgPath, addIssue, func() {
hasError = catchRuntimeError(pkgPath, io.Err(), func() {
stdout, stdin, stderr := io.Out(), io.In(), io.Err()
testStore := tests.TestStore(
rootDir, "",
Expand Down Expand Up @@ -130,7 +130,7 @@
}

tm.RunFiles(testfiles.Files...)
})
}) || hasError

// TODO: Add more checkers
}
Expand Down Expand Up @@ -164,47 +164,33 @@
// XXX: Ideally, error handling should encapsulate location details within a dedicated error type.
var reParseRecover = regexp.MustCompile(`^([^:]+):(\d+)(?::\d+)?:? *(.*)$`)

func catchRuntimeError(pkgPath string, addIssue func(issue lintIssue), action func()) {
func catchRuntimeError(pkgPath string, stderr io.WriteCloser, action func()) (hasError bool) {
defer func() {
// Errors catched here mostly come from: gnovm/pkg/gnolang/preprocess.go
r := recover()
if r == nil {
return
}

var err error
hasError = true
switch verr := r.(type) {
case *gno.PreprocessError:
err = verr.Unwrap()
err := verr.Unwrap()
fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n")
case scanner.ErrorList:
for _, err := range verr {
fmt.Fprint(stderr, issueFromError(pkgPath, err).String()+"\n")
}
case error:
err = verr
fmt.Fprint(stderr, issueFromError(pkgPath, verr).String()+"\n")

Check warning on line 184 in gnovm/cmd/gno/lint.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/lint.go#L184

Added line #L184 was not covered by tests
case string:
err = errors.New(verr)
fmt.Fprint(stderr, issueFromError(pkgPath, errors.New(verr)).String()+"\n")

Check warning on line 186 in gnovm/cmd/gno/lint.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/lint.go#L186

Added line #L186 was not covered by tests
default:
panic(r)
}

var issue lintIssue
issue.Confidence = 1
issue.Code = lintGnoError

parsedError := strings.TrimSpace(err.Error())
parsedError = strings.TrimPrefix(parsedError, pkgPath+"/")

matches := reParseRecover.FindStringSubmatch(parsedError)
if len(matches) == 4 {
sourcepath := guessSourcePath(pkgPath, matches[1])
issue.Location = fmt.Sprintf("%s:%s", sourcepath, matches[2])
issue.Msg = strings.TrimSpace(matches[3])
} else {
issue.Location = fmt.Sprintf("%s:0", filepath.Clean(pkgPath))
issue.Msg = err.Error()
}

addIssue(issue)
}()

action()
return
}

type lintCode int
Expand All @@ -229,3 +215,23 @@
// TODO: consider crafting a doc URL based on Code.
return fmt.Sprintf("%s: %s (code=%d).", i.Location, i.Msg, i.Code)
}

func issueFromError(pkgPath string, err error) lintIssue {
var issue lintIssue
issue.Confidence = 1
issue.Code = lintGnoError

parsedError := strings.TrimSpace(err.Error())
parsedError = strings.TrimPrefix(parsedError, pkgPath+"/")

matches := reParseRecover.FindStringSubmatch(parsedError)
if len(matches) == 4 {
sourcepath := guessSourcePath(pkgPath, matches[1])
issue.Location = fmt.Sprintf("%s:%s", sourcepath, matches[2])
issue.Msg = strings.TrimSpace(matches[3])
} else {
issue.Location = fmt.Sprintf("%s:0", filepath.Clean(pkgPath))
issue.Msg = err.Error()

Check warning on line 234 in gnovm/cmd/gno/lint.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/lint.go#L233-L234

Added lines #L233 - L234 were not covered by tests
}
return issue
}
3 changes: 3 additions & 0 deletions gnovm/cmd/gno/lint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func TestLintApp(t *testing.T) {
}, {
args: []string{"lint", "--set-exit-status=0", "../../tests/integ/package_not_declared/main.gno"},
stderrShouldContain: "main.gno:4: name fmt not declared (code=2).",
}, {
args: []string{"lint", "--set-exit-status=0", "../../tests/integ/several-lint-errors/main.gno"},
stderrShouldContain: "../../tests/integ/several-lint-errors/main.gno:5: expected ';', found example (code=2).\n../../tests/integ/several-lint-errors/main.gno:6",
}, {
args: []string{"lint", "--set-exit-status=0", "../../tests/integ/run_main/"},
stderrShouldContain: "./../../tests/integ/run_main: missing 'gno.mod' file (code=1).",
Expand Down
17 changes: 13 additions & 4 deletions gnovm/cmd/gno/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -102,7 +103,7 @@
}

// read files
files, err := parseFiles(args)
files, err := parseFiles(args, stderr)
if err != nil {
return err
}
Expand Down Expand Up @@ -135,15 +136,16 @@
return nil
}

func parseFiles(fnames []string) ([]*gno.FileNode, error) {
func parseFiles(fnames []string, stderr io.WriteCloser) ([]*gno.FileNode, error) {
files := make([]*gno.FileNode, 0, len(fnames))
var hasError bool
for _, fname := range fnames {
if s, err := os.Stat(fname); err == nil && s.IsDir() {
subFns, err := listNonTestFiles(fname)
if err != nil {
return nil, err
}
subFiles, err := parseFiles(subFns)
subFiles, err := parseFiles(subFns, stderr)
if err != nil {
return nil, err
}
Expand All @@ -154,7 +156,14 @@
// in either case not a file we can parse.
return nil, err
}
files = append(files, gno.MustReadFile(fname))

hasError = catchRuntimeError(fname, stderr, func() {
files = append(files, gno.MustReadFile(fname))
})
}

if hasError {
os.Exit(1)

Check warning on line 166 in gnovm/cmd/gno/run.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/run.go#L166

Added line #L166 was not covered by tests
}
return files, nil
}
Expand Down
12 changes: 10 additions & 2 deletions gnovm/cmd/gno/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,15 @@
memPkg := gno.ReadMemPackage(pkgPath, gnoPkgPath)

// tfiles, ifiles := gno.ParseMemPackageTests(memPkg)
tfiles, ifiles := parseMemPackageTests(memPkg)
var tfiles, ifiles *gno.FileSet

Check warning on line 328 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L328

Added line #L328 was not covered by tests

hasError := catchRuntimeError(gnoPkgPath, stderr, func() {
tfiles, ifiles = parseMemPackageTests(memPkg)
})

Check warning on line 332 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L330-L332

Added lines #L330 - L332 were not covered by tests

if hasError {
os.Exit(1)

Check warning on line 335 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L334-L335

Added lines #L334 - L335 were not covered by tests
}
testPkgName := getPkgNameFromFileset(ifiles)

// run test files in pkg
Expand Down Expand Up @@ -639,7 +647,7 @@
}
n, err := gno.ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(errors.Wrap(err, "parsing file "+mfile.Name))
panic(err)

Check warning on line 650 in gnovm/cmd/gno/test.go

View check run for this annotation

Codecov / codecov/patch

gnovm/cmd/gno/test.go#L650

Added line #L650 was not covered by tests
}
if n == nil {
panic("should not happen")
Expand Down
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,7 +1179,7 @@
}
n, err := ParseFile(mfile.Name, mfile.Body)
if err != nil {
panic(errors.Wrap(err, "parsing file "+mfile.Name))
panic(err)

Check warning on line 1182 in gnovm/pkg/gnolang/nodes.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/nodes.go#L1182

Added line #L1182 was not covered by tests
}
if memPkg.Name != string(n.PkgName) {
panic(fmt.Sprintf(
Expand Down
1 change: 1 addition & 0 deletions gnovm/tests/integ/several-lint-errors/gno.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module gno.land/tests/severalerrors
6 changes: 6 additions & 0 deletions gnovm/tests/integ/several-lint-errors/main.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main

func main() {
for {
_ example
}
Loading