Version
v24.15.0 (also reproduced on v18.15.0, v20.17.0, v22.17.0, v23.11.0)
Platform
Subsystem
source maps
What steps will reproduce the bug?
index.js:
function fail() {
throw new Error('boom');
}
fail();
//# sourceMappingURL=index.js.map
index.js.map, an index map with a single section:
{
"version": 3,
"sections": [
{
"offset": {"line": 0, "column": 0},
"map": {
"version": 3,
"sources": ["original.js"],
"names": [],
"mappings": "AAAA;AACA;AACA;AACA"
}
}
]
}
$ node --enable-source-maps index.js
How often does it reproduce? Is there a required condition?
Always, for any index map. The same mappings as a regular map (flat.js.map below) work.
What is the expected behavior? Why is that the expected behavior?
The stack trace is source mapped, as it is when the same mappings are in a regular map:
$ cat flat.js.map
{"version":3,"sources":["original.js"],"names":[],"mappings":"AAAA;AACA;AACA;AACA"}
$ node --enable-source-maps flat.js
...
at fail (/tmp/node-index-map-repro/original.js:2:1)
Index maps are part of the source map spec, and module.SourceMap already supports them: new SourceMap(payload).findEntry(1, 2) on the map above returns originalSource: 'original.js'.
What do you see instead?
The stack trace is not mapped:
$ node --enable-source-maps index.js
...
at fail (/tmp/node-index-map-repro/index.js:2:9)
module.findSourceMap() also returns undefined for index.js. With NODE_DEBUG=source_map, the map fails to load:
$ NODE_DEBUG=source_map node --enable-source-maps index.js
SOURCE_MAP 12345: TypeError: Cannot read properties of undefined (reading 'map')
Additional information
The error comes from sourcesToAbsolute() in lib/internal/source_map/source_map_cache.js, which runs data.sources.map(...) on the top-level map. Index maps have no top-level sources, so it throws, and the map isn't cached.
A possible fix is to resolve the sources of each section's map (with that map's sourceRoot) when the map has sections. Index maps appear to be tested only by constructing SourceMap directly (test/parallel/test-source-map-api.js with fixtures/source-map/disk-index.map), not through --enable-source-maps.
As a workaround, adding "sources": [] to the top level of the index map makes Node apply the sections. However, the sections' sources are then not resolved to absolute URLs, and sourceRoot is ignored, so stack traces show them verbatim (e.g. original.js:2:1 rather than an absolute path).
I ran into this in Parcel, which writes bundle source maps as index maps.
Version
v24.15.0 (also reproduced on v18.15.0, v20.17.0, v22.17.0, v23.11.0)
Platform
Subsystem
source maps
What steps will reproduce the bug?
index.js:index.js.map, an index map with a single section:{ "version": 3, "sections": [ { "offset": {"line": 0, "column": 0}, "map": { "version": 3, "sources": ["original.js"], "names": [], "mappings": "AAAA;AACA;AACA;AACA" } } ] }$ node --enable-source-maps index.jsHow often does it reproduce? Is there a required condition?
Always, for any index map. The same mappings as a regular map (
flat.js.mapbelow) work.What is the expected behavior? Why is that the expected behavior?
The stack trace is source mapped, as it is when the same mappings are in a regular map:
Index maps are part of the source map spec, and
module.SourceMapalready supports them:new SourceMap(payload).findEntry(1, 2)on the map above returnsoriginalSource: 'original.js'.What do you see instead?
The stack trace is not mapped:
module.findSourceMap()also returnsundefinedforindex.js. WithNODE_DEBUG=source_map, the map fails to load:Additional information
The error comes from
sourcesToAbsolute()inlib/internal/source_map/source_map_cache.js, which runsdata.sources.map(...)on the top-level map. Index maps have no top-levelsources, so it throws, and the map isn't cached.A possible fix is to resolve the
sourcesof each section's map (with that map'ssourceRoot) when the map hassections. Index maps appear to be tested only by constructingSourceMapdirectly (test/parallel/test-source-map-api.jswithfixtures/source-map/disk-index.map), not through--enable-source-maps.As a workaround, adding
"sources": []to the top level of the index map makes Node apply the sections. However, the sections'sourcesare then not resolved to absolute URLs, andsourceRootis ignored, so stack traces show them verbatim (e.g.original.js:2:1rather than an absolute path).I ran into this in Parcel, which writes bundle source maps as index maps.