eslint: Uncaught exception when cache file is empty (i.e. invalid JSON)

Tell us about your environment

  • ESLint Version: 3.10.2
  • Node Version: 6.9.1

What parser (default, Babel-ESLint, etc.) are you using? default

Please show your full configuration:

standard 8.6.0

What did you do? Please include the actual source code causing the issue.

Reposting this issue:

Suddenly standard started giving the attached below output. After spending some time searching through my code and reducing it to 0 files I was still getting the same error. Later I’ve found that there is no try/catch trying to parse back the JSON from the cache files found at .standard-cache, so when the file is empty it crashes. The problem seems to be at flat-cache package or its usage, but I thought you may know more about it. I’ll try to take a deeper look, anyway.

What do you think about trying to delete cache files when this kind of error occurs?

Hope I can help in any manner 😃

So, basically the cache file parsing probably needs a try-catch. In case it’s relevant, here’s the place where we set the cache options: https://github.com/Flet/standard-engine/blob/b579ea143c8e64254caebbd276c1d67b16793458/index.js#L35-L36

What did you expect to happen?

No uncaught exceptions.

What actually happened? Please include the actual, raw output from ESLint.

SyntaxError: Unexpected end of JSON input
    at Object.parse (native)
    at Object.parseRecursion [as parse] (.../node_modules/circular-json/build/circular-json.node.js:182:15)
    at module.exports.readJSON (.../node_modules/flat-cache/utils.js:15:25)
    at Object.cache.load (.../node_modules/flat-cache/cache.js:25:23)
    at Object.module.exports.create (.../node_modules/flat-cache/cache.js:158:9)
    at Object.module.exports.load (.../node_modules/flat-cache/cache.js:144:17)
    at Object.module.exports.create (.../node_modules/file-entry-cache/cache.js:13:27)
    at new CLIEngine (.../node_modules/eslint/lib/cli-engine.js:463:38)
    at .../node_modules/standard-engine/index.js:107:16
    at .../node_modules/deglob/index.js:51:12

cc @alvaropinot @VzqzAc

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 15 (13 by maintainers)

Commits related to this issue

Most upvoted comments

Right now, an uncaught exception is thrown (and the node process terminates) when the cache file is invalid. As a result, the same error occurs the next time ESLint is run, as @not-an-aardvark correctly pointed out.

The issue is in flat-cache on this line. They call a function that can throw exceptions but do not have a try-catch block to handle the case where the string is invalid JSON.

I opened an issue on flat-cache to get the author’s thoughts: https://github.com/royriojas/flat-cache/issues/12

IMO, the correct behavior is to invalidate the cache if it’s unusable.

Yes, as far as I can tell, it should be safe to publish the changes in https://github.com/royriojas/flat-cache/pull/13 in a new patch version.

@royriojas I just pushed a commit to the cache-integration-tests branch on the ESLint repo. The commit has tests for a lot of ESLint’s behavior with cache files. Two tests on that commit are currently failing due to this cache-parsing issue.

When I install the commit on your branch:

npm install git://github.com/royriojas/flat-cache/#eaec2239ca1aaf7efb46b95f1b3153f70df8c8d3

…the tests pass. So as far as I can tell, your fix seems to work. 👍

The tests that fail due to the bug can be found here.

To reproduce without running the tests:

echo 'invalid JSON' > .eslintcache
eslint --cache foo.js

Is there a use case for linting with an invalid cache file?

I wouldn’t call it a use case, but it can definitely happen. I’ve seen multiple issues opened on the standard repo about this issue.

I don’t know for sure what the cause is, but this could easily happen if ESLint throws an uncaught exception or if the user kills the program with Ctrl+C in the middle of execution.

It’s probably a good idea to be robust against invalid cache files.