[Bug]: preserveModules: local declaration renamed instead of clashing import
Reproduction link or steps
- REPL (JS case): link
- Repo: https://github.com/jackw/rolldown-preserve-modules-rename-repro
The repo has two folders - js/ (the repl above) and dts/.
In dts/, run npm install && npm run build && npm run consumer. The consumer's type error shows the internal name AnnotationQuery$1 instead of AnnotationQuery.
What is expected?
rolldown renames the import binding and keeps the local name:
import { AnnotationQuery as AnnotationQuery$1 } from "ext-schema";
var AnnotationQuery = class extends AnnotationQuery$1 {};
export { AnnotationQuery };rolldown gives this output when annotations.js is also an entry (input: ['index.js', 'annotations.js']). rollup 4.60.1 gives this output in both cases.
What is actually happening?
rolldown renames the local declaration and keeps the import name:
import { AnnotationQuery } from "ext-schema";
var AnnotationQuery$1 = class extends AnnotationQuery {};
export { AnnotationQuery$1 as AnnotationQuery };This is fine for JS but with rolldown-plugin-dts, consumers of the declaration output see the renamed declaration. From what I can see the plugin gives rolldown the same code for the entry and non-entry cases, so I think the rename is coming from rolldown rather than the plugin.
dts/ adds a second exported type that uses the interface which is then surfaced to consumers of the types in TS errors and IDEs. e.g.
import { AnnotationQuery } from "ext-schema";
interface AnnotationQuery$1 extends AnnotationQuery { extra: string; }
export interface AnnotationSupport<TAnno = AnnotationQuery$1> {
prepareAnnotation(annotation: TAnno): TAnno;
}
export { AnnotationQuery$1 as AnnotationQuery };The export name is correct, but AnnotationSupport refers to the aliased name. TypeScript prints a type by its declared name, so consumers see AnnotationQuery$1.
npm run consumer type-checks a file that uses AnnotationSupport with the wrong type:
error TS2322: Type '(annotation: number) => number' is not assignable to type '(annotation: AnnotationQuery$1) => AnnotationQuery$1'.When annotations.ts is an entry, the same error says AnnotationQuery.
System Info
System:
OS: macOS 26.6.2
CPU: (18) arm64 Apple M5 Pro
Memory: 2.16 GB / 48.00 GB
Shell: 5.9 - /bin/zsh
Binaries:
Node: 24.11.0 - /Users/jackwestbrook/.nvm/versions/node/v24.11.0/bin/node
Yarn: 1.22.22 - /opt/homebrew/bin/yarn
npm: 11.6.1 - /Users/jackwestbrook/.nvm/versions/node/v24.11.0/bin/npm
pnpm: 11.23.0 - /opt/homebrew/bin/pnpm
Deno: 2.9.5 - /opt/homebrew/bin/deno
Browsers:
Brave Browser: 151.1.93.138
Chrome: 154.0.8037.58
Chrome Canary: 154.0.8020.2
Firefox: 156.0.1
Firefox Nightly: 156.0a1
Safari: 26.6.2
npmPackages:
@rolldown/browser: 1.2.11Any additional comments?
I spotted this in the public types of @grafana/data, for example AnnotationSupport<DataQuery$1, AnnotationQuery$1<DataQuery$1>> whilst trying to replace rollup with rolldown.
grafana/grafana#133782 works around it by setting every module as an entry of the declaration builds.
Source: rolldown/rolldown