Skip to content

Conversation

jsoref
Copy link

@jsoref jsoref commented Apr 26, 2024

Description

prettier wraps lstat to look for cases where a file does not exist... When it does not exist, it will try treating the argument as a glob.

For very long globs, the system will return ENAMETOOLONG instead of ENOENT. This is still a case where the file does not exist and where a glob could exist.

Instead of letting that error bubble up as an uncaught exception, swallow it in the same manner as ENOENT.

Fixes #16115.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.

Try the playground for this PR

prettier wraps lstat to look for cases where a file does not exist...
When it does not exist, it will try treating the argument as a glob.

For very long globs, the system will return ENAMETOOLONG instead of ENOENT.
This is still a case where the file does not exist and where a glob could
exist.

Instead of letting that error bubble up as an uncaught exception,
swallow it in the same manner as ENOENT.
} catch (/** @type {any} */ error) {
/* c8 ignore next 3 */
if (error.code !== "ENOENT") {
if (error.code !== "ENOENT" && error.code !== "ENAMETOOLONG") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder how it will work if it's not a glob pattern. Maybe we should not ignore it?

isDynamicPattern can be useful.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry. I never was able to reason through what to do with that...

... I naively expect people to give reasonably valid input to prettier (we certainly do). If a user constructs a path that is purely ENAMETOOLONG (as opposed to a glob -- our glob, today, matches 1000+ files and would be a bit shy of 75k bytes if i tried to put it into a command-line which would definitely cause someone to get more than a bit annoyed), then they'd never be able to do anything with it in the first place, and not prettifying a file that can't exist isn't particularly interesting to me.

If you want to add a guard for the non-glob case, you're welcome to do so. In the interim, I'm going to patch this into our action.

[error] Input length (22171), exceeds max characters (10000)

which comes from:
https://github.com/micromatch/braces/blob/e53730e6f935498326c72d768889ac194eedc0e0/lib/parse.js#L39

That doesn't impact our actual usage but I did manage to trip on it in testing (I probably have more garbage in my test repository than we do in our real world repositories which are clean).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fisker is the above sufficient to address your inquiry?

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

Successfully merging this pull request may close these issues.

ENAMETOOLONG error when running prettier
2 participants