Skip to content

[Bug]: Babel breaks on Source Maps merge when adding multi-line block comments through babel-core NodePath.addComment API #15363

@ernestostifano

Description

@ernestostifano

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

Minimal Reproducible Example

https://github.com/ernestostifano/babel-comments-issue

Configuration file name

babel.config.json

Configuration

{
    "plugins": ["./babel-plugin-custom.js"]
}

Current and expected behavior

Steps to Reproduce

  1. Clone Minimal Reproducible Example.
  2. Run npm install.
  3. See available scripts in package.json and run them as needed. (EX: npm run succeeds1 or npm run fails1).

NOTE: There are several scripts in the package.json. All cases combining source maps and comments addition in both builds (case scripts) and then failed and succeed cases (fails and succeeds scripts).

Description

It seems that there is an issue when using function babel-core.traverse.NodePath.prototype.addComment to add multi-line comments (by a Babel plugin) and source maps merge.

From tests, it seems that it is enough to trigger an initial build with source maps enabled (without necessarily adding any comment through a plugin) and then a second build (on the output of the first one) that tries to add a multi-line comment and also has source maps enabled (in fact, Babel seems ti break on source map merge step, see error below). (See case17 script.)

Please, also note:

  • If the second build tries to add a single-line comment with source maps enabled, it works.
  • If the second build tries to add a multi-line comment with source maps disabled, it works.
  • So it is a combination of source maps on first build and multi-line comments + source maps on second build.
TypeError: /path/to/repo/babel-comments-issue/build1/index.js: Cannot read properties of undefined (reading 'length')
    at memoizedBinarySearch (/path/to/repo/babel-comments-issue/node_modules/@jridgewell/trace-mapping/dist/trace-mapping.umd.js:138:29)
    at traceSegmentInternal (/path/to/repo/babel-comments-issue/node_modules/@jridgewell/trace-mapping/dist/trace-mapping.umd.js:520:21)
    at Object.exports.traceSegment (/path/to/repo/babel-comments-issue/node_modules/@jridgewell/trace-mapping/dist/trace-mapping.umd.js:394:27)
    at originalPositionFor (/path/to/repo/babel-comments-issue/node_modules/@ampproject/remapping/dist/remapping.umd.js:90:38)
    at traceMappings (/path/to/repo/babel-comments-issue/node_modules/@ampproject/remapping/dist/remapping.umd.js:59:30)
    at remapping (/path/to/repo/babel-comments-issue/node_modules/@ampproject/remapping/dist/remapping.umd.js:203:30)
    at mergeSourceMap (/path/to/repo/babel-comments-issue/node_modules/@babel/core/lib/transformation/file/merge-map.js:17:30)
    at generateCode (/path/to/repo/babel-comments-issue/node_modules/@babel/core/lib/transformation/file/generate.js:61:41)
    at run (/path/to/repo/babel-comments-issue/node_modules/@babel/core/lib/transformation/index.js:39:33)
    at run.next (<anonymous>) {
  code: 'BABEL_GENERATE_ERROR'
}

Environment

System:
OS: macOS 12.6.2
Binaries:
Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
Yarn: 3.2.3 - ~/.nvm/versions/node/v16.15.0/bin/yarn
npm: 8.10.0 - ~/.nvm/versions/node/v16.15.0/bin/npm

Possible solution

N/A.

Additional context

Custom Plugin Code

./babel-plugin-custom.js

const multi = `
line1
line2
`;

const single = 'line1';

module.exports = function babelPluginCustom() {
    const isSingle = process.env.NPM_CONFIG_SINGLE === 'true';
    return {
        visitor: {
            Program(path) {
                path.addComment('leading', isSingle ? single : multi);
            }
        }
    };
};

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions