Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/vite/src/module-runner/sourcemap/interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OriginalMapping } from '@jridgewell/trace-mapping'
import { slash } from '../../shared/utils'
import { decodeSourceURL, slash } from '../../shared/utils'
import type { EvaluatedModules } from '../evaluatedModules'
import type { ModuleRunner } from '../runner'
import { decodeBase64, posixDirname, posixResolve } from '../utils'
Expand Down Expand Up @@ -102,11 +102,12 @@ function supportRelativeURL(file: string, url: string) {
}

function getRunnerSourceMap(position: OriginalMapping): CachedMapEntry | null {
const id = decodeSourceURL(position.source!)
for (const moduleGraph of evaluatedModulesCache) {
const sourceMap = moduleGraph.getModuleSourceMapById(position.source!)
const sourceMap = moduleGraph.getModuleSourceMapById(id)
if (sourceMap) {
return {
url: position.source,
url: id,
map: sourceMap,
vite: true,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function main() {
throw new Error()
}
18 changes: 18 additions & 0 deletions packages/vite/src/node/ssr/__tests__/ssrStacktrace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,24 @@ test('call rewriteStacktrace twice', async () => {
}
})

test('rewrites stacktrace when the module path contains a space', async () => {
const server = await createDevServer()
const mod = await server.ssrLoadModule(
'/fixtures/modules/with space/has-error.js',
)
try {
mod.main()
expect.unreachable()
} catch (e: any) {
server.ssrFixStacktrace(e)
expect(
e.stack.split('\n')[1].replace(root, '<root>').replaceAll('\\', '/'),
).toBe(
' at Module.main (<root>fixtures/modules/with space/has-error.js:2:9)',
)
}
})

test('outputs message when stacktrace appears to be already rewritten', async () => {
const server = await createDevServer()
try {
Expand Down
8 changes: 4 additions & 4 deletions packages/vite/src/node/ssr/fetchModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MODULE_RUNNER_SOURCEMAPPING_SOURCE,
SOURCEMAPPING_URL,
} from '../../shared/constants'
import { unwrapId } from '../../shared/utils'
import { encodeSourceURL, unwrapId } from '../../shared/utils'
import { tryNodeResolve } from '../plugins/resolve'
import type { DevEnvironment } from '../server/environment'
import { genSourceMapUrl } from '../server/sourcemap'
Expand Down Expand Up @@ -146,9 +146,9 @@ function inlineSourceMap(
mappings: ';'.repeat(startOffset) + map.mappings,
}
: map
result.code = `${code.trimEnd()}\n//# sourceURL=${
mod.id
}\n${MODULE_RUNNER_SOURCEMAPPING_SOURCE}\n//# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}\n`
result.code = `${code.trimEnd()}\n//# sourceURL=${encodeSourceURL(
mod.id!,
)}\n${MODULE_RUNNER_SOURCEMAPPING_SOURCE}\n//# ${SOURCEMAPPING_URL}=${genSourceMapUrl(sourceMap)}\n`

return result
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function crash(message: string) {
throw new Error(message)
}

export function main(): void {
crash('crash')
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,21 @@ describe('module runner initialization', async () => {
])
})

it('maps stack traces when the module path contains a space', async ({
runner,
server,
}) => {
const methodError = await getError(async () => {
const mod = await runner.import('/fixtures/with space/has-error-deep.ts')
mod.main()
})
expect(serializeStackDeep(server, methodError).slice(0, 3)).toEqual([
'Error: crash',
' at crash (<root>/fixtures/with space/has-error-deep.ts:2:9)',
' at Module.main (<root>/fixtures/with space/has-error-deep.ts:6:3)',
])
})

it('should not crash when sourceMappingURL pattern appears in string literals', async ({
runner,
server,
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/ssr/ssrStacktrace.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path'
import { TraceMap, originalPositionFor } from '@jridgewell/trace-mapping'
import type { EnvironmentModuleGraph } from '..'
import { decodeSourceURL } from '../../shared/utils'

let offset: number

Expand Down Expand Up @@ -34,9 +35,10 @@ export function ssrRewriteStacktrace(
.map((line) => {
return line.replace(
/^ {4}at (?:(\S.*?)\s\()?(.+?):(\d+)(?::(\d+))?\)?/,
(input, varName, id, originalLine, originalColumn) => {
if (!id) return input
(input, varName, rawId, originalLine, originalColumn) => {
if (!rawId) return input

const id = decodeSourceURL(rawId)
const mod = moduleGraph.getModuleById(id)
const rawSourceMap = mod?.transformResult?.map

Expand Down
22 changes: 22 additions & 0 deletions packages/vite/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,25 @@ export function promiseWithResolvers<T>(): PromiseWithResolvers<T> {
})
return { promise, resolve, reject }
}

const whitespaceRE = /\s/g
const percentEncodedRE = /(?:%[0-9a-f]{2})+/gi
const whitespaceOnlyRE = /^\s+$/

/** Percent-encodes whitespace, which terminates a `//# sourceURL=` value in V8. */
export function encodeSourceURL(id: string): string {
return id.replace(whitespaceRE, encodeURIComponent)
}

/** Reverses {@link encodeSourceURL}. */
export function decodeSourceURL(url: string): string {
return url.replace(percentEncodedRE, (match) => {
let decoded: string
try {
decoded = decodeURIComponent(match)
} catch {
return match
}
return whitespaceOnlyRE.test(decoded) ? decoded : match
})
}