Skip to content

Commit

Permalink
fix(vite-node): top-level throw in module is not reported properly (#…
Browse files Browse the repository at this point in the history
…6840)

Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
  • Loading branch information
vanaigr and hi-ogawa authored Nov 5, 2024
1 parent 487c80a commit cf0cbf6
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
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)`)
})

0 comments on commit cf0cbf6

Please sign in to comment.