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

fix(vite-node): top-level throw in module is not reported properly #6840

Merged
merged 4 commits into from
Nov 5, 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
4 changes: 2 additions & 2 deletions packages/vite-node/src/hmr/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ async function fetchUpdate(
}
}

function warnFailedFetch(err: Error, path: string | string[]) {
if (!err.message.match('fetch')) {
function warnFailedFetch(err: unknown, path: string | string[]) {
if (!(err instanceof Error) || !err.message.match('fetch')) {
console.error(err)
}

Expand Down
11 changes: 11 additions & 0 deletions test/vite-node/src/hmr-throw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
if (import.meta.hot) {
import.meta.hot.accept(() => {})
if (import.meta.hot.data.value == null) {
import.meta.hot.data.value = 0
}
else {
// eslint-disable-next-line no-throw-literal
throw 'some error'
}
}
console.error('ready')
13 changes: 13 additions & 0 deletions test/vite-node/test/hmr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ test('hmr.accept works correctly', async () => {
await viteNode.waitForStderr('Accept')
await viteNode.waitForStdout(`[vite-node] hot updated: ${scriptFile}`)
})

test('can handle top-level throw in self-accepting module', async () => {
const scriptFile = resolve(__dirname, '../src/hmr-throw.js')

const { viteNode } = await runViteNodeCli('--watch', scriptFile)

await viteNode.waitForStderr('ready')

editFile(scriptFile, content => `${content}\nconsole.error("done")`)

await viteNode.waitForStderr('some error')
await viteNode.waitForStderr(`[hmr] Failed to reload ${scriptFile}. This could be due to syntax errors or importing non-existent modules. (see errors above)`)
})