fix(@angular/build): correct misleading error message for top-level await#32887
Open
maruthang wants to merge 1 commit intoangular:mainfrom
Open
fix(@angular/build): correct misleading error message for top-level await#32887maruthang wants to merge 1 commit intoangular:mainfrom
maruthang wants to merge 1 commit intoangular:mainfrom
Conversation
…wait When using top-level await in a Zone.js application, esbuild would show an error mentioning "target environment" with browser versions, which is misleading. The actual reason is that async/await is downleveled for Zone.js compatibility and top-level await cannot be downleveled. This change augments the esbuild error with a note explaining that top-level await is not supported in applications that use Zone.js, and provides a link to the zoneless Angular documentation. Fixes angular#28904
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.
Comment on lines
+163
to
+185
| if (!isZonelessApp(options.polyfills)) { | ||
| for (const error of bundlingResult.errors) { | ||
| if ( | ||
| error.text?.startsWith( | ||
| 'Top-level await is not available in the configured target environment', | ||
| ) | ||
| ) { | ||
| 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.
To improve maintainability and readability, it's a good practice to extract magic strings into constants. The error message from esbuild could change in the future, and having it as a constant makes it easier to update.
if (!isZonelessApp(options.polyfills)) {
const TLA_ERROR_MESSAGE =
'Top-level await is not available in the configured target environment';
for (const error of bundlingResult.errors) {
if (error.text?.startsWith(TLA_ERROR_MESSAGE)) {
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 ?? []),
];
}
}
}
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Issue Number: #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?