Skip to content

[Bug]: namespace pollution when two dynamic-import targets form a static-import cycle #9320

@aminpaks

Description

@aminpaks

Reproduction link or steps

Standalone repo: https://github.com/aminpaks/vite8-cycle-import-bug

git clone https://github.com/aminpaks/vite8-cycle-import-bug
cd vite8-cycle-import-bug
pnpm install
pnpm run all

The repo contains two builds of the same 4-file app: one with vite 6 (rollup) and one with vite 8.0.10 (rolldown rc.17). The repro script then executes each bundle in node and checks the namespaces returned by the two dynamic imports against the modules' declared exports. Vite 6 ✅ runs; vite 8 💥 crashes with exit code 1.

Minimal source:

// src/form.ts — dynamic-import target #1
import {actionImpl} from './action.ts';
export function formImpl() { return 'form-result'; }
export function callActionFromForm() { return actionImpl(); }

// src/action.ts — dynamic-import target #2; cycles with form.ts
import {formImpl} from './form.ts';
export function actionImpl() { return 'action-result'; }
export function callFormFromAction() { return formImpl(); }

// src/main.ts — entry; both modules are dynamic-import targets
const formNs   = await import('./form.ts');
const actionNs = await import('./action.ts');
// app validates each namespace exports exactly its declared keys

What is expected?

Object.keys(actionNs) returns ['actionImpl', 'callFormFromAction'] — the two exports declared by action.ts.

This is what vite 6 / rollup produces (and what the ES module dynamic-import contract requires).

What is actually happening?

Vite 8 / rolldown produces:

Object.keys(actionNs) === ['actionImpl', 'callFormFromAction', 'n', 't']

n and t are formImpl and callActionFromForm from form.ts, renamed and leaked into action's namespace.

Inspecting dist-v8/assets/action-<hash>.js:

export { actionImpl, callFormFromAction, formImpl as n, callActionFromForm as t };
//                                       ^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^^^^
//                                       form.ts's exports leaked into action's chunk

Both form.ts and action.ts are merged into a single chunk (cycle prevention), and rolldown names that chunk after action.ts's basename (action-<hash>.js). The chunk's top-level exports become the union of both members' symbols, so import('./action') resolves to a polluted namespace.

form.ts happens to be safe because rolldown emits a small re-export facade for it (form-<hash>.js) that only re-exports form's own symbols. Which member is "polluted" vs "facaded" is determined by which basename rolldown picked for the merged chunk — both are at risk in general.

App code that iterates the namespace and validates the contract (e.g. Object.entries(mod).forEach(([k, v]) => { if (!isExpected(v)) throw … }), a common module-as-registry pattern) crashes:

💥 App crashed with uncaught error:
Error: action module contract violated: expected exports [actionImpl, callFormFromAction], got [actionImpl, callFormFromAction, n, t]
    at validate (file:///.../dist-v8/assets/main-<hash>.js:68:107)
    at main   (file:///.../dist-v8/assets/main-<hash>.js:76:2)

System Info

System:
    OS: macOS 26.3.1
    CPU: (11) arm64 Apple M3 Pro
    Memory: 1.78 GB / 36.00 GB
    Shell: 5.9 - /bin/zsh
Binaries:
    Node: 22.22.0
    pnpm: 10.17.0
npmPackages:
    vite (vite8 alias): 8.0.10
    vite (vite6 alias): 6.4.2
    rolldown (transitive, via vite 8): 1.0.0-rc.17

Related closed issues that are NOT the same bug but live in the same area:

Metadata

Metadata

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions