Skip to content

fix(worker): keep chunks conflicted with unused workers that are still used by a live worker - #23956

Merged
sapphi-red merged 2 commits into
vitejs:mainfrom
aaronanish-t:fix/nested-worker-treeshaken
Nov 5, 2026
Merged

sapphi-red merged 2 commits into
vitejs:mainfrom
aaronanish-t:fix/nested-worker-treeshaken

Conversation

@aaronanish-t

Copy link
Copy Markdown
Contributor

fixes #23894

So here's the bug: if the module that starts a nested worker gets tree-shaken out of the main bundle, but another worker still uses it, the outer worker ends up pointing at the nested worker's file, and that file just isn't there.

What happens is the main build sees the new Worker(new URL(...)) call and emits the nested worker early (generateEntryUrlExpr). Then generateBundle sees the module got tree-shaken, decides the worker is dead, and deletes it. Then it realises the worker is actually still needed and tries to emit it again, but Rolldown ignores a second emitFile for the same file. So it's gone.

The fix: figure out what's still live first, and don't delete anything that's still used through another worker. Nothing that's still needed gets deleted, so nothing needs re-adding.

For the test, the main page imports a module without using it, and a live worker uses that same module to start a nested worker. The test checks the nested worker's file exists. It fails without the fix and passes with it. A nested worker nobody uses still gets removed like before. I also ran the worker playground in dev, build and bundled-dev modes, plus the packages/vite unit tests.

I thought about ripping out the whole emit-early-then-delete thing, but kept the change small instead. One thing to look at: the same cleanup runs inside worker sub-builds, so those now keep live nested workers too.

There was an earlier PR for this, #23899, but its author closed it to cut down on open PRs. It takes a similar approach, keeping still-live workers in both main and worker builds.

AI usage: Claude Code helped me with testing and information gathering. I built the issue's repro with and without the fix and checked that the nested worker was referenced but missing before, and emitted after.

@sapphi-red sapphi-red changed the title fix(worker): keep nested workers still used by a live worker fix(worker): keep chunks conflicted with unused workers that are still used by a live worker Nov 5, 2026
@sapphi-red
sapphi-red merged commit 7139ef3 into vitejs:main Nov 5, 2026
26 of 27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nested worker not emitted when its module is tree-shaken from the main bundle (regression in 8.3.0-beta.0)

2 participants