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
31 changes: 31 additions & 0 deletions packages/register/__test__/register-runtime-tuning.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ test.serial('supports sourcemap store-only mode to avoid inline map payload', (t
t.true(SourcemapMap.has(filename))
})

test.serial('auto source map mode inlines the map so debuggers can bind breakpoints', (t) => {
// Regression guard for https://github.com/swc-project/swc-node/issues/1059.
// In auto mode (no SWC_NODE_SOURCE_MAP_MODE) the emitted code must carry an
// inline sourceMappingURL even when process.sourceMapsEnabled is false. The V8
// inspector reads that inline map to bind breakpoints, and it does so
// regardless of --enable-source-maps. Dropping the inline map here is what
// broke the debugger in 1.12.0.
delete process.env.SWC_NODE_SOURCE_MAP_MODE

const previousSourceMaps = process.sourceMapsEnabled
process.setSourceMapsEnabled(false)
t.teardown(() => process.setSourceMapsEnabled(previousSourceMaps))

sinon.stub(swcCore, 'transformSync').returns({
code: 'console.log("auto-inline")',
map: emptyMap,
})

const filename = uniquePath('sourcemap-auto', 'ts')
const output = compile('const x = 1', filename, {
module: ts.ModuleKind.CommonJS,
sourceMap: true,
})

// Inline map present -> the inspector can map breakpoints back to source.
t.true(output.includes('sourceMappingURL'))
// Stored map present -> sourcemap-support keeps Error.stack correct when native
// source maps are off.
t.true(SourcemapMap.has(filename))
})

test.serial('skips transform for plain js in commonjs mode', (t) => {
const transformSyncStub = sinon.stub(swcCore, 'transformSync')

Expand Down
14 changes: 11 additions & 3 deletions packages/register/read-default-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,17 @@ export function getSourceMapMode(): { inline: boolean; store: boolean } {
return { inline: false, store: false }
}

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

export function readDefaultTsConfig(
Expand Down