Repository navigation
Do not retain the finished build's plugin context in @tailwindcss/vite
#20503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lazerg
wants to merge
6
commits into
tailwindlabs:main
Choose a base branch
from
lazerg:fix/issue-20501-vite-plugin-context-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f0bf0a8
Do not retain the finished build's plugin context in `@tailwindcss/vite`
lazerg cd97912
update changelog
lazerg 013b26b
Don't clear the build plugin's Root cache during watch rebuilds
lazerg 880aca0
Use the bundler's watch mode flag and keep plugins reachable in the l…
lazerg bd7cf84
Release cached roots when the watcher closes
lazerg ce8a0ae
Surface watch build errors in the leak test
lazerg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| import { css, js, json, test } from '../utils' | ||
|
|
||
| // The build plugin used to cache a `Root` (and its compiler) for the | ||
| // lifetime of the plugin instance without ever releasing it, which kept the | ||
| // `PluginContext` of the `transform` call that created it (and everything | ||
| // reachable through it, e.g. a finished Rolldown bundle) alive long after | ||
| // the build finished. Only observable with Vite 8's Rolldown bundler, not | ||
| // with Rollup. | ||
| // | ||
| // https://github.com/tailwindlabs/tailwindcss/issues/20501 | ||
| test( | ||
| 'does not keep the transform PluginContext alive after a build finishes', | ||
| { | ||
| fs: { | ||
| 'package.json': json` | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "@tailwindcss/vite": "workspace:^", | ||
| "tailwindcss": "workspace:^" | ||
| }, | ||
| "devDependencies": { | ||
| "vite": "^8" | ||
| } | ||
| } | ||
| `, | ||
| 'src/app.css': css`@import 'tailwindcss';`, | ||
| 'src/main.js': js`import './app.css'`, | ||
| 'probe.mjs': js` | ||
| import { build } from 'vite' | ||
| import tailwindcss from '@tailwindcss/vite' | ||
|
|
||
| const NAME = '@tailwindcss/vite:generate:build' | ||
|
|
||
| let ctxRef = null | ||
| let probeRan = false | ||
|
|
||
| function instrument(plugins) { | ||
| return plugins.map((plugin) => { | ||
| if (plugin.name !== NAME) return plugin | ||
| let original = plugin.transform.handler | ||
| return { | ||
| ...plugin, | ||
| transform: { | ||
| ...plugin.transform, | ||
| handler(...args) { | ||
| ctxRef ??= new WeakRef(this) | ||
| probeRan = true | ||
| return original.apply(this, args) | ||
| }, | ||
| }, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Keep the plugins reachable so only the plugin's own cache can be | ||
| // what keeps the context alive. | ||
| let plugins = instrument(tailwindcss()) | ||
|
|
||
| await build({ | ||
| root: import.meta.dirname, | ||
| logLevel: 'error', | ||
| configFile: false, | ||
| build: { | ||
| write: false, | ||
| lib: { entry: 'src/main.js', formats: ['es'], fileName: 'out' }, | ||
| }, | ||
| plugins: [plugins], | ||
| }) | ||
|
|
||
| for (let i = 0; i < 5; i++) { | ||
| await new Promise((resolve) => setImmediate(resolve)) | ||
| global.gc() | ||
| } | ||
|
|
||
| console.log('plugins=' + plugins.length) | ||
| console.log('probeRan=' + probeRan) | ||
| console.log('pluginContextAlive=' + (ctxRef?.deref() !== undefined)) | ||
| `, | ||
| }, | ||
| }, | ||
| async ({ exec, expect }) => { | ||
| let output = await exec('node --expose-gc probe.mjs') | ||
| expect(output).toContain('probeRan=true') | ||
| expect(output).toContain('pluginContextAlive=false') | ||
| }, | ||
| ) | ||
|
|
||
| test( | ||
| 'does not keep the transform PluginContext alive after a watcher closes', | ||
| { | ||
| fs: { | ||
| 'package.json': json` | ||
| { | ||
| "type": "module", | ||
| "dependencies": { | ||
| "@tailwindcss/vite": "workspace:^", | ||
| "tailwindcss": "workspace:^" | ||
| }, | ||
| "devDependencies": { | ||
| "vite": "^8" | ||
| } | ||
| } | ||
| `, | ||
| 'src/app.css': css`@import 'tailwindcss';`, | ||
| 'src/main.js': js`import './app.css'`, | ||
| 'probe.mjs': js` | ||
| import { build } from 'vite' | ||
| import tailwindcss from '@tailwindcss/vite' | ||
|
|
||
| const NAME = '@tailwindcss/vite:generate:build' | ||
|
|
||
| let ctxRef = null | ||
| let probeRan = false | ||
|
|
||
| function instrument(plugins) { | ||
| return plugins.map((plugin) => { | ||
| if (plugin.name !== NAME) return plugin | ||
| let original = plugin.transform.handler | ||
| return { | ||
| ...plugin, | ||
| transform: { | ||
| ...plugin.transform, | ||
| handler(...args) { | ||
| ctxRef ??= new WeakRef(this) | ||
| probeRan = true | ||
| return original.apply(this, args) | ||
| }, | ||
| }, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // Keep the plugins reachable so only the plugin's own cache can be | ||
| // what keeps the context alive. | ||
| let plugins = instrument(tailwindcss()) | ||
|
|
||
| let watcher = await build({ | ||
| root: import.meta.dirname, | ||
| logLevel: 'error', | ||
| configFile: false, | ||
| build: { | ||
| write: false, | ||
| watch: {}, | ||
| lib: { entry: 'src/main.js', formats: ['es'], fileName: 'out' }, | ||
| }, | ||
| plugins: [plugins], | ||
| }) | ||
|
|
||
| await new Promise((resolve, reject) => { | ||
| watcher.on('event', (event) => { | ||
| if (event.code === 'BUNDLE_END') { | ||
| event.result.close() | ||
| resolve() | ||
| } else if (event.code === 'ERROR') { | ||
| event.result?.close() | ||
| watcher.close().then(() => reject(event.error)) | ||
| } | ||
| }) | ||
| }) | ||
| await watcher.close() | ||
|
|
||
| for (let i = 0; i < 5; i++) { | ||
| await new Promise((resolve) => setImmediate(resolve)) | ||
| global.gc() | ||
| } | ||
|
|
||
| console.log('plugins=' + plugins.length) | ||
| console.log('probeRan=' + probeRan) | ||
| console.log('pluginContextAlive=' + (ctxRef?.deref() !== undefined)) | ||
| `, | ||
| }, | ||
| }, | ||
| async ({ exec, expect }) => { | ||
| let output = await exec('node --expose-gc probe.mjs') | ||
| expect(output).toContain('probeRan=true') | ||
| expect(output).toContain('pluginContextAlive=false') | ||
| }, | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.