Skip to content

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

Open
maruthang wants to merge 1 commit into
angular: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 into
angular:mainfrom
maruthang:fix-28904-error-message

Conversation

@maruthang

@maruthang maruthang commented Mar 27, 2026

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.

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:

  • "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

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

Copy link
Copy Markdown

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 thread packages/angular/build/src/builders/application/execute-build.ts
Comment on lines +170 to +182
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 ?? []),
];

@alan-agius4 alan-agius4 Apr 9, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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,
},
)

Comment on lines +56 to +63
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();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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'`),
}),
);

@maruthang

Copy link
Copy Markdown
Contributor Author

Hi @alan-agius4, thank you for the review feedback! I've addressed both suggestions in the latest commit:

  1. Switched to error.notes ??= [] with .push() and merged the two notes into a single one with a newline separator.
  2. Updated the test to use expect(result?.success).toBeTrue() with expect(logs).not.toContain(...) as you suggested.

Please let me know if there's anything else you'd like me to adjust.

@alan-agius4 alan-agius4 added target: patch This PR is targeted for the next patch release action: merge The PR is ready for merge by the caretaker action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: merge The PR is ready for merge by the caretaker labels Apr 28, 2026
…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
@maruthang maruthang force-pushed the fix-28904-error-message branch from 6c1a652 to 54fc33f Compare July 6, 2026 08:39
@angular-robot angular-robot Bot requested a review from alan-agius4 July 6, 2026 08:39
@maruthang

Copy link
Copy Markdown
Contributor Author

Squashed and rebased onto main: the style: commit type was failing commit-message lint, and the no-Zone.js test needed an export in the generated main.ts (top-level await is only valid in modules, which is why the build failed even without Zone.js). Verified both specs pass locally against the rebased builder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews area: @angular/build target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error message still is wrong

2 participants