mozilla / mozilla/source-map

Error creating a SourceMapGenerator from a SourceMapConsumer of an index map (part 2)

Open
#420 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

needs-triage
Dominant language
JavaScript
Stars
3.7k
Forks
370
PR merge metrics
No merged PRs in 30d

Description

;(async () => {
  let mozilla = require('source-map')
  let map;

  map = {
    version: 3,
    sections: [
      {
        offset: { line: 0, column: 0 },
        map: {
          version: 3,
          sources: ['123', '456'],
          sourcesContent: ['a\nb\nc\n', ''],
          mappings: 'AAAA;AACA;AACA;'
        }
      }
    ]
  }

  // this map works for some reason
  /*map = {
    version: 3,
    sources: ['123', '456'],
    sourcesContent: ['a\nb\nc\n', ''],
    mappings: 'AAAA;AACA;AACA;'
  }*/

  let consumer = await new mozilla.SourceMapConsumer(map)
  consumer.sourceRoot = null; // https://github.com/mozilla/source-map/issues/345
  let generator = mozilla.SourceMapGenerator.fromSourceMap(consumer)
  console.log(generator.toJSON())

})().catch(console.log)

This errors out:

Error: "456" is not in the SourceMap.
    at IndexedSourceMapConsumer.sourceContentFor (/tmp/node_modules/source-map/lib/source-map-consumer.js:840:11)
    at /tmp/node_modules/source-map/lib/source-map-generator.js:82:42
    at Array.forEach (<anonymous>)
    at Function.fromSourceMap (/tmp/node_modules/source-map/lib/source-map-generator.js:72:32)
    at /tmp/123/test.js:29:46

The issue is in here somewhere:

https://github.com/mozilla/source-map/blob/7a263408de1b60e60b6220ca3e36d9b3e4aa047d/lib/source-map-consumer.js#L880

Content is empty - '' - but it is still in the sourcemap. I believe it should be if (content == null) { check, i.e. null or undefined, letting empty string pass along.

this may be related to https://github.com/mozilla/source-map/issues/345, so tagging it just in case

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/source-map-consumer.js around the sourceContentFor implementation and trace how lib/source-map-generator.js calls it from fromSourceMap. Run the provided index-map reproducer and confirm that an empty sourcesContent entry is preserved rather than treated as missing, without changing the behavior for null or undefined content.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.