Skip to content

fix: keep a declaration's name when it clashes with an external import - #11052

Merged
hyfdev merged 7 commits into
rolldown:mainfrom
Nic-Polumeyv:fix/deconflict-locals-before-externals
Nov 8, 2026
Merged

hyfdev merged 7 commits into
rolldown:mainfrom
Nic-Polumeyv:fix/deconflict-locals-before-externals

Conversation

@Nic-Polumeyv

@Nic-Polumeyv Nic-Polumeyv commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

In ESM output, when a module's top-level declaration and an external import had the same name, rolldown renamed the declaration and kept the name for the import.

This PR renames the import instead, as Rollup does.

Before:

import { AnnotationQuery } from "ext-schema";
var AnnotationQuery$1 = class extends AnnotationQuery {};
export { AnnotationQuery$1 as AnnotationQuery };

After:

import { AnnotationQuery as AnnotationQuery$1 } from "ext-schema";
var AnnotationQuery = class extends AnnotationQuery$1 {};
export { AnnotationQuery };

The renamed declaration showed up where its name is visible: the .name of a class or function, and type names in .d.ts output from rolldown-plugin-dts.

The renamer now names external import bindings after the modules' declarations, instead of together with the entry's exports.

CommonJS, IIFE and UMD output does not change.

Fixes #11035.

Since rolldown#3082 the esm external import bindings were named before the
modules' top-level symbols, so a local sharing a name with an external
import got the `$1` suffix and the import kept the name. Rollup and
esbuild do the reverse, and the entry case only looked right because
entry exports are pre-claimed. The two esbuild fixtures now match
esbuild's own expected output for those tests.

Fixes rolldown#11035
…declarations

Apply the same priority to the iife/umd/cjs external namespaces, which
lets `collect_chunk_scope_captured_names` read original names like it
already does for wrappers and drop its renamer parameter. Import
bindings read through a namespace (`ns.prop`) and `require()` results
inside a CJS closure are never declared at the chunk root, so they no
longer reserve a root name; they only ever pushed a real binding to a
suffix.
@Nic-Polumeyv
Nic-Polumeyv marked this pull request as ready for review September 30, 2026 16:55
@codspeed

codspeed Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

1 warning

⚠️ 4 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 7 untouched benchmarks
⏩ 10 skipped benchmarks1


Comparing Nic-Polumeyv:fix/deconflict-locals-before-externals (3dfc6bf) with main (0a42a68)

Open in CodSpeed

Footnotes

  1. 10 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

…e's own declarations"

This reverts commit d187503.

An ESM entry that re-exports a named import of a CommonJS module declares that import at the top level, so its name must stay reserved.
Without the reservation, `export { for } from './dep.js'` printed `var for = import_dep.for;`.
…wn#11122 fixture

Entry exports are named before the modules' declarations, and external import bindings after them.
So entry_renamed_binding_avoids_own_nested_binding re-exports `other.js`'s `join` from the entry: the entry's own `join` is then renamed and must skip the parameter `join$1`.
internal-docs/renaming/implementation.md lists the external import bindings after the declarations.
@hyfdev hyfdev self-assigned this Nov 7, 2026
@hyfdev hyfdev changed the title fix: name a chunk's own declarations before its external imports fix: keep a declaration's name when it clashes with an external import Nov 7, 2026
@netlify

netlify Bot commented Nov 7, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit beacea3
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6af085d3cb41695c5a32c245

@hyfdev

hyfdev commented Nov 7, 2026

Copy link
Copy Markdown
Member

Sorry for the wait.

I reverted the second commit, so this PR lands the first commit's fix, plus a doc and fixture update.

The second commit stops reserving top-level names for imports read through a namespace.

But an ESM entry that re-exports a CommonJS named import declares such an import at the top level.

With that commit, arbitrary-import-names.test.ts fails on top of current main with var for = import_dep.for;.

@hyfdev hyfdev 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!

hyfdev added a commit to vitejs/vite that referenced this pull request Nov 8, 2026
@hyfdev
hyfdev merged commit 7eb6c79 into rolldown:main Nov 8, 2026
34 of 35 checks passed
graphite-app Bot pushed a commit that referenced this pull request Nov 9, 2026
This PR deletes 9 test fixtures I wrote while reviewing #11052.

A merge commit I pushed to that PR added them by mistake.

