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

core(build): inline-fs error if file missing, ignorePaths #14436

Merged
merged 5 commits into from
Oct 13, 2022

Conversation

connorjclark
Copy link
Collaborator

@connorjclark connorjclark commented Oct 6, 2022

I noticed this when I tried to run yarn build-lr in a clean checkout, which silently messes up because there is no dist/report so the JS inlining fails by simply emitting code that tried to read the file with fs. Instead, the plugin should throw a fatal error.

In doing this, I had to skip a case the plugin came across in node_modules/puppeteer-core/lib/esm/puppeteer/common/Page.js:
image

Copy link
Member

@adamraine adamraine left a comment

Choose a reason for hiding this comment

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

Can you add some tests for this

@connorjclark connorjclark changed the title build(inline-fs): fatal error if file missing, and ignore block comments core(build): inline-fs plugin error if file missing, ignore block comments Oct 6, 2022
}
if (maybeInBlockComment) {
const err = new Error('ignoring potential match because likely inside a block comment');
const line = code.substring(0, foundIndex).split(/\r\n|\r|\n/).length;
Copy link
Member

Choose a reason for hiding this comment

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

acorn.getLineInfo(code, foundIndex) is essentially this but lets you skip the DIY part (see use on L124)

@@ -98,6 +114,11 @@ async function inlineFs(code, filepath) {
parsed.callee.property);
}
} catch (err) {
// Consider missing files to be a fatal error.
Copy link
Member

Choose a reason for hiding this comment

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

Kind of weird that this isn't issuing more fatal errors, though I think this was copying the old brfs behavior of just warning on everything, and we rely on it for fs calls that aren't resolvable but also can't be reached at runtime.

I wonder if the rule should be something like: if there's an error statically determining the path, that's a warning, but if there's an fs error when accessing the path, that's an error, but we can change that if it's ever a bigger problem.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

My intention was to only flag cases where fs.readFileSync failed, I'll double back and see how to really do that.

Copy link
Member

Choose a reason for hiding this comment

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

Only if you want to, I think this is fine, too.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Alrighty. Let's just address it if we ever hit this case.

// First iterate backwards until a newline to determine if we are in a block comment.
let maybeInBlockComment = false;
for (let i = foundIndex; i > 0; i--) {
if (code[i] === '*') {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm just realizing this is horribly tailored to the one case I found. Doesn't catch all block comments (don't need an asterisk on every line...), and trips up on multiplication. But how can we do something similar w/o parsing the AST?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could drop trying to detect if inside a comment, go back to this being a warning, and iterate all warnings and throw on any ENOENT that isn't one we expect.

Copy link
Member

Choose a reason for hiding this comment

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

This entire file seems tailored to our use cases. If we aren't handling one of these cases properly, will this plugin fail loudly?

Copy link
Collaborator Author

@connorjclark connorjclark Oct 6, 2022

Choose a reason for hiding this comment

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

Yes, it'd be throwing an error and failing the build step. I'd just expect this to possibly get triggered upon some random node_modules change.

@connorjclark connorjclark changed the title core(build): inline-fs plugin error if file missing, ignore block comments core(build): inline-fs plugin error if file missing, ignorePaths Oct 13, 2022
@connorjclark connorjclark changed the title core(build): inline-fs plugin error if file missing, ignorePaths core(build): inline-fs error if file missing, ignorePaths Oct 13, 2022
@connorjclark connorjclark merged commit 95e3993 into main Oct 13, 2022
@connorjclark connorjclark deleted the inline-fatal-errors branch October 13, 2022 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants