facebookarchive / prepack

A JavaScript bundle optimizer.
http://prepack.io
Other
14.22k stars 425 forks source link

Invariant at Emitter.dependenciesVisitor #1846

Open gaearon opened 6 years ago

gaearon commented 6 years ago

Repro:

require("react");

__evaluatePureFunction(function() {
  var React = require("react");

  function foo(i) {
    var x = {};
    if (!Array.isArray(i)) {
      x = x[i];
    }
    if (Array.isArray(x)) {
      throw new Error("foo");
    }
  }

  function getFoo(props) {
    if (props.no) {
      throw new Error("no");
    }
    return foo(props.count === 1 ? [] : []);
  }

  function App(props) {
    return React.createElement(
      "div",
      props.foo({}),
      React.createElement("div", {
        "data-foo": [getFoo(props)]
      })
    );
  }

  __optimizeReactComponentTree(App, {
    firstRenderOnly: true
  });

  module.exports = App;
});

Invariant:

https://github.com/facebook/prepack/blob/41817e59150cc5ae99a6cdeaced2ee91df56084a/src/serializer/Emitter.js#L322

This is likely a bug in Prepack, not your code. Feel free to open an issue on GitHub.
    at invariant (/Users/gaearon/p/prepack/src/invariant.js:16:15)
    at Emitter.dependenciesVisitor (/Users/gaearon/p/prepack/src/serializer/Emitter.js:322:19)
    at recurse (/Users/gaearon/p/prepack/src/serializer/Emitter.js:278:33)
    at Emitter.dependenciesVisitor (/Users/gaearon/p/prepack/src/serializer/Emitter.js:283:18)
    at Emitter.getReasonToWaitForDependencies (/Users/gaearon/p/prepack/src/serializer/Emitter.js:267:17)
    at Emitter.emitNowOrAfterWaitingForDependencies (/Users/gaearon/p/prepack/src/serializer/Emitter.js:434:32)
    at Object.emitPropertyModification (/Users/gaearon/p/prepack/src/serializer/ResidualHeapSerializer.js:1864:24)
    at ModifiedPropertyEntry.serialize (/Users/gaearon/p/prepack/src/utils/generator.js:189:13)
    at serializeFn (/Users/gaearon/p/prepack/src/utils/generator.js:1085:46)
    at evaluateForEffectsInGlobalEnv (/Users/gaearon/p/prepack/src/realm.js:711:18)
NTillmann commented 6 years ago

Once #1850 lands, this crash degenerates to

'_K' is not defined. (96:25)

Where _K is defined in an optimized function, and referenced by

var $f_0 = function () {
    _I = <div data-foo={_K} />;
  };

This may be very react specific.

NTillmann commented 6 years ago

I am getting this on master now:

Failed to render React component root "App" due to side-effects from throwing exception at location: 18:16 - 18:18

- App (fatal: Failed to render React component root "App" due to side-effects from throwing exception at location: 18:16 - 18:18)
gaearon commented 6 years ago

Yeah, we disabled throwing in React to work around the bugs. I think you should be able to temporarily re-enable it by commenting out this line:

https://github.com/facebook/prepack/blob/aa700a2667f5c28754ae2dad2a64a3ad4191276a/src/realm.js#L648