Problem/Motivation
We are having problems with Peast not being able to parse some of the JavaScripts generated by our theme build pipeline, causing aggregation to fail with a parsing exception (...and it's fairly slow for large files when it doesn't) in Drupal\Core\Asset\JsOptimizer.
Steps to reproduce
Aggregate any JS file which has a class with a method named `async`. See https://github.com/mck89/peast/issues/62 for a minimal example.
In our case, the file containing the class is generated by our theme building pipeline and part of the clientside runtime, so not something we can easily change.
An exception is thrown with just the parsing error message (no source file name or parsing position included) and the aggregation is aborted.
Note that if I hack around this and rename all the async methods in the file it parses correctly, but I would be hesitant to assume Peast won't choke on something else later - given build pipelines and JavaScript itself are constantly changing.
Proposed resolution
Wrap the calls to Peast in a try-catch and if the parsing or rendering fails, log the error and include the unprocessed file as before Peast was introduced.
If there was an actual JavaScript syntax error it would then be possible for a developer to more easily identify it as part of the aggregated file
Remaining tasks
Implement a fix.
User interface changes
None
API changes
None
Data model changes
None
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #17 | 3381097-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #15 | 3381097-nr-bot.txt | 90 bytes | needs-review-queue-bot |
| #13 | 3381097-test-only.diff | 1.7 KB | catch |
Issue fork drupal-3381097
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
twodWhoops, meant to create this for 11.x, but no matter, they seem to be identical.
Pushed up a simple proof of concept which catches the exception, creates an informative log entry if it can, and proceeds to use the original source to not cause a complete crash.
Other than it also needing a test, Is this a good approach?
Comment #3
b_sharpe commentedTHANK YOU for this. I was running down a big rabbit hole when JS aggregation was on trying to find out what was causing my errors and this saved me.
Comment #4
catchGood idea - can you open an MR for the branch?
To test this - do we need js with a syntax exception that Peast will definitely throw an exception for?
Comment #5
catchI opened #3382123: Update Peast to 1.15.4 to update Peast too.
Comment #7
nicxvan commentedThis seems to also resolve the case where you mistakenly don't mark a library as minified: https://www.drupal.org/project/drupal/issues/3371010
Comment #8
b_sharpe commentedRebased and Fixed issues with tests failing. Needs Review
Comment #9
catchCouple of comments on the MR but overall looking good.
Comment #10
b_sharpe commentedWent with the default system logger as there isn't any other instances of asset logging to really warrant it's own channel.
Comment #11
catchLooks great. Will mean that actually broken JavaScript won't cause any more problems than it would have done in 10.x, and if Peast has its own parsing errors on unusual/new syntax that we still get a perfectly usable aggregate, just without that particular file minimized.
Comment #12
twodLol, was just about to check in on this and it's already RTBC.
Do we need an actual test to ensure broken scripts keep making it through to the aggregated files (or at least go through this part unmodified)?
Comment #13
catchGood point yes we do.
Here's a test only patch that should fail.
Comment #14
catchComment #15
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #16
catchComment #17
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #18
catchRebased/retargeted onto 11.x
Comment #19
b_sharpe commentedLooks good!
Comment #22
catchHere's the interdiff of the test coverage I added:
Given that was the only code I touched here, I'm going to go ahead and commit it. Committed/pushed to 11.x and cherry-picked to 10.1.x, thanks!
Comment #23
catch