Skip to content

Commit cf0cbf6

Browse files
vanaigrhi-ogawa
andauthoredNov 5, 2024··
fix(vite-node): top-level throw in module is not reported properly (#6840)
Co-authored-by: Hiroshi Ogawa <hi.ogawa.zz@gmail.com>
1 parent 487c80a commit cf0cbf6

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed
 

‎packages/vite-node/src/hmr/hmr.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,8 @@ async function fetchUpdate(
160160
}
161161
}
162162

163-
function warnFailedFetch(err: Error, path: string | string[]) {
164-
if (!err.message.match('fetch')) {
163+
function warnFailedFetch(err: unknown, path: string | string[]) {
164+
if (!(err instanceof Error) || !err.message.match('fetch')) {
165165
console.error(err)
166166
}
167167

‎test/vite-node/src/hmr-throw.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
if (import.meta.hot) {
2+
import.meta.hot.accept(() => {})
3+
if (import.meta.hot.data.value == null) {
4+
import.meta.hot.data.value = 0
5+
}
6+
else {
7+
// eslint-disable-next-line no-throw-literal
8+
throw 'some error'
9+
}
10+
}
11+
console.error('ready')

‎test/vite-node/test/hmr.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,16 @@ test('hmr.accept works correctly', async () => {
1515
await viteNode.waitForStderr('Accept')
1616
await viteNode.waitForStdout(`[vite-node] hot updated: ${scriptFile}`)
1717
})
18+
19+
test('can handle top-level throw in self-accepting module', async () => {
20+
const scriptFile = resolve(__dirname, '../src/hmr-throw.js')
21+
22+
const { viteNode } = await runViteNodeCli('--watch', scriptFile)
23+
24+
await viteNode.waitForStderr('ready')
25+
26+
editFile(scriptFile, content => `${content}\nconsole.error("done")`)
27+
28+
await viteNode.waitForStderr('some error')
29+
await viteNode.waitForStderr(`[hmr] Failed to reload ${scriptFile}. This could be due to syntax errors or importing non-existent modules. (see errors above)`)
30+
})

0 commit comments

Comments
 (0)
Please sign in to comment.