Skip to content

Commit 34aa322

Browse files
authoredMar 7, 2025··
fix(vite-node): fix source map of inlined node_modules (#7557)
1 parent 0fb21fa commit 34aa322

15 files changed

+317
-1
lines changed
 

‎eslint.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export default antfu(
2323
'test/network-imports/public/slash@3.0.0.js',
2424
'test/coverage-test/src/transpiled.js',
2525
'test/coverage-test/src/original.ts',
26+
'test/cli/deps/error/*',
2627
'examples/**/mockServiceWorker.js',
2728
'examples/sveltekit/.svelte-kit',
2829
'packages/browser/**/esm-client-injector.js',

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ export class ViteNodeServer {
428428
}
429429

430430
const sourcemap = this.options.sourcemap ?? 'inline'
431-
if (sourcemap === 'inline' && result && !id.includes('node_modules')) {
431+
if (sourcemap === 'inline' && result) {
432432
result = await this.processTransformResult(filepath, result)
433433
}
434434

‎pnpm-lock.yaml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/cli/deps/error/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import 'node:path'
2+
3+
export default function testStack() {
4+
innerTestStack()
5+
}
6+
7+
import 'node:util'
8+
9+
function innerTestStack() {
10+
throw new Error('__TEST_STACK__')
11+
}

‎test/cli/deps/error/package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@vitest/test-dep-error",
3+
"type": "module",
4+
"private": true,
5+
"exports": {
6+
".": "./index.js",
7+
"./*": "./*"
8+
}
9+
}

‎test/cli/deps/error/transpiled-inline.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'node:path'
2+
3+
export type Dummy = {
4+
foo: "foo",
5+
}
6+
7+
/**
8+
* dummy
9+
* dummy
10+
*/
11+
export default function testStack() {
12+
innerTestStack()
13+
}
14+
15+
import 'node:util'
16+
17+
/**
18+
* bar
19+
* bar
20+
*/
21+
function innerTestStack() {
22+
throw new Error('__TEST_STACK_TRANSPILED_INLINE__')
23+
}
24+
25+
// transpiled-inline.js is copied from
26+
// https://esbuild.github.io/try/#dAAwLjI1LjAALS1sb2FkZXI9dHMgLS1zb3VyY2VtYXA9aW5saW5lIC0tc291cmNlZmlsZT10cmFuc3BpbGVkLWlubGluZS50cwBpbXBvcnQgJ25vZGU6cGF0aCcKCmV4cG9ydCB0eXBlIER1bW15ID0gewogIGZvbzogImZvbyIsCn0KCi8qKgogKiBkdW1teQogKiBkdW1teQogKi8KZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gdGVzdFN0YWNrKCkgewogIGlubmVyVGVzdFN0YWNrKCkKfQoKaW1wb3J0ICdub2RlOnV0aWwnCgovKioKICogYmFyCiAqIGJhcgogKi8KZnVuY3Rpb24gaW5uZXJUZXN0U3RhY2soKSB7CiAgdGhyb3cgbmV3IEVycm9yKCdfX1RFU1RfU1RBQ0tfVFJBTlNQSUxFRF9JTkxJTkVfXycpCn0

‎test/cli/deps/error/transpiled.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/cli/deps/error/transpiled.js.map

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎test/cli/deps/error/transpiled.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import 'node:path'
2+
3+
export type Dummy = {
4+
foo: "foo",
5+
}
6+
7+
/**
8+
* dummy
9+
* dummy
10+
*/
11+
export default function testStack() {
12+
innerTestStack()
13+
}
14+
15+
import 'node:util'
16+
17+
/**
18+
* bar
19+
* bar
20+
*/
21+
function innerTestStack() {
22+
throw new Error('__TEST_STACK_TRANSPILED__')
23+
}
24+
25+
// transpiled.js and transpiled.js.map are copied from
26+
// https://esbuild.github.io/try/#dAAwLjI1LjAALS1sb2FkZXI9dHMgLS1zb3VyY2VtYXAgLS1zb3VyY2VmaWxlPXRyYW5zcGlsZWQudHMAaW1wb3J0ICdub2RlOnBhdGgnCgpleHBvcnQgdHlwZSBEdW1teSA9IHsKICBmb286ICJmb28iLAp9CgovKioKICogZHVtbXkKICogZHVtbXkKICovCmV4cG9ydCBkZWZhdWx0IGZ1bmN0aW9uIHRlc3RTdGFjaygpIHsKICBpbm5lclRlc3RTdGFjaygpCn0KCmltcG9ydCAnbm9kZTp1dGlsJwoKLyoqCiAqIGJhcgogKiBiYXIKICovCmZ1bmN0aW9uIGlubmVyVGVzdFN0YWNrKCkgewogIHRocm93IG5ldyBFcnJvcignX19URVNUX1NUQUNLX1RSQU5TUElMRURfXycpCn0

‎test/cli/deps/error/ts.ts

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'node:path'
2+
3+
export type Dummy = {
4+
foo: "foo",
5+
}
6+
7+
/**
8+
* dummy
9+
* dummy
10+
*/
11+
export default function testStack() {
12+
innerTestStack()
13+
}
14+
15+
import 'node:util'
16+
17+
/**
18+
* bar
19+
* bar
20+
*/
21+
function innerTestStack() {
22+
throw new Error('__TEST_STACK_TS__')
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { test } from 'vitest'
2+
import testStack from "@vitest/test-dep-error"
3+
import testStackTs from "@vitest/test-dep-error/ts.ts"
4+
import testStackTranspiled from "@vitest/test-dep-error/transpiled.js"
5+
import testStackTranspiledInline from "@vitest/test-dep-error/transpiled-inline.js"
6+
7+
test('js', () => {
8+
testStack()
9+
})
10+
11+
test('ts', () => {
12+
testStackTs()
13+
})
14+
15+
test('transpiled', () => {
16+
testStackTranspiled()
17+
})
18+
19+
test('transpiled inline', () => {
20+
testStackTranspiledInline()
21+
})

‎test/cli/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"@types/ws": "^8.5.9",
1111
"@vitejs/plugin-basic-ssl": "^1.0.2",
1212
"@vitest/runner": "workspace:^",
13+
"@vitest/test-dep-error": "file:./deps/error",
1314
"@vitest/utils": "workspace:*",
1415
"debug": "^4.3.4",
1516
"unplugin-swc": "^1.4.4",

‎test/cli/test/__snapshots__/stacktraces.test.ts.snap

+137
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,132 @@ Error: Something truly horrible has happened!
2222
"
2323
`;
2424

25+
exports[`stacktrace in dependency package > external 1`] = `
26+
"
27+
⎯⎯ Failed Tests 4 ⎯⎯
28+
29+
FAIL error-in-package.test.js > js
30+
Error: __TEST_STACK__
31+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/index.js:10:9
32+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/index.js:4:3
33+
❯ error-in-package.test.js:8:12
34+
6|
35+
7| test('js', () => {
36+
8| testStack()
37+
| ^
38+
9| })
39+
10|
40+
41+
⎯⎯[1/4]⎯
42+
43+
FAIL error-in-package.test.js > ts
44+
Error: __TEST_STACK_TS__
45+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:22:9
46+
20| */
47+
21| function innerTestStack() {
48+
22| throw new Error('__TEST_STACK_TS__')
49+
| ^
50+
23| }
51+
24|
52+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:12:3
53+
❯ error-in-package.test.js:12:14
54+
55+
⎯⎯[2/4]⎯
56+
57+
FAIL error-in-package.test.js > transpiled
58+
Error: __TEST_STACK_TRANSPILED__
59+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:22:9
60+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:12:3
61+
❯ error-in-package.test.js:16:22
62+
14|
63+
15| test('transpiled', () => {
64+
16| testStackTranspiled()
65+
| ^
66+
17| })
67+
18|
68+
69+
⎯⎯[3/4]⎯
70+
71+
FAIL error-in-package.test.js > transpiled inline
72+
Error: __TEST_STACK_TRANSPILED_INLINE__
73+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:22:9
74+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:12:3
75+
❯ error-in-package.test.js:20:28
76+
18|
77+
19| test('transpiled inline', () => {
78+
20| testStackTranspiledInline()
79+
| ^
80+
21| })
81+
22|
82+
83+
⎯⎯[4/4]⎯
84+
85+
"
86+
`;
87+
88+
exports[`stacktrace in dependency package > inline 1`] = `
89+
"
90+
⎯⎯ Failed Tests 4 ⎯⎯
91+
92+
FAIL error-in-package.test.js > js
93+
Error: __TEST_STACK__
94+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/index.js:10:9
95+
8|
96+
9| function innerTestStack() {
97+
10| throw new Error('__TEST_STACK__')
98+
| ^
99+
11| }
100+
12|
101+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/index.js:4:3
102+
❯ error-in-package.test.js:8:12
103+
104+
⎯⎯[1/4]⎯
105+
106+
FAIL error-in-package.test.js > ts
107+
Error: __TEST_STACK_TS__
108+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:22:9
109+
20| */
110+
21| function innerTestStack() {
111+
22| throw new Error('__TEST_STACK_TS__')
112+
| ^
113+
23| }
114+
24|
115+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:12:3
116+
❯ error-in-package.test.js:12:14
117+
118+
⎯⎯[2/4]⎯
119+
120+
FAIL error-in-package.test.js > transpiled
121+
Error: __TEST_STACK_TRANSPILED__
122+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:22:9
123+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:12:3
124+
❯ error-in-package.test.js:16:22
125+
14|
126+
15| test('transpiled', () => {
127+
16| testStackTranspiled()
128+
| ^
129+
17| })
130+
18|
131+
132+
⎯⎯[3/4]⎯
133+
134+
FAIL error-in-package.test.js > transpiled inline
135+
Error: __TEST_STACK_TRANSPILED_INLINE__
136+
❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:22:9
137+
❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:12:3
138+
❯ error-in-package.test.js:20:28
139+
18|
140+
19| test('transpiled inline', () => {
141+
20| testStackTranspiledInline()
142+
| ^
143+
21| })
144+
22|
145+
146+
⎯⎯[4/4]⎯
147+
148+
"
149+
`;
150+
25151
exports[`stacktrace in vmThreads 1`] = `
26152
"
27153
⎯⎯ Failed Tests 1 ⎯⎯
@@ -117,6 +243,17 @@ exports[`stacktraces should respect sourcemaps > error-in-deps.test.js 1`] = `
117243
"
118244
`;
119245
246+
exports[`stacktraces should respect sourcemaps > error-in-package.test.js 1`] = `
247+
" ❯ error-in-package.test.js:8:12
248+
6|
249+
7| test('js', () => {
250+
8| testStack()
251+
| ^
252+
9| })
253+
10|
254+
"
255+
`;
256+
120257
exports[`stacktraces should respect sourcemaps > error-with-stack.test.js 1`] = `
121258
" ❯ d error-with-stack.test.js:20:9
122259
18|

‎test/cli/test/stacktraces.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ describe('stacktrace filtering', async () => {
6868
})
6969
})
7070

71+
describe('stacktrace in dependency package', () => {
72+
const root = resolve(__dirname, '../fixtures/stacktraces')
73+
const testFile = resolve(root, './error-in-package.test.js')
74+
75+
it('external', async () => {
76+
const { stderr } = await runVitest({
77+
root,
78+
}, [testFile])
79+
expect(removeNodeModules(removeLines(stderr))).toMatchSnapshot()
80+
})
81+
82+
it('inline', async () => {
83+
const { stderr } = await runVitest({
84+
root,
85+
server: {
86+
deps: {
87+
inline: [/@vitest\/test-dep-error/],
88+
},
89+
},
90+
}, [testFile])
91+
expect(removeNodeModules(removeLines(stderr))).toMatchSnapshot()
92+
})
93+
})
94+
7195
it.runIf(major < 22)('stacktrace in vmThreads', async () => {
7296
const root = resolve(__dirname, '../fixtures/stacktraces')
7397
const testFile = resolve(root, './error-with-stack.test.js')
@@ -82,3 +106,7 @@ it.runIf(major < 22)('stacktrace in vmThreads', async () => {
82106
function removeLines(log: string) {
83107
return log.replace(/{2,}/g, '⎯⎯')
84108
}
109+
110+
function removeNodeModules(log: string) {
111+
return log.replace(/[^ ]*\/node_modules\//g, '(NODE_MODULES)/')
112+
}

0 commit comments

Comments
 (0)
Please sign in to comment.