Skip to content

Commit 62beae1

Browse files
johnlindquistclaude
andcommitted
fix: fix sourcemap formatter tests for ESM and cross-platform paths
- Add __filename polyfill for ESM modules using import.meta.url - Fix FILE_URL_REGEX to preserve leading slash in file:/// URLs - Ensure absolute paths remain absolute after normalization The tests were failing in CI because: 1. __filename is not available in ESM modules 2. The file:// URL regex was removing all slashes including the path separator 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 3d42e39 commit 62beae1

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/core/sourcemap-formatter.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ava from 'ava'
22
import { SourcemapErrorFormatter } from './sourcemap-formatter.js'
33
import os from 'os'
4+
import { fileURLToPath } from 'url'
5+
6+
const __filename = fileURLToPath(import.meta.url)
47

58
ava('formatError should parse basic error stack', (t) => {
69
const error = new Error('Test error')

src/core/sourcemap-formatter.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export interface FormattedError {
2323
export class SourcemapErrorFormatter {
2424
private static readonly STACK_FRAME_REGEX = /^\s*at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?$/
2525
private static readonly WINDOWS_PATH_REGEX = /^([a-zA-Z]:\\|\\\\)/
26-
private static readonly FILE_URL_REGEX = /^file:\/\/\/?/
26+
private static readonly FILE_URL_REGEX = /^file:\/\//
2727

2828
/**
2929
* Formats an error with enhanced stack trace information
@@ -63,9 +63,15 @@ export class SourcemapErrorFormatter {
6363
file = file.substring(1)
6464
}
6565

66-
// Normalize path for current platform
66+
// Ensure absolute paths remain absolute after normalization
67+
const isAbsolutePath = file.startsWith('/') || this.WINDOWS_PATH_REGEX.test(file)
6768
file = normalize(file)
6869

70+
// On Unix, normalize may strip leading slash, restore it if needed
71+
if (isAbsolutePath && !file.startsWith('/') && os.platform() !== 'win32') {
72+
file = '/' + file
73+
}
74+
6975
frames.push({
7076
file,
7177
line: parseInt(lineStr, 10),
@@ -155,10 +161,16 @@ export class SourcemapErrorFormatter {
155161
}
156162

157163
// Resolve relative paths
158-
const resolvedPath = isAbsolute(cleanPath)
164+
const isAbsolutePath = cleanPath.startsWith('/') || this.WINDOWS_PATH_REGEX.test(cleanPath)
165+
let resolvedPath = isAbsolute(cleanPath)
159166
? normalize(cleanPath)
160167
: resolve(basePath || process.cwd(), cleanPath)
161168

169+
// On Unix, normalize may strip leading slash, restore it if needed
170+
if (isAbsolutePath && !resolvedPath.startsWith('/') && os.platform() !== 'win32') {
171+
resolvedPath = '/' + resolvedPath
172+
}
173+
162174
// Check if file exists
163175
if (existsSync(resolvedPath)) {
164176
return resolvedPath

0 commit comments

Comments
 (0)