-
-
Notifications
You must be signed in to change notification settings - Fork 116
feat: improve compile error reporting #220
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0a97d47
feat: improve compile error reporting
dominikg f7aadbc
feat: improve error handling in esbuild dependency optimization
dominikg d2baa00
fix: use lineText instead of suggestion/detail so that esbuild produc…
dominikg 01db878
fix: reformat code frame so that it is displayed in vite error overlay
dominikg 7e6a66d
chore: add changeset
dominikg 1f2cb21
Apply suggestions from code review
dominikg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@sveltejs/vite-plugin-svelte': minor | ||
--- | ||
|
||
Improved error reporting for svelte compiler errors |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { RollupError } from 'rollup'; | ||
import { Warning } from './options'; | ||
import { buildExtendedLogMessage } from './log'; | ||
import { PartialMessage } from 'esbuild'; | ||
|
||
/** | ||
* convert an error thrown by svelte.compile to a RollupError so that vite displays it in a user friendly way | ||
* @param error a svelte compiler error, which is a mix of Warning and an error | ||
* @returns {RollupError} the converted error | ||
*/ | ||
export function toRollupError(error: Warning & Error): RollupError { | ||
const { filename, frame, start, code, name } = error; | ||
const rollupError: RollupError = { | ||
name, // needed otherwise sveltekit coalesce_to_error turns it into a string | ||
id: filename, | ||
message: buildExtendedLogMessage(error), // include filename:line:column so that it's clickable | ||
frame: formatFrameForVite(frame), | ||
code, | ||
stack: '' | ||
}; | ||
if (start) { | ||
rollupError.loc = { | ||
line: start.line, | ||
column: start.column, | ||
file: filename | ||
}; | ||
} | ||
return rollupError; | ||
} | ||
|
||
/** | ||
* convert an error thrown by svelte.compile to an esbuild PartialMessage | ||
* @param error a svelte compiler error, which is a mix of Warning and an error | ||
* @returns {PartialMessage} the converted error | ||
*/ | ||
export function toESBuildError(error: Warning & Error): PartialMessage { | ||
const { filename, frame, start } = error; | ||
const partialMessage: PartialMessage = { | ||
text: buildExtendedLogMessage(error) | ||
}; | ||
if (start) { | ||
partialMessage.location = { | ||
line: start.line, | ||
column: start.column, | ||
file: filename, | ||
lineText: lineFromFrame(start.line, frame) // needed to get a meaningful error message on cli | ||
}; | ||
} | ||
return partialMessage; | ||
} | ||
|
||
/** | ||
* extract line with number from codeframe | ||
*/ | ||
function lineFromFrame(lineNo: number, frame?: string): string { | ||
if (!frame) { | ||
return ''; | ||
} | ||
const lines = frame.split('\n'); | ||
const errorLine = lines.find((line) => line.trimStart().startsWith(`${lineNo}: `)); | ||
return errorLine ? errorLine.substring(errorLine.indexOf(': ') + 3) : ''; | ||
} | ||
|
||
/** | ||
* vite error overlay expects a specific format to show frames | ||
* this reformats svelte frame (colon separated, less whitespace) | ||
* to one that vite displays on overlay ( pipe separated, more whitespace) | ||
* e.g. | ||
* ``` | ||
* 1: foo | ||
* 2: bar; | ||
* ^ | ||
* 3: baz | ||
* ``` | ||
* to | ||
* ``` | ||
* 1 | foo | ||
* 2 | bar; | ||
* ^ | ||
* 3 | baz | ||
* ``` | ||
* @see https://github.com/vitejs/vite/blob/96591bf9989529de839ba89958755eafe4c445ae/packages/vite/src/client/overlay.ts#L116 | ||
*/ | ||
function formatFrameForVite(frame?: string): string { | ||
if (!frame) { | ||
return ''; | ||
} | ||
return frame | ||
.split('\n') | ||
.map((line) => (line.match(/^\s+\^/) ? ' ' + line : ' ' + line.replace(':', ' | '))) | ||
.join('\n'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.