fix(@angular/build): correct misleading error message for top-level await#32887
fix(@angular/build): correct misleading error message for top-level await#32887maruthang wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request enhances the Angular build process by providing a more descriptive error message when top-level await is used in applications that still utilize Zone.js. The build logic now intercepts specific esbuild errors to add helpful notes and documentation links, and new behavior tests have been added to verify these changes. A review comment suggests extracting the hardcoded error string into a constant to improve maintainability.
| error.notes = [ | ||
| { | ||
| text: | ||
| 'Top-level await is not supported in applications that use Zone.js. ' + | ||
| 'Consider removing Zone.js or moving this code into an async function.', | ||
| location: null, | ||
| }, | ||
| { | ||
| text: 'For more information about zoneless Angular applications, visit: https://angular.dev/guide/zoneless', | ||
| location: null, | ||
| }, | ||
| ...(error.notes ?? []), | ||
| ]; |
There was a problem hiding this comment.
| error.notes = [ | |
| { | |
| text: | |
| 'Top-level await is not supported in applications that use Zone.js. ' + | |
| 'Consider removing Zone.js or moving this code into an async function.', | |
| location: null, | |
| }, | |
| { | |
| text: 'For more information about zoneless Angular applications, visit: https://angular.dev/guide/zoneless', | |
| location: null, | |
| }, | |
| ...(error.notes ?? []), | |
| ]; | |
| error.notes ??= []; | |
| error.notes.push( | |
| { | |
| text: | |
| 'Top-level await is not supported in applications that use Zone.js. ' + | |
| 'Consider removing Zone.js or moving this code into an async function. \n' + , | |
| 'For more information about zoneless Angular applications, visit: https://angular.dev/guide/zoneless' | |
| location: null, | |
| }, | |
| ) |
| const zoneJsErrorPresent = logs.some( | ||
| (log) => | ||
| typeof log.message === 'string' && | ||
| log.message.includes( | ||
| 'Top-level await is not supported in applications that use Zone.js', | ||
| ), | ||
| ); | ||
| expect(zoneJsErrorPresent).toBeFalse(); |
There was a problem hiding this comment.
| const zoneJsErrorPresent = logs.some( | |
| (log) => | |
| typeof log.message === 'string' && | |
| log.message.includes( | |
| 'Top-level await is not supported in applications that use Zone.js', | |
| ), | |
| ); | |
| expect(zoneJsErrorPresent).toBeFalse(); | |
| expect(result?.success).toBeTrue(); | |
| expect(logs).not.toContain( | |
| jasmine.objectContaining({ | |
| level: 'error', | |
| message: jasmine.stringContaining(`Top-level await is not supported in applications that use Zone.js'`), | |
| }), | |
| ); |
|
Hi @alan-agius4, thank you for the review feedback! I've addressed both suggestions in the latest commit:
Please let me know if there's anything else you'd like me to adjust. |
…wait When top-level await is used in an application that includes Zone.js, esbuild reports that top-level await is not available in the configured target environment even though the actual cause is the async/await downleveling required for Zone.js support. The error is now augmented with a note explaining the Zone.js limitation and pointing to the zoneless guide. Closes angular#28904
6c1a652 to
54fc33f
Compare
|
Squashed and rebased onto main: the |
PR Checklist
PR Type
What is the current behavior?
When using top-level
awaitin an Angular app with Zone.js, esbuild reports "Top-level await is not available in the configured target environment" with a list of browser versions. This is misleading — all modern browsers support top-level await. The real issue is that Angular disables native async/await when Zone.js is in use (since Zone.js can't intercept it), and top-level await can't be downleveled.Closes #28904
What is the new behavior?
When the top-level await error occurs and Zone.js is active, the error is augmented with explanatory notes:
Does this PR introduce a breaking change?