Skip to content

Fix section offsets in generatedMappings() for indexed source maps - #1976

Merged
robhogan merged 2 commits into
react:mainfrom
kwy404:fix/sections-consumer-generated-mappings
Sep 26, 2026
Merged

robhogan merged 2 commits into
react:mainfrom
kwy404:fix/sections-consumer-generated-mappings

Conversation

@kwy404

@kwy404 kwy404 commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

Summary

For indexed (sectioned) source maps, generatedMappings() on metro-source-map's Consumer (which eachMapping() iterates) applies section offsets differently from originalPositionFor():

  • The section's column offset is added to mappings on every line of the section. It should only apply to the first line of the section, which is what originalPositionFor() already does (see the existing column offset only applies to first line of section test).
  • When a section has no mapping at its start, the unmapped segment inserted before its first mapping is placed at line 1, column 0 of the whole map instead of at the section's offset.

composeSourceMaps walks the last map with eachMapping(), so when that map is an indexed map, composed mappings can land on the wrong column. For example, with a second section at {line: 0, column: 4} that spans two lines, the composed map puts that section's second line mapping at column 4 instead of 0. Looking up line 2, column 0 in the composed map then returns no source, while the indexed map itself maps that position.

This code has not changed since the consumer was added in ccd508c. The fix applies the column offset only to mappings on the first line of a section, and places the inserted unmapped segment at the section's offset.

Changelog: [Fix] Fix generated positions when iterating the mappings of indexed source maps, e.g. in composeSourceMaps

Test plan

Added a test to packages/metro-source-map/src/__tests__/Consumer-test.js: an indexed map whose second section starts at column 4, has no mapping at its start, and spans two lines.

  • Without the fix it fails: the unmapped segment comes out at [1, 0] instead of [1, 4], and the second line mapping at [2, 4] instead of [2, 0].
  • With the fix: yarn jest packages/metro-source-map: 7 suites, 191 tests passed.
  • Full jest run on Windows with Node 24.16.0 (so scripts/jestFilter.js skips the suites it lists as broken on Windows): 125 suites passed, 1921 tests passed, 24 skipped.
  • flow check: no errors.
  • eslint and prettier --check on both changed files: clean.

SectionsConsumer#generatedMappings() added a section's column offset to
mappings on every line of the section, while originalPositionFor() only
applies it to the first line. It also emitted the unmapped segment that
precedes a section's first mapping at line 1, column 0 instead of at the
section's offset.

composeSourceMaps iterates the last map with eachMapping(), so composing
with an indexed map as the last map could put mappings on the wrong
column.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 26, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Sep 26, 2026
Composes an indexed map whose second section starts at a nonzero column and spans two lines, and checks that the composed map resolves each position the same way the indexed map does. Without the SectionsConsumer fix, both the unmapped start of the section and its second line resolve differently.

@robhogan robhogan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, tidy fix - thanks!

@robhogan
robhogan merged commit ab82c6a into react:main Sep 26, 2026
14 checks passed
kwy404 added a commit to kwy404/kwy404 that referenced this pull request Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants