Skip to content

Commit fe79062

Browse files
lazerghi-ogawaOpenCode
authored
fix(utils): don't crash on a malformed inline source map (fix #10892) (#10893)
Co-authored-by: Hiroshi Ogawa <[email protected]> Co-authored-by: OpenCode <[email protected]> Co-authored-by: Hiroshi Ogawa <[email protected]>
1 parent 76096c7 commit fe79062

14 files changed

Lines changed: 100 additions & 8 deletions

File tree

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export default antfu(
2626
'test/coverage-test/src/transpiled.js',
2727
'test/coverage-test/src/original.ts',
2828
'test/e2e/deps/error/*',
29+
'test/e2e/deps/malformed-source-map/*.js',
2930
'examples/**/mockServiceWorker.js',
3031
'examples/sveltekit/.svelte-kit',
3132
'packages/browser/**/esm-client-injector.js',

packages/utils/src/source-map/node.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,19 @@ export function extractSourcemapFromFile(
1212
code: string,
1313
filePath: string,
1414
): ExtractedSourceMap | undefined {
15-
const map = (
16-
convertSourceMap.fromSource(code)
17-
|| convertSourceMap.fromMapFileSource(
18-
code,
19-
createConvertSourceMapReadMap(filePath),
20-
)
21-
)?.toObject()
22-
return map ? { map } : undefined
15+
try {
16+
const map = (
17+
convertSourceMap.fromSource(code)
18+
|| convertSourceMap.fromMapFileSource(
19+
code,
20+
createConvertSourceMapReadMap(filePath),
21+
)
22+
)?.toObject()
23+
return map ? { map } : undefined
24+
}
25+
catch {
26+
return undefined
27+
}
2328
}
2429

2530
function createConvertSourceMapReadMap(originalFileName: string) {

packages/vitest/src/node/test-run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ export class TestRun {
189189
// source map is already applied for inlined modules.
190190
// Module node exists due to Vitest fetch module,
191191
// but transformResult should be empty for external modules.
192+
// TODO: this uses client module graph and thus miss detects
193+
// inlined modules in ssr environment as if external.
192194
const mod = project.vite.moduleGraph.getModuleById(file)
193195
if (!mod?.transformResult && existsSync(file)) {
194196
const code = readFileSync(file, 'utf-8')

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/deps/malformed-source-map/external-map.js

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/deps/malformed-source-map/external-map.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const embeddedSourceMapComment = `
2+
//# sourceMappingURL=data:application/json;base64,bm90LWpzb24=
3+
`
4+
5+
export default function testMalformedSourceMap() {
6+
throw new Error('test error')
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "@test/test-dep-malformed-source-map",
3+
"type": "module",
4+
"private": true,
5+
"exports": {
6+
".": "./index.js",
7+
"./external-map": "./external-map.js"
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from 'vitest'
2+
import testMalformedSourceMap from '@test/test-dep-malformed-source-map/external-map'
3+
4+
test('reports the original module error', () => {
5+
testMalformedSourceMap()
6+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { test } from 'vitest'
2+
import testMalformedSourceMap from '@test/test-dep-malformed-source-map'
3+
4+
test('reports the original module error', () => {
5+
testMalformedSourceMap()
6+
})

0 commit comments

Comments
 (0)