Commit eb506d6f authored by RobPruzan's avatar RobPruzan

fix: preprocess windows files correctly

parent 5e65f807
import { stripFormatSpecifiers } from './receive-logs'
import { preprocessStackTrace } from './source-map'
describe('stripFormatSpecifiers', () => {
it('should only process when first arg is string containing %', () => {
......@@ -72,3 +73,26 @@ describe('stripFormatSpecifiers', () => {
expect(stripFormatSpecifiers(['ends with %'])).toEqual(['ends with %'])
})
})
describe('preprocessStackTrace', () => {
it('should convert _next/static paths to file URLs (posix)', () => {
const input = ' at myFunc (_next/static/chunks/app/page.js:1:1)'
const distDir = '/project/.next'
const output = preprocessStackTrace(input, distDir)
expect(output).toContain('file://')
expect(output).toContain('/project/.next/static/chunks/app/page.js:1:1')
})
it('should convert _next static paths with backslashes (windows) to file URLs', () => {
const input = ' at myFunc (_next\\static\\chunks\\app\\page.js:1:1)'
const distDir = '/project/.next'
const output = preprocessStackTrace(input, distDir)
expect(output).toContain('file://')
expect(output).toContain('/project/.next/static/chunks/app/page.js:1:1')
expect(output).not.toContain('_next/static')
expect(output).not.toContain('\\')
})
})
......@@ -79,22 +79,29 @@ export async function mapFramesUsingBundler(
// converts _next/static/chunks/... to file:///.next/static/chunks/... for parseStack
// todo: where does next dev overlay handle this case and re-use that logic
function preprocessStackTrace(stackTrace: string, distDir?: string): string {
export function preprocessStackTrace(
stackTrace: string,
distDir?: string
): string {
const toForwardSlashes = (p: string) => p.replace(/\\/g, '/')
return stackTrace
.split('\n')
.map((line) => {
const match = line.match(/^(\s*at\s+.*?)\s+\(([^)]+)\)$/)
if (match) {
const [, prefix, location] = match
const [, prefix, rawLocation] = match
const location = toForwardSlashes(rawLocation)
if (location.startsWith('_next/static/') && distDir) {
const normalizedDistDir = distDir
.replace(/\\/g, '/')
.replace(/\/$/, '')
const normalizedDistDir = toForwardSlashes(distDir).replace(/\/$/, '')
const absolutePath =
normalizedDistDir + '/' + location.slice('_next/'.length)
const fileUrl = `file://${path.resolve(absolutePath)}`
const resolved = toForwardSlashes(path.resolve(absolutePath))
const fileUrl = `file://${resolved}`
return `${prefix} (${fileUrl})`
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment