Skip to content

Release the roots of a finished build in @tailwindcss/vite - #20736

Merged
RobinMalfait merged 2 commits into
mainfrom
robin/research-issue-20501
Nov 23, 2026
Merged

RobinMalfait merged 2 commits into
mainfrom
robin/research-issue-20501

Conversation

@RobinMalfait

Copy link
Copy Markdown
Contributor

This PR makes @tailwindcss/vite release the compilers and scanners of a build once the build finishes.

On main, a finished build no longer keeps its plugin context or its module graph, thanks to #20531 and #20678. A probe like the one in the issue reports both as freed.

But the build plugin still keeps a Root for each CSS file of each environment, for as long as the plugin lives. Each root keeps its compiler, its scanner, and the resolvers of the environment that built it. So a process that keeps working after the build keeps all of them in memory. Examples are Astro, which renders its pages after the build, and Nuxt, which bundles the server with Nitro after the Vite builds. On Vite 8, Vite itself keeps the plugin alive after build(), so this happens even when the process drops the plugin.

This PR deletes the roots of an environment in closeBundle, except in watch mode, where rebuilds use them again. In watch mode, it deletes them in closeWatcher, once the watcher closes. Unlike the rootsByEnv.clear() that the issue suggests, it only deletes the roots of the environment that finished, so it doesn't touch environments that are still building. Dev mode doesn't change, because the hooks are on the build plugin only (apply: 'build').

The hooks come from #20503 by lazerg, who is a co-author of the commit.

Behavior change

A reused builder (builder.build(environment) more than once) now starts each build of an environment with new roots. So it no longer generates CSS for classes that were removed since an earlier build, like a build in a new process. #20678 already starts the scans over in this case, but the roots kept their earlier candidates. The cost is that the second build sets up its compilers and scanners again.

Memory

Heap after GC, while the process keeps the plugin, for a fixture with 2,000 modules and one Tailwind CSS root, on Node 24.22:

main This PR
Vite 7, build() 34.8 MiB 23.4 MiB
Vite 7, builder with client and SSR 46.6 MiB 23.7 MiB
Vite 7, watch build after close() 35.8 MiB 23.0 MiB
Vite 8, build() 33.8 MiB 21.4 MiB
Vite 8, builder with client and SSR 46.3 MiB 22.5 MiB

Before the build, the heap is 13.6 MiB with Vite 7 and 14.5 MiB with Vite 8. Each root keeps about 11 MiB here, so projects with more roots gain more. In a Nuxt project, #20501 (comment) measured about 130 MiB of live heap for the state of the plugin on main.

The fix also doesn't depend on how the runtime captures closures. On Bun 1.4, the heap of the builder goes from 59.3 MiB to 22.9 MiB.

What stays belongs to Vite. The plugin keeps the resolved config for its next build. On Vite 7, the config keeps the environment of the latest build, because vite:prepare-out-dir keeps a Set of the environments that it prepared. Vite 8 keeps its environments alive by itself.

Test plan

  • A new integration test checks that a finished build releases its compiler while the process keeps the plugin, and that a watch build keeps it until the watcher closes. A @plugin tracks an object that only the compiler keeps, with a WeakRef, and the scripts run with node --expose-gc.
  • A new integration test checks that a reused builder doesn't keep a removed class.
  • Both tests fail without the fix: the compiler stays alive, and .font-bold stays in the second build.
  • Watch rebuilds still reuse their roots: with DEBUG=tailwindcss, the number of compiler setups doesn't change.
  • pnpm run build and the Vite integration tests pass locally, except the tests whose pnpm install fails in my local setup (?commonjs-proxy and Nuxt).

Fixes #20501

Sent by an AI assistant on behalf of @RobinMalfait.

@RobinMalfait
RobinMalfait requested a review from a team as a code owner November 20, 2026 16:13
@RobinMalfait RobinMalfait self-assigned this Nov 20, 2026
@greptile-apps

greptile-apps Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; no actionable issues were found.

Reviews (2) · Last reviewed commit: "Use the watch setting of the environment..." · Reviewed by Greptile

@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The Vite plugin now removes the current environment’s cached roots after a non-watch build and when a watcher closes. Integration tests check compiler collection after build completion and watcher closure, and verify that a rebuild with the same builder retains CSS for a remaining class while omitting a removed class. The changelog records these fixes.

Severity of issue fixed: High

Merge Risk: Low · up to be67b

This change releases cached Vite build roots after builds and when watchers close, which should reduce memory use. A rare race on watcher close could still recreate and retain roots until the plugin is released. The impact is small and bounded, so the change is mergeable with that known follow-up.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the Vite build plugin in @tailwindcss/vite now releases the roots (compilers, scanners, resolvers) of a finished build. The changeset adds closeBundle and closeWatcher…
Description check Passed The description explains the change in detail. It covers the problem (finished builds keep Root objects and their compilers, scanners, and resolvers in memory), the closeBundle and closeWatcher cleanu…
Linked Issues check Passed [#20501] The issue asks that the build plugin stop retaining a finished build's PluginContext through cached Roots. It suggests either detaching the addWatchFile capture or clearing rootsByEnv when th…
Out of Scope Changes check Passed All changed files relate to the linked issue. packages/@tailwindcss-vite/src/index.ts adds the closeBundle and closeWatcher hooks that release roots, including the environment-level watch check. The i…
Full details: Title check

Explanation

The title names the main change: the Vite build plugin in @tailwindcss/vite now releases the roots (compilers, scanners, resolvers) of a finished build. The changeset adds closeBundle and closeWatcher hooks that delete an environment's roots, which matches the title.

Full details: Description check

Explanation

The description explains the change in detail. It covers the problem (finished builds keep Root objects and their compilers, scanners, and resolvers in memory), the closeBundle and closeWatcher cleanup, watch-mode retention, the reused-builder behavior change, memory measurements, and the tests added. This matches the changeset, which adds the hooks in packages/@tailwindcss-vite/src/index.ts and integration tests in integrations/vite/index.test.ts.

Full details: Linked Issues check

Explanation

[#20501] The issue asks that the build plugin stop retaining a finished build's PluginContext through cached Roots. It suggests either detaching the addWatchFile capture or clearing rootsByEnv when the bundle is done. The whole-PR diff (merge base to head) adds a closeBundle hook to the build-only plugin (apply: 'build'). Outside watch mode, it deletes the finished environment's entry from rootsByEnv, which removes the Root.compiler retention path described in the issue. The later commit changes the watch check: it reads this.environment?.config (falling back to the config) build.watch, and also honours this.meta.watchMode. Watched environments keep their roots, and closeWatcher deletes the roots once the watcher closes. This still removes the retention path for non-watched builds, and it does not touch other environments. The incremental diff from the previous reviewed head could not be read, so the change was checked against the whole-PR diff instead. The author-reported heap and RSS measurements were not re-run here. The code path that the issue describes is removed, so the requirement is met by the code.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the linked issue. packages/@tailwindcss-vite/src/index.ts adds the closeBundle and closeWatcher hooks that release roots, including the environment-level watch check. The integration test file adds tests for compiler collection after a build and after watcher close, and for a reused builder not keeping a removed class. The second test covers the direct consequence of the same root lifecycle change. The changelog entry documents the fix and the behavior change. No unrelated change was found in the whole-PR diff. The incremental diff from the previous reviewed head could not be read, so this conclusion relies on the whole-PR diff.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cd092e6a-8e54-4e7e-808a-b425cbed9066
Commits

Reviewing files that changed from the base of the PR and between 89a5f24 and be67b86.

Files selected for processing: 2

  • integrations/vite/index.test.ts
  • packages/@tailwindcss-vite/src/index.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 2

Review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 862d4276-bdfb-4193-9714-a0167ede3e95
Commits

Reviewing files that changed from the base of the PR and between 511c82a and 89a5f24.

Files selected for processing: 3

  • .changelog/vite-release-roots-after-build.md
  • integrations/vite/index.test.ts
  • packages/@tailwindcss-vite/src/index.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

Comment thread packages/@tailwindcss-vite/src/index.ts Resolved
Comment thread packages/@tailwindcss-vite/src/index.ts Resolved
RobinMalfait added a commit that referenced this pull request Nov 20, 2026
`closeBundle` kept the roots when the top-level config had `build.watch`,
also for an environment that turned it off. With `sharedConfigBuild`, a
`configEnvironment` hook can do that, and Vite then builds the environment
without a watcher. No `closeWatcher` follows, so its roots stayed in
memory.

Read the watch setting of the environment when there is one, and fall
back to the config otherwise. Workers built during a watched build still
keep their roots, because their environment has the watch setting of the
config.

This fixes a finding of CodeRabbit on
#20736.
RobinMalfait and others added 2 commits November 23, 2026 14:16
The build plugin kept a `Root` for each CSS file of each environment for
as long as the plugin lived. Each root keeps its compiler, its scanner
and the resolvers of the environment that built it. A process that keeps
the plugin after a build (e.g. Astro, which renders its pages after the
build) kept all of them in memory.

Delete the roots of an environment in `closeBundle`, except in watch
mode, where rebuilds use them again. In watch mode, delete them in
`closeWatcher`, once the watcher closes.

A reused builder now starts each build of an environment with new
roots. It no longer generates CSS for classes that were removed since
an earlier build, like a build in a new process.

This takes the `closeBundle` and `closeWatcher` hooks of
#20503 by lazerg.

#20501

Co-authored-by: lazerg <[email protected]>
`closeBundle` kept the roots when the top-level config had `build.watch`,
also for an environment that turned it off. With `sharedConfigBuild`, a
`configEnvironment` hook can do that, and Vite then builds the environment
without a watcher. No `closeWatcher` follows, so its roots stayed in
memory.

Read the watch setting of the environment when there is one, and fall
back to the config otherwise. Workers built during a watched build still
keep their roots, because their environment has the watch setting of the
config.

This fixes a finding of CodeRabbit on
#20736.
@RobinMalfait
RobinMalfait force-pushed the robin/research-issue-20501 branch from be67b86 to d858b77 Compare November 23, 2026 13:19
@RobinMalfait
RobinMalfait merged commit 032bf02 into main Nov 23, 2026
12 checks passed
@RobinMalfait
RobinMalfait deleted the robin/research-issue-20501 branch November 23, 2026 13:26
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.

@tailwindcss/vite: cached Root.compiler pins the build's plugin context, keeping the finished bundle alive (multi-GB RSS in Astro SSG)

1 participant