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

When Babel cache gets too big, an exception "TypeError: Method Uint8Array.length called on incompatible receiver" is thrown from fs.writeFileSync #5259

Closed
sompylasar opened this issue Feb 3, 2017 · 3 comments
Labels
Has PR i: bug i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@sompylasar
Copy link

This issue: #2678 (Babel cache errors when it gets too big)
was improperly fixed via this commit: 8ffc701
which worked around the original error but introduced a different error:

TypeError: Method Uint8Array.length called on incompatible receiver [object Object]
    at Buffer.get length (native)
    at Object.fs.writeFileSync (fs.js:1161:20)
    at save (/__CENSORED__/node_modules/babel-register/lib/cache.js:45:16)
    at nextTickCallbackWith0Args (node.js:436:9)
    at process._tickDomainCallback (node.js:406:13)

which gets thrown from fs.writeFileSync: https://github.com/nodejs/node/blob/v4.7.0/lib/fs.js#L1161

The error is caused by erroneously passing an Object ({}) instead of a String, Buffer or UInt8Array into fs.writeFileSync. https://nodejs.org/api/fs.html#fs_fs_writefilesync_file_data_options

The error as of now is in the latest master of babel/packages/babel-register:

fs.writeFileSync(FILENAME, serialised);

/**
 * Write stringified cache to disk.
 */

export function save() {
  let serialised = {};  //<<<<< `serialized` is initialized with a non-serialized `{}`, i.e. `Object`.
  try {
    serialised = JSON.stringify(data, null, "  ");
  } catch (err) {
    if (err.message === "Invalid string length") {
      err.message = "Cache too large so it's been cleared.";
      console.error(err.stack);
      //<<<<< No control flow interruption happens here, fall through, `serialized === {}`.
    } else {
      throw err;
    }
  }
  mkdirpSync(path.dirname(FILENAME));
  fs.writeFileSync(FILENAME, serialised);  //<<<<< `{}` in `serialized` got to here.
}

Proposed change:

  export function save() {
-   let serialised = {};
+   let serialised = "{}";
@babel-bot
Copy link
Collaborator

Hey @sompylasar! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

@xtuc
Copy link
Member

xtuc commented Feb 3, 2017

Good catch 👍

This would have catch using Flow annotations I think. I will make a PR for that.

@xtuc
Copy link
Member

xtuc commented Feb 8, 2017

Closed in #5260. Feel free to reopen this issue.

@xtuc xtuc closed this as completed Feb 8, 2017
@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label May 5, 2018
@lock lock bot locked as resolved and limited conversation to collaborators May 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Has PR i: bug i: enhancement outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

3 participants