Skip to content

fix(explicit-resource-management): keep await using in for-of heads - #18628

Merged
nicolo-ribaudo merged 1 commit into
babel:mainfrom
joellemd:fix-for-await-using-of
Nov 16, 2026
Merged

nicolo-ribaudo merged 1 commit into
babel:mainfrom
joellemd:fix-for-await-using-of

Conversation

@joellemd

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues? Fixes #18627
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
Need backport? No
License MIT

for (await using x of y) was transformed as if it were for (using x of y):

// input
for (await using x of y) doSomethingWith(x);

// before
for (const _x of y) try {
  var _usingCtx = babelHelpers.usingCtx();
  const x = _usingCtx.u(_x);
  doSomethingWith(x);
} catch (_) {
  _usingCtx.e = _;
} finally {
  _usingCtx.d();
}

// after
for (const _x of y) try {
  var _usingCtx = babelHelpers.usingCtx();
  const x = _usingCtx.a(_x);
  doSomethingWith(x);
} catch (_) {
  _usingCtx.e = _;
} finally {
  await _usingCtx.d();
}

The ForOfStatement visitor moves the head declaration into the loop body, but it recreated it with a hardcoded "using" kind, so "await using" was lost. As a result, resources with only Symbol.asyncDispose threw Object is not disposable., and async disposal wasn't awaited before the next iteration.

This PR keeps the original kind. for await (await using x of y) had the same problem. Plain using in for-of heads is unchanged.

@babel-bot

Copy link
Copy Markdown
Collaborator

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/63317

@pkg-pr-new

pkg-pr-new Bot commented Nov 15, 2026

Copy link
Copy Markdown

Open in StackBlitz

commit: 3aba41c

@JLHwung JLHwung left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks.

@nicolo-ribaudo nicolo-ribaudo added PR: Bug Fix 🐛 A type of pull request used for our changelog categories 7.x: needs backport labels Nov 16, 2026

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 16, 2026
Merged via the queue into babel:main with commit c8ac91c Nov 16, 2026
61 checks passed
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18631.

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

Labels

PR: Bug Fix 🐛 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: for (await using x of y) is transformed as using, so async disposal never happens

4 participants