question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CRA's sourcemaps don't match code in node_modules, which breaks IDE debugging

See original GitHub issue

React source code inside sourcemaps generated by create-react-app adds many extra blank lines and extra indentation compared with React’s node_modules source. Because line and column numbers don’t match between source maps and actual source, debuggers like VSCode don’t accurately set breakpoints inside React source and can’t accurately step into React source. Debuggers like Chrome are not affected because they use the sitemap source (not the on-disk files) to set breakpoints.

This issue is the cause of #6044 that @raiskila filed 6 weeks ago. I created a new issue with a new repro to simplify the repro case, to remove the need to install VSCode to reproduce the problem, and to clarify that all file-based debuggers will be affected (not just VSCode).

It’s possible that the root cause of this problem might be https://github.com/webpack/webpack/issues/8302, although I don’t know enough about webpack to know for sure one way or the other. It’s also possible that the problem is caused by create-react-app’s webpack configuration. It’s also possible that the problem might be in how React itself is transforming its source code before publishing generated source files like react-dom.development.js to NPM.

Is this a bug report?

Yes

Did you try recovering your dependencies?

Yes. Well, really N/A because the repro starts with npm install -g npm@latest and npx create-react-app so there are no deps to recover!

NPM version is the latest: 6.7.0

Which terms did you search for in User Guide?

sourcemap sourcemaps “source map” “source maps” mismatch whitespace line breakpoint

Environment

Environment Info:

  System:
    OS: macOS 10.14.2
    CPU: x64 Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz
  Binaries:
    Node: 10.14.2 - /usr/local/bin/node
    npm: 6.7.0 - /usr/local/bin/npm
  Browsers:
    Chrome: 71.0.3578.98
    Firefox: 63.0.1
    Safari: 12.0.2
  npmPackages:
    react: ^16.7.0 => 16.7.0
    react-dom: ^16.7.0 => 16.7.0
    react-scripts: 2.1.3 => 2.1.3
  npmGlobalPackages:
    create-react-app: Not Found

Steps to Reproduce

1. Create a new app

npm install -g npm@latest
npx create-react-app sourcemap-bug
cd sourcemap-bug
npm start

2. Load the sourcemap and extract code from it

/** @license React v16.7.0\n * react-dom.development.js\n *\n * Copyright © Facebook, Inc. and its affiliates.\n *\n * This source code is licensed under the MIT license found in the\n * LICENSE file in the root directory of this source tree.\n */\n’use strict’;\n\nif (process.env.NODE_ENV !== "production") {\n (function () {\n ‘use strict’;\n\n var React = require(‘react’);\n\n var _assign = require(‘object-assign’);\n\n var checkPropTypes = require(‘prop-types/checkPropTypes’);\n\n var scheduler = require(‘scheduler’);\n\n var tracing = require(‘scheduler/tracing’);\n /**\n * Use invariant() to assert state which your program assumes to be true.\n *\n * Provide sprintf-style format (only %s is supported) and arguments\n * to provide information about what broke and what you were\n * expecting.\n *\n * The invariant message will be stripped in production, but the invariant\n * will remain to ensure logic does not differ in production.\n */\n\n\n var validateFormat = function validateFormat() {};\n\n {\n validateFormat = function validateFormat(format) {\n if (format === undefined) {\n throw new Error(‘invariant requires an error message argument’);\n }\n };\n }\n\n

  • 2.4 Now paste that code into an editor and replace all instances of \n with a newline and \" with a regular single quote. You should end up with code like this:
/** @license React v16.7.0
 * react-dom.development.js
 *
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
'use strict';

if (process.env.NODE_ENV !== "production") {
  (function () {
    'use strict';

    var React = require('react');

    var _assign = require('object-assign');

    var checkPropTypes = require('prop-types/checkPropTypes');

    var scheduler = require('scheduler');

    var tracing = require('scheduler/tracing');
    /**
     * Use invariant() to assert state which your program assumes to be true.
     *
     * Provide sprintf-style format (only %s is supported) and arguments
     * to provide information about what broke and what you were
     * expecting.
     *
     * The invariant message will be stripped in production, but the invariant
     * will remain to ensure logic does not differ in production.
     */


    var validateFormat = function validateFormat() {};

    {
      validateFormat = function validateFormat(format) {
        if (format === undefined) {
          throw new Error('invariant requires an error message argument');
        }
      };
    }

3. Compare the sourcemap code to actual source in node_modules/react-dom/cjs/react-dom.development.js:

/** @license React v16.7.0
 * react-dom.development.js
 *
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

'use strict';



if (process.env.NODE_ENV !== "production") {
  (function() {
'use strict';

var React = require('react');
var _assign = require('object-assign');
var checkPropTypes = require('prop-types/checkPropTypes');
var scheduler = require('scheduler');
var tracing = require('scheduler/tracing');

/**
 * Use invariant() to assert state which your program assumes to be true.
 *
 * Provide sprintf-style format (only %s is supported) and arguments
 * to provide information about what broke and what you were
 * expecting.
 *
 * The invariant message will be stripped in production, but the invariant
 * will remain to ensure logic does not differ in production.
 */

var validateFormat = function () {};

{
  validateFormat = function (format) {
    if (format === undefined) {
      throw new Error('invariant requires an error message argument');
    }
  };
}

Expected Behavior

Source is the same, which is required for VS Code’s debugger breakpoints and step-into to work properly.

Actual Behavior

The “sourcemap source” has many extra blank lines compared with the “node_modules source”. As a result of these whitespace differences, VSCode breakpoints won’t work and VSCode will set focus to the wrong code line when stepping into React’s source code.

Also, the “sourcemap source” is indented differently from the “node_modules source”. This breaks columnar breakpoints in VSCode.

Note that the Chrome debugger only uses the “sourcemap source” (and ignores the actual source files) so it’s not affected by this problem by default. However, if you use a Chrome devtools workspace to actually load the on-disk source files, it’s possible that Chrome debugging may break too.

Refer to #6044 for screenshots demonstrating the impact on VSCode users. This issue is narrowly defined to highlight the root cause of #6044 (the mismatch in whitespace between sourcemaps code and node_modules code) without requiring VSCode to reproduce the problem.

Reproducible Demo

Repro is trivial and the problem reproduces with every create-react-app project so I’m assuming that a hosted repro is not needed. If this is a bad assumption, let me know and I’ll post a repro project.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
justingrantcommented, Jul 8, 2019

Not stale. Waiting for #7022.

0reactions
stale[bot]commented, Jul 8, 2019

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in 5 days if no further activity occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Reasons Why Your Source Maps are Broken - Sentry Blog
Missing original source files​​ This likely means that your source map doesn't contain or link to your original source files. Without your ...
Read more >
Debug Typescript: Source Maps and Custom Errors - YouTube
Debugging is harder with Typescript, since the code that runs isn't the code you wrote. Learn how to make debugging easy again with...
Read more >
How do I get VSCode to load source maps for NPM ...
The debug adapter will look at those files for sourcemap references, so it can resolve breakpoints in the TS files immediately. Examples:.
Read more >
Debugging in node (typescript compiled), sourcemaps work ...
Debugging in node (typescript compiled), sourcemaps work for the main project but not any files imported from node_modules - even though they are...
Read more >
User and Workspace Settings - Visual Studio Code
If you're using a workspace that needs custom settings but you don't want to ... Controls whether the editor should be focused when...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found