Skip to content

fix(@angular/build): correct misleading error message for top-level await#32887

Open
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-28904-error-message
Open

fix(@angular/build): correct misleading error message for top-level await#32887
maruthang wants to merge 1 commit intoangular:mainfrom
maruthang:fix-28904-error-message

Conversation

@maruthang
Copy link
Copy Markdown
Contributor

PR Checklist

PR Type

  • Bugfix

What is the current behavior?

When using top-level await in 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:

  • "Top-level await is not supported in applications that use Zone.js. Consider removing Zone.js or moving this code into an async function."
  • A link to the Angular zoneless documentation at https://angular.dev/guide/zoneless

Does this PR introduce a breaking change?

  • Yes
  • No

…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
Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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 ?? []),
];
}
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

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 ?? []),
          ];
        }
      }
    }

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.

1 participant