Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deletes babel code, leading to broken while loops #4147

Closed
mitchellwarr opened this issue Jun 24, 2021 · 4 comments · Fixed by #4149
Closed

Deletes babel code, leading to broken while loops #4147

mitchellwarr opened this issue Jun 24, 2021 · 4 comments · Fixed by #4149

Comments

@mitchellwarr
Copy link

mitchellwarr commented Jun 24, 2021

  • Rollup Version: 2.52.2
  • Operating System (or Browser): Mac
  • Node Version (if applicable): v12.19.0
  • Link to reproduction: reproduction

Expected Behavior

The main code should remain the same between both pieces

Actual Behavior

Lines 26:29 of the input babel transpiled code gets removed

if (!x.looping) {
  _context.next = 7;
  break;
}

This might be due to dead code removal, but this isnt a dead end branch! In fact its quite important to stop an infinite loop!

image

The untranspiled code

import { useEffect, useRef } from 'react';

const interval = (f, delay) => {
  let x = {
    looping: true,
    abort() {
      this.looping = false;
    }
  };
  let loop = async function() {
    while (x.looping) {
      await f();
      await new Promise(resolve => setTimeout(resolve, delay));
    }
  };
  loop();
  return x;
};

const useInterval = (callback, delay) => {
  const savedCallback = useRef();
  savedCallback.current = callback;

  useEffect(
    () => {
      const tick = () => savedCallback.current();

      if (delay !== null && delay !== undefined) {
        let clear = interval(tick, delay);
        return () => {
          clear.abort();
        };
      }
    },
    [delay]
  );
};

export default useInterval;

Workaround

Here is a slight change that makes it work

Essentially it adds this one line

var x = {
  looping: true,
  abort: function abort() {
    this.looping = false;
  }
};
x.looping = true; // This line

After that, it all works

@lukastaegert
Copy link
Member

Thanks for posting, this looks like a serious bug. One thing I can tell you so far: The problem where Rollup is struggling is actually not the Babel code, that one just avoids some necessary deoptimizations Rollup has in place currently for async functions. It is this line:

this.looping = false;

If you replace it with

x.looping = false;

then it works, i.e. Rollup fails to acknowledge that this is likely x or otherwise x needs to be deoptimized in some way. I will have a look, but at least this should give you a better workaround for now.

@lukastaegert
Copy link
Member

It seems to be something really subtle. For my own and other's reference, this is as much as I could boil down the reproduction for now:

https://rollupjs.org/repl/?version=2.52.2&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmltcG9ydCUyMGRvU3R1ZmYlMjBmcm9tJTIwJ2V4dGVybmFsJyUzQiU1Q24lNUNudmFyJTIwaW50ZXJ2YWwlMjAlM0QlMjAoKSUyMCUzRCUzRSUyMCU3QiU1Q24lMjAlMjB2YXIlMjB4JTIwJTNEJTIwJTdCJTVDbiUyMCUyMCUyMCUyMGxvb3BpbmclM0ElMjB0cnVlJTJDJTVDbiUyMCUyMCUyMCUyMGFib3J0JTNBJTIwZnVuY3Rpb24lMjBhYm9ydCgpJTIwJTdCJTVDbiUyMCUyMCUyMCUyMCUyMCUyMHRoaXMubG9vcGluZyUyMCUzRCUyMGZhbHNlJTNCJTVDbiUyMCUyMCUyMCUyMCU3RCU1Q24lMjAlMjAlN0QlM0IlNUNuJTVDdCU1Q24lNUN0ZG9TdHVmZigoKSUyMCUzRCUzRSUyMCU3QiU1Q24lNUN0JTVDdCUyRiUyRiUyMFRoaXMlMjBuZWVkcyUyMHRvJTIwYmUlMjByZXRhaW5lZCU1Q24lNUN0JTIwJTIwaWYlMjAoIXgubG9vcGluZyklMjAlN0IlNUNuJTIwJTIwJTIwJTIwJTIwJTIwcmV0dXJuJTIwZmFsc2UlM0IlNUNuJTIwJTIwJTIwJTIwJTdEJTVDbiUyMCUyMCUyMCUyMHJldHVybiUyMHRydWUlM0IlNUNuJTIwJTIwJTdEKSU1Q24lNUNuJTIwJTIwcmV0dXJuJTIweCUzQiU1Q24lN0QlM0IlNUNuJTVDbmV4cG9ydCUyMGRlZmF1bHQlMjAoKSUyMCUzRCUzRSUyMCU3QiU1Q24lNUN0JTJGJTJGJTIwSW5saW5pbmclMjB0aGUlMjBpZiUyMHNlZW1zJTIwdG8lMjBmaXglMjBpdCU1Q24lMjAlMjBpZiUyMCh0cnVlKSUyMCU3QiU1Q24lNUN0JTVDdCUyRiUyRiUyMFVzaW5nJTIwY29uc3QlMjBzZWVtcyUyMHRvJTIwZml4JTIwaXQlNUNuJTIwJTIwJTIwJTIwdmFyJTIwY2xlYXIlMjAlM0QlMjBpbnRlcnZhbCgpJTNCJTVDbiUyMCUyMCUyMCUyMHJldHVybiUyMCgpJTIwJTNEJTNFJTIwY2xlYXIuYWJvcnQoKSUzQiU1Q24lMjAlMjAlN0QlNUNuJTdEJTNCJTVDbiUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=

@lukastaegert
Copy link
Member

Turns out the actual issue was very different from what I originally thought. Fix at #4149

@mitchellwarr
Copy link
Author

mitchellwarr commented Jun 25, 2021

Thank you for the quick fix! @lukastaegert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants