Repository navigation
fix(start-plugin-core): release the start compiler when a build bundle closes #8662
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
scil
wants to merge
2
commits into
TanStack:main
Choose a base branch
from
scil:fix/start-compiler-release-after-build
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/start-plugin-core': patch | ||
| --- | ||
|
|
||
| Release each environment's Start compiler when its build bundle closes. `vite build` builds the client, SSR and server environments once each in one process, so the compiler kept from an earlier environment, and the finished module graph its load/resolve closures held, stayed in memory for the rest of the build and could push large apps past the heap limit. |
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
77 changes: 77 additions & 0 deletions
77
packages/start-plugin-core/tests/vite/start-compiler-release.test.ts
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,77 @@ | ||
| import { setFlagsFromString } from 'node:v8' | ||
| import { runInNewContext } from 'node:vm' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { VITE_ENVIRONMENT_NAMES } from '../../src/constants' | ||
| import { startCompilerPlugin } from '../../src/vite/start-compiler-plugin/plugin' | ||
|
|
||
| // Expose `gc()` without the process-wide `--expose-gc` flag, then turn the flag | ||
| // off again once we hold a reference to it. | ||
| setFlagsFromString('--expose-gc') | ||
| const collectGarbage = runInNewContext('gc') as () => void | ||
| setFlagsFromString('--no-expose-gc') | ||
|
|
||
| function createClientPlugin() { | ||
| const plugins = ( | ||
| startCompilerPlugin({ | ||
| framework: 'react', | ||
| environments: [{ name: VITE_ENVIRONMENT_NAMES.client, type: 'client' }], | ||
| providerEnvName: VITE_ENVIRONMENT_NAMES.server, | ||
| }) as Array<any> | ||
| ).flat() | ||
|
|
||
| return plugins.find( | ||
| (plugin) => | ||
| plugin.name === | ||
| `tanstack-start-core::server-fn:${VITE_ENVIRONMENT_NAMES.client}`, | ||
| ) | ||
| } | ||
|
|
||
| // Runs one transform in a fresh hook context, the way a build's plugin driver | ||
| // does, and hands back only a weak reference to that context. | ||
| async function transformInFreshContext(plugin: any) { | ||
| const context = { | ||
| environment: { | ||
| name: VITE_ENVIRONMENT_NAMES.client, | ||
| mode: 'build', | ||
| config: { experimental: {} }, | ||
| }, | ||
| load: () => Promise.resolve(null), | ||
| resolve: () => Promise.resolve(null), | ||
| error: (message: string) => { | ||
| throw new Error(message) | ||
| }, | ||
| warn: () => {}, | ||
| } | ||
|
|
||
| await plugin.transform.handler.call( | ||
| context, | ||
| 'export const value = 1', | ||
| '/project/src/value.ts', | ||
| ) | ||
|
|
||
| return new WeakRef(context) | ||
| } | ||
|
|
||
| describe('startCompilerPlugin in a build', () => { | ||
| test('releases the hook context once the environment bundle closes', async () => { | ||
| const plugin = createClientPlugin() | ||
| plugin.buildStart.call({ | ||
| environment: { name: VITE_ENVIRONMENT_NAMES.client, mode: 'build' }, | ||
| }) | ||
|
|
||
| const contextRef = await transformInFreshContext(plugin) | ||
|
|
||
| plugin.closeBundle?.call({ | ||
| environment: { name: VITE_ENVIRONMENT_NAMES.client, mode: 'build' }, | ||
| }) | ||
|
|
||
| // One collection is not guaranteed to reclaim the target, and a WeakRef | ||
| // keeps it alive until the current job ends, so yield before each pass. | ||
| for (let attempt = 0; attempt < 10 && contextRef.deref(); attempt++) { | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
| collectGarbage() | ||
| } | ||
|
|
||
| expect(contextRef.deref()).toBeUndefined() | ||
| }) | ||
| }) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '1,90p' packages/start-plugin-core/tests/vite/start-compiler-release.test.ts cat packages/start-plugin-core/vite.config.tsRepository: TanStack/router
Length of output: 3356
🏁 Script executed:
Repository: TanStack/router
Length of output: 7243
🌐 Web query:
official JavaScript WeakRef garbage collection timing guarantee and Node.js exposed gc documentation💡 Result:
Gate the WeakRef assertion behind an opt-in test flag.
WeakRefdoes not guarantee when, or whether, an unreachable object is reclaimed. The package’s normaltest:unitandtestscripts run this test unconditionally, so the assertion can fail even aftercloseBundlereleases the compiler context. Gate this test with an opt-in environment variable, consistent with the repository’s existing GC tests.Suggested fix
import { VITE_ENVIRONMENT_NAMES } from '../../src/constants' import { startCompilerPlugin } from '../../src/vite/start-compiler-plugin/plugin' +const runGcTests = process.env.RUN_SSR_GC_TESTS === '1' + // Expose `gc()` without the process-wide `--expose-gc` flag, then turn the flag // off again once we hold a reference to it. @@ - test('releases the hook context once the environment bundle closes', async () => { + test.skipIf(!runGcTests)( + 'releases the hook context once the environment bundle closes', + async () => { @@ - }) + }, + ) })🤖 Prompt for AI Agents