-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Error handling improvements to esbuild plugin #2595
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
Changes from 9 commits
3d2e68e
d3d13e1
199439e
ed891fc
377c6e4
0b85289
bbe4f87
2b32e26
3dffdcf
a89028b
d3c901f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
/** | ||
* @import {CompileOptions} from '@mdx-js/mdx' | ||
* @import { | ||
Location, | ||
Message, | ||
OnLoadArgs, | ||
OnLoadResult, | ||
|
@@ -118,13 +119,13 @@ export function esbuild(options) { | |
} catch (error_) { | ||
const cause = /** @type {VFileMessage | Error} */ (error_) | ||
const message = | ||
'reason' in cause | ||
? cause | ||
: new VFileMessage('Cannot process MDX file with esbuild', { | ||
cause, | ||
ruleId: 'process-error', | ||
source: '@mdx-js/esbuild' | ||
}) | ||
new VFileMessage( | ||
'Cannot process MDX file with esbuild', { | ||
cause, | ||
place: 'reason' in cause ? cause.place : undefined, | ||
ruleId: 'process-error', | ||
source: '@mdx-js/esbuild' | ||
}) | ||
message.fatal = true | ||
messages.push(message) | ||
} | ||
|
@@ -157,44 +158,60 @@ export function esbuild(options) { | |
* ESBuild message. | ||
*/ | ||
function vfileMessageToEsbuild(state, message) { | ||
/** @type {Location} */ | ||
let location = { | ||
column: 0, | ||
file: state.path, | ||
length: 0, | ||
line: 0, | ||
lineText: '', | ||
namespace: 'file', | ||
suggestion: '', | ||
} | ||
|
||
const place = message.place | ||
const start = place ? ('start' in place ? place.start : place) : undefined | ||
const end = place && 'end' in place ? place.end : undefined | ||
let length = 0 | ||
let lineStart = 0 | ||
let line = 0 | ||
let column = 0 | ||
|
||
if (start && start.offset !== undefined) { | ||
line = start.line | ||
column = start.column - 1 | ||
lineStart = start.offset - column | ||
length = 1 | ||
|
||
if (end && end.offset !== undefined) { | ||
length = end.offset - start.offset | ||
if (start) { | ||
location.column = start.column - 1 | ||
location.line = start.line | ||
location.length = 1 | ||
|
||
const end = place && 'end' in place ? place.end : undefined | ||
if (end) { | ||
if (start.offset !== undefined && end.offset !== undefined) { | ||
location.length = end.offset - start.offset; | ||
} else if (end.line === start.line) { | ||
location.length = end.column - start.column; | ||
} | ||
} | ||
} | ||
|
||
eol.lastIndex = lineStart | ||
if (start.offset !== undefined) { | ||
eol.lastIndex = start.offset | ||
const match = eol.exec(state.doc) | ||
const lineStart = start.offset - (start.column - 1) | ||
const lineEnd = match ? match.index : state.doc.length | ||
location.lineText = state.doc.slice(lineStart, lineEnd) | ||
location.length = Math.min(location.length, lineEnd - (start.offset || 0)) | ||
} | ||
|
||
const maxLength = state.doc.length - (start.offset || 0) | ||
location.length = Math.min(location.length, maxLength) | ||
} | ||
|
||
const match = eol.exec(state.doc) | ||
const lineEnd = match ? match.index : state.doc.length | ||
/** @type {Error} */ | ||
var exc = message | ||
var text = message.reason | ||
while (exc.cause instanceof Error) { | ||
egnor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exc = exc.cause | ||
text = `${text}:\n ${exc}` | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are these lines because otherwise the “cause” is lost? That ESBuild doesn’t print them? Is that something that ESbuild should fix? Upstream? Or, if indeed needed here, perhaps you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay yes this is the tricky bit. In esbuild you can report errors by just throwing an exception, or by adding to a list of messages using their own Alternatives
For now I simplified the cause processing to not try to walk the whole cause chain but just include the string representation of the cause if one exists. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good points, I appreciate that you came up with alternatives & discussed trade offs. I could see an The no-wrapping idea could also perhaps work. But this is probably fine to start with! |
||
|
||
return { | ||
detail: message, | ||
id: '', | ||
location: { | ||
column, | ||
file: state.path, | ||
length: Math.min(length, lineEnd), | ||
line, | ||
lineText: state.doc.slice(lineStart, lineEnd), | ||
namespace: 'file', | ||
suggestion: '' | ||
}, | ||
location, | ||
notes: [], | ||
pluginName: state.name, | ||
text: message.reason | ||
text, | ||
egnor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.