Skip to content

Commit 986094d

Browse files
Brooooooklynclaude
andauthored
fix(register): always inline source maps in auto mode so debuggers work (#1062)
The auto source-map mode introduced in 1.12.0 only inlined the map into the emitted code when `process.sourceMapsEnabled` was true (i.e. Node was started with `--enable-source-maps`). Otherwise it fell back to store-only mode, which populates SourcemapMap for `@swc-node/sourcemap-support` to rewrite `Error.stack`, but emits no inline `//# sourceMappingURL=`. The V8 inspector reads that inline map off the running code to bind breakpoints, and it does so regardless of `--enable-source-maps`. With no inline map, debuggers (VS Code, `node --inspect`) stopped binding breakpoints or landed on the wrong line. This is the 1.12.0 regression in #1059. Auto mode now always inlines the map, and additionally stores it only when native source maps are off (so stack traces stay correct without retaining a duplicate map when Node already handles them). This restores the 1.11.1 behavior of always emitting an inline map. The `inline`/`store`/`none` env modes are unchanged. Closes #1059 Claude-Session: https://claude.ai/code/session_01LXSDomd3mGu4SL4TJ9q6T2 Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent eec0e8c commit 986094d

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

packages/register/__test__/register-runtime-tuning.spec.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,37 @@ test.serial('supports sourcemap store-only mode to avoid inline map payload', (t
124124
t.true(SourcemapMap.has(filename))
125125
})
126126

127+
test.serial('auto source map mode inlines the map so debuggers can bind breakpoints', (t) => {
128+
// Regression guard for https://github.com/swc-project/swc-node/issues/1059.
129+
// In auto mode (no SWC_NODE_SOURCE_MAP_MODE) the emitted code must carry an
130+
// inline sourceMappingURL even when process.sourceMapsEnabled is false. The V8
131+
// inspector reads that inline map to bind breakpoints, and it does so
132+
// regardless of --enable-source-maps. Dropping the inline map here is what
133+
// broke the debugger in 1.12.0.
134+
delete process.env.SWC_NODE_SOURCE_MAP_MODE
135+
136+
const previousSourceMaps = process.sourceMapsEnabled
137+
process.setSourceMapsEnabled(false)
138+
t.teardown(() => process.setSourceMapsEnabled(previousSourceMaps))
139+
140+
sinon.stub(swcCore, 'transformSync').returns({
141+
code: 'console.log("auto-inline")',
142+
map: emptyMap,
143+
})
144+
145+
const filename = uniquePath('sourcemap-auto', 'ts')
146+
const output = compile('const x = 1', filename, {
147+
module: ts.ModuleKind.CommonJS,
148+
sourceMap: true,
149+
})
150+
151+
// Inline map present -> the inspector can map breakpoints back to source.
152+
t.true(output.includes('sourceMappingURL'))
153+
// Stored map present -> sourcemap-support keeps Error.stack correct when native
154+
// source maps are off.
155+
t.true(SourcemapMap.has(filename))
156+
})
157+
127158
test.serial('skips transform for plain js in commonjs mode', (t) => {
128159
const transformSyncStub = sinon.stub(swcCore, 'transformSync')
129160

packages/register/read-default-tsconfig.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,17 @@ export function getSourceMapMode(): { inline: boolean; store: boolean } {
2828
return { inline: false, store: false }
2929
}
3030

31-
// In auto mode, follow runtime capability: native source maps favor inline,
32-
// non-native stacks favor store mode.
33-
return process.sourceMapsEnabled ? { inline: true, store: false } : { inline: false, store: true }
31+
// In auto mode, always inline the map: the V8 inspector reads the inline
32+
// sourceMappingURL off the running code to bind breakpoints, and it does so
33+
// regardless of `process.sourceMapsEnabled` (which only governs how Node
34+
// rewrites Error.stack). Gating inline emission on that flag broke debuggers
35+
// in 1.12.0 (https://github.com/swc-project/swc-node/issues/1059).
36+
//
37+
// Additionally store the map when native source maps are off so
38+
// @swc-node/sourcemap-support keeps Error.stack line numbers correct; when
39+
// native maps are on, Node handles stack traces and storing would just retain
40+
// a duplicate map.
41+
return process.sourceMapsEnabled ? { inline: true, store: false } : { inline: true, store: true }
3442
}
3543

3644
export function readDefaultTsConfig(

0 commit comments

Comments
 (0)