Skip to content

Commit

Permalink
capture and report parser panics
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Sep 2, 2021
1 parent 9f507f4 commit 0c2c0a0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
16 changes: 16 additions & 0 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"math/rand"
"net/http"
"runtime/debug"
"sort"
"strings"
"sync"
Expand Down Expand Up @@ -171,6 +172,21 @@ func parseFile(args parseArgs) {
},
}

defer func() {
r := recover()
if r != nil {
stack := strings.TrimSpace(string(debug.Stack()))
tracker := logger.MakeLineColumnTracker(&source)
data := logger.RangeData(&tracker, logger.Range{}, fmt.Sprintf("panic: %v", r))
data.Location.LineText = fmt.Sprintf("%s\n%s", data.Location.LineText, stack)
args.log.AddMsg(logger.Msg{
Kind: logger.Error,
Data: data,
})
args.results <- result
}
}()

switch loader {
case config.LoaderJS:
ast, ok := args.caches.JSCache.Parse(args.log, source, js_parser.OptionsFromConfig(&args.options))
Expand Down
11 changes: 10 additions & 1 deletion scripts/test262.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async function main() {
let reparseCount = 0;
let reprintCount = 0;
let minifyCount = 0;
let panicCount = 0;

async function esbuildFile(input, options) {
try {
Expand Down Expand Up @@ -155,7 +156,7 @@ async function main() {
return
}

const result = await esbuildFile(content, { minify: false });
const result = await esbuildFile(content, { minify: false, sourcefile: file });

if (result.success !== shouldParse) {
if (!result.success) shouldHavePassed++;
Expand Down Expand Up @@ -188,6 +189,13 @@ async function main() {
}
}
}

else if (result.error.toString().includes('panic')) {
console.log(`\n!!! PANIC: ${file} !!!`);
console.log(`${result.error}\n${result.error.errors[0].location.lineText}`);
panicCount++;
}

runCount++;
}

Expand Down Expand Up @@ -220,6 +228,7 @@ async function main() {
console.log(`reparse failures: ${reparseCount}`);
console.log(`reprint failures: ${reprintCount}`);
console.log(`minify failures: ${minifyCount}`);
console.log(`panics: ${panicCount}`);
}

main().catch(e => setTimeout(() => {
Expand Down

0 comments on commit 0c2c0a0

Please sign in to comment.