diff --git a/eslint.config.js b/eslint.config.js index 3f4556c694b5..0cbb5e63500f 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -23,6 +23,7 @@ export default antfu( 'test/network-imports/public/slash@3.0.0.js', 'test/coverage-test/src/transpiled.js', 'test/coverage-test/src/original.ts', + 'test/cli/deps/error/*', 'examples/**/mockServiceWorker.js', 'examples/sveltekit/.svelte-kit', 'packages/browser/**/esm-client-injector.js', diff --git a/packages/vite-node/src/server.ts b/packages/vite-node/src/server.ts index 0bbbe75ee958..b560ec141bf0 100644 --- a/packages/vite-node/src/server.ts +++ b/packages/vite-node/src/server.ts @@ -428,7 +428,7 @@ export class ViteNodeServer { } const sourcemap = this.options.sourcemap ?? 'inline' - if (sourcemap === 'inline' && result && !id.includes('node_modules')) { + if (sourcemap === 'inline' && result) { result = await this.processTransformResult(filepath, result) } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 92ace05fd9d8..52701af0722a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1099,6 +1099,9 @@ importers: '@vitest/runner': specifier: workspace:^ version: link:../../packages/runner + '@vitest/test-dep-error': + specifier: file:./deps/error + version: file:test/cli/deps/error '@vitest/utils': specifier: workspace:* version: link:../../packages/utils @@ -3941,6 +3944,9 @@ packages: '@vitest/test-dep-conditions@file:test/config/deps/test-dep-conditions': resolution: {directory: test/config/deps/test-dep-conditions, type: directory} + '@vitest/test-dep-error@file:test/cli/deps/error': + resolution: {directory: test/cli/deps/error, type: directory} + '@vitest/test-dep1@file:test/core/deps/dep1': resolution: {directory: test/core/deps/dep1, type: directory} @@ -12131,6 +12137,8 @@ snapshots: dependencies: '@vitest/test-dep-conditions-indirect': file:test/config/deps/test-dep-conditions-indirect + '@vitest/test-dep-error@file:test/cli/deps/error': {} + '@vitest/test-dep1@file:test/core/deps/dep1': {} '@vitest/test-dep2@file:test/core/deps/dep2': diff --git a/test/cli/deps/error/index.js b/test/cli/deps/error/index.js new file mode 100644 index 000000000000..064e1a1be7ee --- /dev/null +++ b/test/cli/deps/error/index.js @@ -0,0 +1,11 @@ +import 'node:path' + +export default function testStack() { + innerTestStack() +} + +import 'node:util' + +function innerTestStack() { + throw new Error('__TEST_STACK__') +} diff --git a/test/cli/deps/error/package.json b/test/cli/deps/error/package.json new file mode 100644 index 000000000000..a254a0692069 --- /dev/null +++ b/test/cli/deps/error/package.json @@ -0,0 +1,9 @@ +{ + "name": "@vitest/test-dep-error", + "type": "module", + "private": true, + "exports": { + ".": "./index.js", + "./*": "./*" + } +} diff --git a/test/cli/deps/error/transpiled-inline.js b/test/cli/deps/error/transpiled-inline.js new file mode 100644 index 000000000000..cde9d7047e50 --- /dev/null +++ b/test/cli/deps/error/transpiled-inline.js @@ -0,0 +1,9 @@ +import "node:path"; +export default function testStack() { + innerTestStack(); +} +import "node:util"; +function innerTestStack() { + throw new Error("__TEST_STACK_TRANSPILED_INLINE__"); +} +//# sourceMappingURL=data:application/json;base64,ewogICJ2ZXJzaW9uIjogMywKICAic291cmNlcyI6IFsidHJhbnNwaWxlZC1pbmxpbmUudHMiXSwKICAic291cmNlc0NvbnRlbnQiOiBbImltcG9ydCAnbm9kZTpwYXRoJ1xuXG5leHBvcnQgdHlwZSBEdW1teSA9IHtcbiAgZm9vOiBcImZvb1wiLFxufVxuXG4vKipcbiAqIGR1bW15XG4gKiBkdW1teVxuICovXG5leHBvcnQgZGVmYXVsdCBmdW5jdGlvbiB0ZXN0U3RhY2soKSB7XG4gIGlubmVyVGVzdFN0YWNrKClcbn1cblxuaW1wb3J0ICdub2RlOnV0aWwnXG5cbi8qKlxuICogYmFyXG4gKiBiYXJcbiAqL1xuZnVuY3Rpb24gaW5uZXJUZXN0U3RhY2soKSB7XG4gIHRocm93IG5ldyBFcnJvcignX19URVNUX1NUQUNLX1RSQU5TUElMRURfSU5MSU5FX18nKVxufSJdLAogICJtYXBwaW5ncyI6ICJBQUFBLE9BQU87QUFVUCx3QkFBd0IsWUFBWTtBQUNsQyxpQkFBZTtBQUNqQjtBQUVBLE9BQU87QUFNUCxTQUFTLGlCQUFpQjtBQUN4QixRQUFNLElBQUksTUFBTSxrQ0FBa0M7QUFDcEQ7IiwKICAibmFtZXMiOiBbXQp9Cg== diff --git a/test/cli/deps/error/transpiled-inline.ts b/test/cli/deps/error/transpiled-inline.ts new file mode 100644 index 000000000000..1c8c6c630ae4 --- /dev/null +++ b/test/cli/deps/error/transpiled-inline.ts @@ -0,0 +1,26 @@ +import 'node:path' + +export type Dummy = { + foo: "foo", +} + +/** + * dummy + * dummy + */ +export default function testStack() { + innerTestStack() +} + +import 'node:util' + +/** + * bar + * bar + */ +function innerTestStack() { + throw new Error('__TEST_STACK_TRANSPILED_INLINE__') +} + +// transpiled-inline.js is copied from +// https://esbuild.github.io/try/#dAAwLjI1LjAALS1sb2FkZXI9dHMgLS1zb3VyY2VtYXA9aW5saW5lIC0tc291cmNlZmlsZT10cmFuc3BpbGVkLWlubGluZS50cwBpbXBvcnQgJ25vZGU6cGF0aCcKCmV4cG9ydCB0eXBlIER1bW15ID0gewogIGZvbzogImZvbyIsCn0KCi8qKgogKiBkdW1teQogKiBkdW1teQogKi8KZXhwb3J0IGRlZmF1bHQgZnVuY3Rpb24gdGVzdFN0YWNrKCkgewogIGlubmVyVGVzdFN0YWNrKCkKfQoKaW1wb3J0ICdub2RlOnV0aWwnCgovKioKICogYmFyCiAqIGJhcgogKi8KZnVuY3Rpb24gaW5uZXJUZXN0U3RhY2soKSB7CiAgdGhyb3cgbmV3IEVycm9yKCdfX1RFU1RfU1RBQ0tfVFJBTlNQSUxFRF9JTkxJTkVfXycpCn0 diff --git a/test/cli/deps/error/transpiled.js b/test/cli/deps/error/transpiled.js new file mode 100644 index 000000000000..aadc738fd2f2 --- /dev/null +++ b/test/cli/deps/error/transpiled.js @@ -0,0 +1,9 @@ +import "node:path"; +export default function testStack() { + innerTestStack(); +} +import "node:util"; +function innerTestStack() { + throw new Error("__TEST_STACK_TRANSPILED__"); +} +//# sourceMappingURL=transpiled.js.map diff --git a/test/cli/deps/error/transpiled.js.map b/test/cli/deps/error/transpiled.js.map new file mode 100644 index 000000000000..d27537c30a50 --- /dev/null +++ b/test/cli/deps/error/transpiled.js.map @@ -0,0 +1,7 @@ +{ + "version": 3, + "sources": ["transpiled.ts"], + "sourcesContent": ["import 'node:path'\n\nexport type Dummy = {\n foo: \"foo\",\n}\n\n/**\n * dummy\n * dummy\n */\nexport default function testStack() {\n innerTestStack()\n}\n\nimport 'node:util'\n\n/**\n * bar\n * bar\n */\nfunction innerTestStack() {\n throw new Error('__TEST_STACK_TRANSPILED__')\n}"], + "mappings": "AAAA,OAAO;AAUP,wBAAwB,YAAY;AAClC,iBAAe;AACjB;AAEA,OAAO;AAMP,SAAS,iBAAiB;AACxB,QAAM,IAAI,MAAM,2BAA2B;AAC7C;", + "names": [] +} diff --git a/test/cli/deps/error/transpiled.ts b/test/cli/deps/error/transpiled.ts new file mode 100644 index 000000000000..364e52545847 --- /dev/null +++ b/test/cli/deps/error/transpiled.ts @@ -0,0 +1,26 @@ +import 'node:path' + +export type Dummy = { + foo: "foo", +} + +/** + * dummy + * dummy + */ +export default function testStack() { + innerTestStack() +} + +import 'node:util' + +/** + * bar + * bar + */ +function innerTestStack() { + throw new Error('__TEST_STACK_TRANSPILED__') +} + +// transpiled.js and transpiled.js.map are copied from +// https://esbuild.github.io/try/#dAAwLjI1LjAALS1sb2FkZXI9dHMgLS1zb3VyY2VtYXAgLS1zb3VyY2VmaWxlPXRyYW5zcGlsZWQudHMAaW1wb3J0ICdub2RlOnBhdGgnCgpleHBvcnQgdHlwZSBEdW1teSA9IHsKICBmb286ICJmb28iLAp9CgovKioKICogZHVtbXkKICogZHVtbXkKICovCmV4cG9ydCBkZWZhdWx0IGZ1bmN0aW9uIHRlc3RTdGFjaygpIHsKICBpbm5lclRlc3RTdGFjaygpCn0KCmltcG9ydCAnbm9kZTp1dGlsJwoKLyoqCiAqIGJhcgogKiBiYXIKICovCmZ1bmN0aW9uIGlubmVyVGVzdFN0YWNrKCkgewogIHRocm93IG5ldyBFcnJvcignX19URVNUX1NUQUNLX1RSQU5TUElMRURfXycpCn0 diff --git a/test/cli/deps/error/ts.ts b/test/cli/deps/error/ts.ts new file mode 100644 index 000000000000..dbb1f926dc0e --- /dev/null +++ b/test/cli/deps/error/ts.ts @@ -0,0 +1,23 @@ +import 'node:path' + +export type Dummy = { + foo: "foo", +} + +/** + * dummy + * dummy + */ +export default function testStack() { + innerTestStack() +} + +import 'node:util' + +/** + * bar + * bar + */ +function innerTestStack() { + throw new Error('__TEST_STACK_TS__') +} diff --git a/test/cli/fixtures/stacktraces/error-in-package.test.js b/test/cli/fixtures/stacktraces/error-in-package.test.js new file mode 100644 index 000000000000..91cdf48f8c55 --- /dev/null +++ b/test/cli/fixtures/stacktraces/error-in-package.test.js @@ -0,0 +1,21 @@ +import { test } from 'vitest' +import testStack from "@vitest/test-dep-error" +import testStackTs from "@vitest/test-dep-error/ts.ts" +import testStackTranspiled from "@vitest/test-dep-error/transpiled.js" +import testStackTranspiledInline from "@vitest/test-dep-error/transpiled-inline.js" + +test('js', () => { + testStack() +}) + +test('ts', () => { + testStackTs() +}) + +test('transpiled', () => { + testStackTranspiled() +}) + +test('transpiled inline', () => { + testStackTranspiledInline() +}) diff --git a/test/cli/package.json b/test/cli/package.json index f8fa07c1f3c9..f558fb18d56d 100644 --- a/test/cli/package.json +++ b/test/cli/package.json @@ -10,6 +10,7 @@ "@types/ws": "^8.5.9", "@vitejs/plugin-basic-ssl": "^1.0.2", "@vitest/runner": "workspace:^", + "@vitest/test-dep-error": "file:./deps/error", "@vitest/utils": "workspace:*", "debug": "^4.3.4", "unplugin-swc": "^1.4.4", diff --git a/test/cli/test/__snapshots__/stacktraces.test.ts.snap b/test/cli/test/__snapshots__/stacktraces.test.ts.snap index f7b31e47c7da..2260ed52b601 100644 --- a/test/cli/test/__snapshots__/stacktraces.test.ts.snap +++ b/test/cli/test/__snapshots__/stacktraces.test.ts.snap @@ -22,6 +22,132 @@ Error: Something truly horrible has happened! " `; +exports[`stacktrace in dependency package > external 1`] = ` +" +⎯⎯ Failed Tests 4 ⎯⎯ + + FAIL error-in-package.test.js > js +Error: __TEST_STACK__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/index.js:10:9 + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/index.js:4:3 + ❯ error-in-package.test.js:8:12 + 6| + 7| test('js', () => { + 8| testStack() + | ^ + 9| }) + 10| + +⎯⎯[1/4]⎯ + + FAIL error-in-package.test.js > ts +Error: __TEST_STACK_TS__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:22:9 + 20| */ + 21| function innerTestStack() { + 22| throw new Error('__TEST_STACK_TS__') + | ^ + 23| } + 24| + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:12:3 + ❯ error-in-package.test.js:12:14 + +⎯⎯[2/4]⎯ + + FAIL error-in-package.test.js > transpiled +Error: __TEST_STACK_TRANSPILED__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:22:9 + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:12:3 + ❯ error-in-package.test.js:16:22 + 14| + 15| test('transpiled', () => { + 16| testStackTranspiled() + | ^ + 17| }) + 18| + +⎯⎯[3/4]⎯ + + FAIL error-in-package.test.js > transpiled inline +Error: __TEST_STACK_TRANSPILED_INLINE__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:22:9 + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:12:3 + ❯ error-in-package.test.js:20:28 + 18| + 19| test('transpiled inline', () => { + 20| testStackTranspiledInline() + | ^ + 21| }) + 22| + +⎯⎯[4/4]⎯ + +" +`; + +exports[`stacktrace in dependency package > inline 1`] = ` +" +⎯⎯ Failed Tests 4 ⎯⎯ + + FAIL error-in-package.test.js > js +Error: __TEST_STACK__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/index.js:10:9 + 8| + 9| function innerTestStack() { + 10| throw new Error('__TEST_STACK__') + | ^ + 11| } + 12| + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/index.js:4:3 + ❯ error-in-package.test.js:8:12 + +⎯⎯[1/4]⎯ + + FAIL error-in-package.test.js > ts +Error: __TEST_STACK_TS__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:22:9 + 20| */ + 21| function innerTestStack() { + 22| throw new Error('__TEST_STACK_TS__') + | ^ + 23| } + 24| + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/ts.ts:12:3 + ❯ error-in-package.test.js:12:14 + +⎯⎯[2/4]⎯ + + FAIL error-in-package.test.js > transpiled +Error: __TEST_STACK_TRANSPILED__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:22:9 + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled.ts:12:3 + ❯ error-in-package.test.js:16:22 + 14| + 15| test('transpiled', () => { + 16| testStackTranspiled() + | ^ + 17| }) + 18| + +⎯⎯[3/4]⎯ + + FAIL error-in-package.test.js > transpiled inline +Error: __TEST_STACK_TRANSPILED_INLINE__ + ❯ innerTestStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:22:9 + ❯ testStack (NODE_MODULES)/@vitest/test-dep-error/transpiled-inline.ts:12:3 + ❯ error-in-package.test.js:20:28 + 18| + 19| test('transpiled inline', () => { + 20| testStackTranspiledInline() + | ^ + 21| }) + 22| + +⎯⎯[4/4]⎯ + +" +`; + exports[`stacktrace in vmThreads 1`] = ` " ⎯⎯ Failed Tests 1 ⎯⎯ @@ -117,6 +243,17 @@ exports[`stacktraces should respect sourcemaps > error-in-deps.test.js 1`] = ` " `; +exports[`stacktraces should respect sourcemaps > error-in-package.test.js 1`] = ` +" ❯ error-in-package.test.js:8:12 + 6| + 7| test('js', () => { + 8| testStack() + | ^ + 9| }) + 10| +" +`; + exports[`stacktraces should respect sourcemaps > error-with-stack.test.js 1`] = ` " ❯ d error-with-stack.test.js:20:9 18| diff --git a/test/cli/test/stacktraces.test.ts b/test/cli/test/stacktraces.test.ts index 6798f8febe03..6452326a395b 100644 --- a/test/cli/test/stacktraces.test.ts +++ b/test/cli/test/stacktraces.test.ts @@ -68,6 +68,30 @@ describe('stacktrace filtering', async () => { }) }) +describe('stacktrace in dependency package', () => { + const root = resolve(__dirname, '../fixtures/stacktraces') + const testFile = resolve(root, './error-in-package.test.js') + + it('external', async () => { + const { stderr } = await runVitest({ + root, + }, [testFile]) + expect(removeNodeModules(removeLines(stderr))).toMatchSnapshot() + }) + + it('inline', async () => { + const { stderr } = await runVitest({ + root, + server: { + deps: { + inline: [/@vitest\/test-dep-error/], + }, + }, + }, [testFile]) + expect(removeNodeModules(removeLines(stderr))).toMatchSnapshot() + }) +}) + it.runIf(major < 22)('stacktrace in vmThreads', async () => { const root = resolve(__dirname, '../fixtures/stacktraces') const testFile = resolve(root, './error-with-stack.test.js') @@ -82,3 +106,7 @@ it.runIf(major < 22)('stacktrace in vmThreads', async () => { function removeLines(log: string) { return log.replace(/⎯{2,}/g, '⎯⎯') } + +function removeNodeModules(log: string) { + return log.replace(/[^ ]*\/node_modules\//g, '(NODE_MODULES)/') +}