The tests that #11052 added for its fix stay.
graphite-app Bot pushed a commit that referenced this pull request Nov 9, 2026
This PR deletes 9 test fixtures I wrote while reviewing #11052.

A merge commit I pushed to that PR added them by mistake.

The tests that #11052 added for its fix stay.
This was referenced Nov 9, 2026
shulaoda added a commit that referenced this pull request Nov 9, 2026
## [1.3.1] - 2026-11-09

### 🚀 Features

- inline-common-chunks: inline chunks using `import.meta.ROLLDOWN_FILE_URL_*` (#11543) by @mattlewis92

### 🐛 Bug Fixes

- cli: apply --environment before loading the binding (#11553) by @Brooooooklyn
- rolldown_plugin_vite_resolve: escape module names in generated error stubs (#11584) by @sapphi-red
- add the code location to transform logs and errors at position 0 (#11568) by @chinmayajha
- match sticky regexes only at the start of the string (#11569) by @chinmayajha
- iife/umd: skip namespace markers when the entry has no exports (#11567) by @lazerg
- keep a declaration's name when it clashes with an external import (#11052) by @Nic-Polumeyv
- drop top-level statements made dead by imported constants before chunk linking (#11053) by @elliott-with-the-longest-name-on-github
- keep `this.resolve` custom options apart for overlapping calls (#11505) by @wan9chi
- transform: reuse TsconfigCache's answer when no tsconfig owns the file (#11265) by @sarankumar1325
- treat top-level `this` of a required module without module syntax as its exports (#11271) by @sarankumar1325
- keep side effects of a module whose exported constant is inlined (#11440) by @hyfdev
- leave same-chunk dynamic imports out of `dynamicImports` (#11156) by @kkkhs

### 🚜 Refactor

- prepend transferred imports as AST fragments (#11464) by @Dunqing

### 📚 Documentation

- show `vp` first in setup commands (#11503) by @IWANABETHATGUY

### ⚡ Performance

- incremental: reuse link-time facade symbols across rebuilds (#11542) by @mattlewis92
- incremental: share module sourcemap chains across cache clones (#11540) by @mattlewis92
- chunk_optimizer: answer runtime-merge reachability from one chunk-level graph (#11541) by @mattlewis92
- send only changed chunk properties back to Rust from output hooks (#11488) by @anonrig

### 🧪 Testing

- remove review fixtures committed by mistake in #11052 (#11574) by @hyfdev
- wasi: cover raw context destruction (#11561) by @Brooooooklyn

### ⚙️ Miscellaneous Tasks

- deps: update rust crates (#11573) by @renovate[bot]
- deps: update mimalloc-safe to 0.1.70 (#11589) by @shulaoda
- renovate: update GitHub Actions that are pinned to a commit (#11583) by @hyfdev
- skip Playwright browser downloads in Prepare Release (#11579) by @shulaoda
- pin the rolldown crates to exact versions of each other on release (#11577) by @shulaoda
- update `CodSpeedHQ/action` to v5.6.3 for Ubuntu 26.04 (#11575) by @hyfdev
- remove stale entries from `.typos.toml` (#11571) by @iiio2
- pick the Prepare Release target branch from the version (#11525) by @shulaoda
- run every OS on pushes to maintenance branches (#11520) by @shulaoda
- rebase maintenance Vite branches onto their Vite minor (#11515) by @shulaoda
- run CI on pushes to maintenance branches (#11516) by @shulaoda
- support patch releases from maintenance branches (#11393) by @shulaoda

### ❤️ New Contributors

* @chinmayajha made their first contribution in [#11568](#11568)
* @lazerg made their first contribution in [#11567](#11567)
* @elliott-with-the-longest-name-on-github made their first contribution in [#11053](#11053)
* @sarankumar1325 made their first contribution in [#11265](#11265)
* @anonrig made their first contribution in [#11488](#11488)
* @kkkhs made their first contribution in [#11156](#11156)

Co-authored-by: shulaoda <[email protected]>
hyfdev added a commit to vitejs/vite that referenced this pull request Nov 10, 2026
hyfdev added a commit to vitejs/vite that referenced this pull request Nov 11, 2026
shulaoda pushed a commit to vitejs/vite that referenced this pull request Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: preserveModules: local declaration renamed instead of clashing import

2 participants