diff --git a/.changepacks/changepack_log_compiler_sessions.json b/.changepacks/changepack_log_compiler_sessions.json new file mode 100644 index 000000000..acaa995ce --- /dev/null +++ b/.changepacks/changepack_log_compiler_sessions.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/plugin-utils/package.json": "Patch" + }, + "note": "New `openCompilerSession(compiler)`: it opens an engine session of its own on a shared compiler and returns `{ id, compiler, close }`. Every function of the scoped `compiler` selects the session before it runs, so two apps in one process keep their numbering, sheet, theme and settings apart; `close()` drops the session", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/.changepacks/changepack_log_engine_sessions_bindings.json b/.changepacks/changepack_log_engine_sessions_bindings.json new file mode 100644 index 000000000..f43836947 --- /dev/null +++ b/.changepacks/changepack_log_engine_sessions_bindings.json @@ -0,0 +1,8 @@ +{ + "changes": { + "bindings/devup-ui-wasm/package.json": "Patch", + "bindings/devup-ui-binding/package.json": "Patch" + }, + "note": "New `createSession()`, `selectSession(id)` and `dropSession(id)` exports on the WASM and native engines: a session is an engine state of its own (numbering, sheet, theme, settings and module resolver), and every call works on the selected one. Session 0 is selected from the start. `selectSession` and `dropSession` throw a `TypeError` for an id that is not an unsigned 32-bit integer, and an `Error` for an unknown session or for dropping session 0", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/.changepacks/changepack_log_next_coordinator_registry.json b/.changepacks/changepack_log_next_coordinator_registry.json new file mode 100644 index 000000000..e9651da97 --- /dev/null +++ b/.changepacks/changepack_log_next_coordinator_registry.json @@ -0,0 +1,7 @@ +{ + "changes": { + "packages/next-plugin/package.json": "Patch" + }, + "note": "Turbopack apps in one process no longer share one coordinator: coordinators are kept per resolved `distDir`, each with its own engine session, extraction tracking and listener. Starting a second app no longer closes the first app's coordinator, and a config re-evaluation replaces only its own app's coordinator; the app's session is dropped when its last coordinator closes", + "date": "2026-11-15T00:00:00.000Z" +} diff --git a/bindings/argument-validation.cjs b/bindings/argument-validation.cjs index a0e3edb41..1488e41ba 100644 --- a/bindings/argument-validation.cjs +++ b/bindings/argument-validation.cjs @@ -5,6 +5,7 @@ module.exports = function createBoundary() { const descriptors = { codeExtract: extraction, codeExtractWithoutSourceMap: extraction, + dropSession: 'id:u', getCss: 'file_num:n import_main_css:b', getThemeInterface: 'package_name:s color_interface_name:s typography_interface_name:s length_interface_name:s shadows_interface_name:s theme_interface_name:s', @@ -20,6 +21,7 @@ module.exports = function createBoundary() { reserveClassSlots: 'rows:rows _package:s single_css:b import_main_css_in_code:b _import_main_css_in_css:b import_aliases:aliases', seedFileMap: 'files:a', + selectSession: 'id:u', setAtomHoist: 'threshold:n', setDebug: 'debug:b', setModuleResolver: 'resolver:f', @@ -32,6 +34,7 @@ module.exports = function createBoundary() { p: 'a string, null or undefined', f: 'a function, null or undefined', a: 'an array of strings', + u: 'a session id', aliases: 'an alias mapping', canonical: 'a canonical mapping', class: 'a nested class-index mapping', @@ -51,6 +54,7 @@ module.exports = function createBoundary() { p: (value) => value == null || typeof value === 'string', f: (value) => value == null || typeof value === 'function', a: Array.isArray, + u: (value) => Number.isInteger(value) && value >= 0 && value <= 0xffffffff, } let frame const fail = (slot) => { diff --git a/bindings/devup-ui-binding/README.md b/bindings/devup-ui-binding/README.md index 71afa1212..6014328dc 100644 --- a/bindings/devup-ui-binding/README.md +++ b/bindings/devup-ui-binding/README.md @@ -2,7 +2,7 @@ Synchronous native build-time compiler for Node.js and Bun. Both `require('@devup-ui/binding')` and named ESM imports expose exactly the existing -30 compiler names, with identical function/class identities and no default init. +33 compiler names, with identical function/class identities and no default init. Authored CommonJS/ESM declarations are included; no WASM dependency is required. The private loader selects one optional platform sibling: Windows MSVC, macOS, @@ -96,4 +96,4 @@ The decoder is private and target-bounded: unused Serde hints use trait forwardi Malformed-enum error paths follow shipped serde-wasm-bindgen 0.6.5: Object.entries result length is read and coerced once for the check, then read and coerced again for the invalid-length diagnostic when the first length is not one, before any index read. The second value supplies the diagnostic; errors thrown by its getter or coercion propagate unchanged. Valid enums retain one length read. -The shared native/WASM boundary validates ordinary calls at 46 argument positions in 19 functions: malformed inputs throw a genuine `TypeError` naming the function, parameter and required type before state mutation. Preflight defaults the entire alias map on schema failure (after diagnostics) or a caught iterator-import failure whose `new Error(String(error))` conversion succeeds. Raw access failures and conversion faults escape unchanged before resolver selection; conversion runs once with the pending failure cleared, so reentry is isolated and resolver changes take effect before the outer core starts. Extraction and reservation remain strict. For example, `setPrefix(false)` throws `setPrefix(): argument prefix must be a string, null or undefined`; string/nullish prefixes remain supported. Extraction wrappers retain the measured metadata, receiver, extra-argument and apply/construct policy; `seedFileMap` validates and copies the caller's array before engine entry without rereading its indices. Same-revision Node/Bun identity, 272-case invalid-input, 73-exception preservation and seed-safety gates cover these contracts, not optimized shipping-native performance or real Node-host Linux Rust/authored-JS 100% coverage, which remain public-delivery gates. +The shared native/WASM boundary validates ordinary calls at 48 argument positions in 21 functions: malformed inputs throw a genuine `TypeError` naming the function, parameter and required type before state mutation. Preflight defaults the entire alias map on schema failure (after diagnostics) or a caught iterator-import failure whose `new Error(String(error))` conversion succeeds. Raw access failures and conversion faults escape unchanged before resolver selection; conversion runs once with the pending failure cleared, so reentry is isolated and resolver changes take effect before the outer core starts. Extraction and reservation remain strict. For example, `setPrefix(false)` throws `setPrefix(): argument prefix must be a string, null or undefined`; string/nullish prefixes remain supported. Extraction wrappers retain the measured metadata, receiver, extra-argument and apply/construct policy; `seedFileMap` validates and copies the caller's array before engine entry without rereading its indices. Same-revision Node/Bun identity, 288-case invalid-input, 73-exception preservation and seed-safety gates cover these contracts, not optimized shipping-native performance or real Node-host Linux Rust/authored-JS 100% coverage, which remain public-delivery gates. diff --git a/bindings/devup-ui-binding/adapter.cjs b/bindings/devup-ui-binding/adapter.cjs index a34c93cac..0c3e24546 100644 --- a/bindings/devup-ui-binding/adapter.cjs +++ b/bindings/devup-ui-binding/adapter.cjs @@ -82,6 +82,8 @@ exports.createBinding = function createBinding(raw) { Output: raw.Output, codeExtract, codeExtractWithoutSourceMap, + createSession: () => raw.createSession(), + dropSession: (id) => raw.dropSession(id), exportCanonicalMap: raw.exportCanonicalMap, exportClassMap: raw.exportClassMap, exportFileMap: raw.exportFileMap, @@ -108,6 +110,7 @@ exports.createBinding = function createBinding(raw) { resetBuildState: () => raw.reset(false), resetStateForTesting: () => raw.reset(true), seedFileMap, + selectSession: (id) => raw.selectSession(id), setAtomHoist: (value) => raw.setAtomHoist(index(value)), setDebug: (value) => raw.setDebug(value), setModuleResolver, diff --git a/bindings/devup-ui-binding/index.d.cts b/bindings/devup-ui-binding/index.d.cts index eae4ba96a..153dfeb41 100644 --- a/bindings/devup-ui-binding/index.d.cts +++ b/bindings/devup-ui-binding/index.d.cts @@ -11,7 +11,7 @@ export class Output { readonly updatedBaseStyle: boolean } -// Positional signatures intentionally match the existing 30-name compiler ABI. +// Positional signatures intentionally match the existing 33-name compiler ABI. export function codeExtract( filename: string, code: string, @@ -32,6 +32,10 @@ export function codeExtractWithoutSourceMap( importMainCssInCss: boolean, importAliases: unknown, ): Output +/** Create an engine session with a clean state; select it before each call. */ +export function createSession(): number +/** Drop a session and its state; dropping the live one makes session 0 live. */ +export function dropSession(id: number): boolean export function exportCanonicalMap(): string export function exportClassMap(): string export function exportFileMap(): string @@ -78,6 +82,8 @@ export function reserveClassSlots( export function resetBuildState(): void export function resetStateForTesting(): void export function seedFileMap(files: string[]): void +/** Make a session live: later calls work on its state until another is selected. */ +export function selectSession(id: number): void export function setAtomHoist(threshold?: number | null): void export function setDebug(debug: boolean): void export function setModuleResolver( diff --git a/bindings/devup-ui-binding/index.mjs b/bindings/devup-ui-binding/index.mjs index 015b58724..9e71069c4 100644 --- a/bindings/devup-ui-binding/index.mjs +++ b/bindings/devup-ui-binding/index.mjs @@ -4,6 +4,8 @@ export const { Output, codeExtract, codeExtractWithoutSourceMap, + createSession, + dropSession, exportCanonicalMap, exportClassMap, exportFileMap, @@ -27,6 +29,7 @@ export const { resetBuildState, resetStateForTesting, seedFileMap, + selectSession, setAtomHoist, setDebug, setModuleResolver, diff --git a/bindings/devup-ui-binding/src/host.rs b/bindings/devup-ui-binding/src/host.rs index fd667e6a7..50a782fed 100644 --- a/bindings/devup-ui-binding/src/host.rs +++ b/bindings/devup-ui-binding/src/host.rs @@ -51,6 +51,25 @@ pub fn reset(env: Env, full: bool) -> Result<()> { } Ok(()) } +/// Create an engine session with a clean state and return its id. +#[napi] +pub fn create_session() -> u32 { + core::create_session() +} +/// Make session `id` live until another is selected. +#[napi] +pub fn select_session(env: Env, id: u32) -> Result<()> { + core::select_session(id) + .map(drop) + .map_err(|error| binding_error(env, error)) +} +/// Drop session `id` with its state and this environment's resolver for it. +#[napi] +pub fn drop_session(env: Env, id: u32) -> Result { + let dropped = core::drop_session(id).map_err(|error| binding_error(env, error))?; + state(&env)?.0.remove(id); + Ok(dropped) +} fn with_resolver( env: Env, diagnostics: core::ResolverState, diff --git a/bindings/devup-ui-binding/src/lib.rs b/bindings/devup-ui-binding/src/lib.rs index 7211b6d16..cc0eec980 100644 --- a/bindings/devup-ui-binding/src/lib.rs +++ b/bindings/devup-ui-binding/src/lib.rs @@ -5,7 +5,10 @@ mod host; mod output; mod value; -pub use host::{Resolved, extract, has_devup_ui, init, reserve, reset, set_resolver}; +pub use host::{ + Resolved, create_session, drop_session, extract, has_devup_ui, init, reserve, reset, + select_session, set_resolver, +}; pub use output::NativeOutput; use de::Wasm32Index; diff --git a/bindings/devup-ui-wasm/README.md b/bindings/devup-ui-wasm/README.md index 066708bcf..507b238b7 100644 --- a/bindings/devup-ui-wasm/README.md +++ b/bindings/devup-ui-wasm/README.md @@ -67,6 +67,16 @@ unwind. Binding tests compose these with sheet/resolver snapshots. Keep a named guard on its creating thread and retain `#[serial]`: guards clean up but do not serialize concurrent tests. Cleanup does not run for panic=abort. +## Sessions + +`createSession()` returns the id of a new engine session with a clean state, and +`selectSession(id)` makes it live: every later call reads and changes that +session's state, resolver included, until another is selected. Session 0 is +live from the start, so a single app needs neither. Two apps in one process +each select their own session before each call. `dropSession(id)` forgets a +session (dropping the live one makes session 0 live), and the full test reset +forgets every session but 0. + ## Errors A source the build cannot compile makes `codeExtract` throw an `Error`. Its diff --git a/bindings/devup-ui-wasm/script.js b/bindings/devup-ui-wasm/script.js index 7bc343dff..754f3bb39 100644 --- a/bindings/devup-ui-wasm/script.js +++ b/bindings/devup-ui-wasm/script.js @@ -50,9 +50,9 @@ types = types.replace(hookType, '') assert.equal(types.includes('install_panic_hook'), false) writeFileSync('pkg/index.d.ts', types, 'utf8') const names = [...source.matchAll(/^exports\.(\w+) = \1;/gm)] -assert.equal(names.length, 30) -assert.equal(new Set(names.map((match) => match[1])).size, 30) -assert.equal([...source.matchAll(/^exports\./gm)].length, 30) +assert.equal(names.length, 33) +assert.equal(new Set(names.map((match) => match[1])).size, 33) +assert.equal([...source.matchAll(/^exports\./gm)].length, 33) assert.equal(source.includes('module.exports'), false) for (const name of ['codeExtract', 'codeExtractWithoutSourceMap']) { const signatures = [ diff --git a/bindings/devup-ui-wasm/src/lib.rs b/bindings/devup-ui-wasm/src/lib.rs index 7c3166f2e..516044ce8 100644 --- a/bindings/devup-ui-wasm/src/lib.rs +++ b/bindings/devup-ui-wasm/src/lib.rs @@ -299,6 +299,40 @@ pub fn seed_file_map(files: Vec) { binding_core::seed_file_map(&files); } +/// Create an engine session with a clean state and return its id. +/// +/// Two apps in one process each keep their own session: select it before +/// every call so the call reads and changes only that app's state. Session 0 +/// is live until another is selected. +#[wasm_bindgen(js_name = "createSession")] +#[must_use] +pub fn create_session() -> u32 { + binding_core::create_session() +} + +/// Make session `id` live: every later call works on its state until +/// another session is selected. +#[wasm_bindgen(js_name = "selectSession")] +#[cfg(not(tarpaulin_include))] +pub fn select_session(id: u32) -> Result<(), JsValue> { + binding_core::select_session(id).map(drop).map_err(js_error) +} + +/// Drop session `id` with its state and resolver (testable without `JsValue`) +pub fn drop_session_internal(id: u32) -> Result { + let dropped = binding_core::drop_session(id)?; + MODULE_RESOLVER.with_borrow_mut(|resolvers| resolvers.remove(id)); + Ok(dropped) +} + +/// Drop session `id` with its state and module resolver, returning whether +/// it existed. Dropping the live session makes session 0 live. +#[wasm_bindgen(js_name = "dropSession")] +#[cfg(not(tarpaulin_include))] +pub fn drop_session(id: u32) -> Result { + drop_session_internal(id).map_err(js_error) +} + /// Forget a source file that was deleted or renamed away. /// /// Its global CSS, its own bucket and the class names handed out in it go, @@ -1729,6 +1763,25 @@ mod tests { assert!(json_str.starts_with('{') || json_str.starts_with("[]")); } + #[test] + #[serial] + fn sessions_keep_their_own_prefix_until_dropped() { + let _state = TestStateGuard::new(); + // Given: a second session with its own prefix + let session = create_session(); + assert_eq!(binding_core::select_session(session), Ok(0)); + set_prefix(Some("app-".into())); + // When / Then: session 0 keeps its own, and the dropped session goes + assert_eq!(binding_core::select_session(0), Ok(session)); + assert_eq!(get_prefix(), None); + assert_eq!( + drop_session_internal(0), + Err("engine session 0 cannot be dropped".to_string()) + ); + assert_eq!(drop_session_internal(session), Ok(true)); + assert_eq!(drop_session_internal(session), Ok(false)); + } + #[test] #[serial] fn remove_file_reports_whether_the_sheet_changed() { diff --git a/e2e/binding/corpus/state.mjs b/e2e/binding/corpus/state.mjs index 85f5e8b64..9a56e169f 100644 --- a/e2e/binding/corpus/state.mjs +++ b/e2e/binding/corpus/state.mjs @@ -42,6 +42,33 @@ add('reservation-malformed-second-row-arity-nonmutation', (h) => { h.call('reserveClassSlots', value, pkg, false, false, false, {}) h.call('reserveClassSlots', rows, pkg, false, false, false, { bad: 7 }) }) +add('sessions-keep-apart-until-dropped', (h) => { + h.call('setPrefix', 'zero-') + h.extract(source) + const session = h.call('createSession').value.value + h.call('selectSession', session) + h.call('registerTheme', theme) + h.extract(box('color="blue"'), { filename: 'src/second.tsx' }) + h.call('selectSession', 0) + h.extract(source) + h.call('selectSession', 7) + h.call('dropSession', 0) + h.call('selectSession', session) + h.call('dropSession', session) + h.call('dropSession', session) +}) +add('remove-file-stays-in-its-session', (h) => { + h.extract(source, { single: false }) + const session = h.call('createSession').value.value + h.call('selectSession', session) + h.extract(box('color="blue"'), { single: false }) + h.call('selectSession', 0) + h.call('removeFile', file) + h.call('removeFile', file) + h.call('selectSession', session) + h.call('removeFile', file) + h.call('dropSession', session) +}) add('remove-file-drops-its-own-styles', (h) => { h.call('importCanonicalMap', { 'src/child.tsx': 'src/root.tsx' }) h.extract(source, { single: false }) diff --git a/e2e/binding/host.mjs b/e2e/binding/host.mjs index a86141ce5..861eed8ba 100644 --- a/e2e/binding/host.mjs +++ b/e2e/binding/host.mjs @@ -68,7 +68,7 @@ save('provenance.json', { scenarios: corpus.map((c) => c.name), }) const names = - 'Output codeExtract codeExtractWithoutSourceMap exportCanonicalMap exportClassMap exportFileMap exportSheet getCss getDefaultTheme getPrefix getThemeInterface getUnreservedClassSlotCount hasDevupUI importCanonicalMap importClassMap importFileMap importFileRoutes importSheet isDebug registerShorthands registerTheme removeFile reserveClassSlots resetBuildState resetStateForTesting seedFileMap setAtomHoist setDebug setModuleResolver setPrefix'.split( + 'Output codeExtract codeExtractWithoutSourceMap createSession dropSession exportCanonicalMap exportClassMap exportFileMap exportSheet getCss getDefaultTheme getPrefix getThemeInterface getUnreservedClassSlotCount hasDevupUI importCanonicalMap importClassMap importFileMap importFileRoutes importSheet isDebug registerShorthands registerTheme removeFile reserveClassSlots resetBuildState resetStateForTesting seedFileMap selectSession setAtomHoist setDebug setModuleResolver setPrefix'.split( ' ', ) const abi = { @@ -199,8 +199,8 @@ const h = { ) }, } -assert.equal(corpus.length, 26) -assert.equal(new Set(corpus.map((c) => c.name)).size, 26) +assert.equal(corpus.length, 28) +assert.equal(new Set(corpus.map((c) => c.name)).size, 28) h.note('public-error-inventory', runDiagnostics(w)) for (const test of corpus) { scenario = test.name diff --git a/e2e/binding/package/consumer.mjs b/e2e/binding/package/consumer.mjs index 3940b9cca..c3bb6d17f 100644 --- a/e2e/binding/package/consumer.mjs +++ b/e2e/binding/package/consumer.mjs @@ -136,7 +136,7 @@ export async function consume(options) { ] .map((match) => match[1]) .sort() - assert.equal(names.length, 30) + assert.equal(names.length, 33) for (const manager of ['npm', 'bun']) { for (const omit of [false, true]) { const startRequest = server.requests.length diff --git a/e2e/binding/validation-cases.mjs b/e2e/binding/validation-cases.mjs index f69d04387..715538bdd 100644 --- a/e2e/binding/validation-cases.mjs +++ b/e2e/binding/validation-cases.mjs @@ -20,6 +20,7 @@ const extractSlots = export const positions = [ ['codeExtract', extractSlots, extraction], ['codeExtractWithoutSourceMap', extractSlots, extraction], + ['dropSession', 'id:session', () => [1]], ['getCss', 'file_num:n import_main_css:b', () => [null, false]], [ 'getThemeInterface', @@ -45,6 +46,7 @@ export const positions = [ () => [[row], pkg, false, false, false, {}], ], ['seedFileMap', 'files:seed', () => [['src/new.tsx']]], + ['selectSession', 'id:session', () => [0]], ['setAtomHoist', 'threshold:n', () => [2]], ['setDebug', 'debug:b', () => [true]], ['setModuleResolver', 'resolver:resolver', () => [null]], @@ -62,6 +64,7 @@ const labels = { prefix: 'a string, null or undefined', resolver: 'a function, null or undefined', seed: 'an array of strings', + session: 'a session id', aliases: 'an alias mapping', canonical: 'a canonical mapping', class: 'a nested class-index mapping', @@ -81,6 +84,7 @@ const invalid = { b: [7, 'false', null, undefined], n: ['2', false, 1n, Symbol('number'), {}], prefix: [false, true, 7, {}], + session: ['1', false, -1, 1.5, 2 ** 32, null, undefined, {}], resolver: [false, true, 7, 'callback', {}], seed: [ ...roots, @@ -154,6 +158,6 @@ export const cases = positions.flatMap((position) => .map((good, index) => (index === position.index ? value : good)), })), ) -assert.equal(positions.length, 46) -assert.equal(new Set(positions.map((p) => p.fn)).size, 19) +assert.equal(positions.length, 48) +assert.equal(new Set(positions.map((p) => p.fn)).size, 21) assert.equal(new Set(cases.map((c) => c.id)).size, cases.length) diff --git a/e2e/binding/validation.mjs b/e2e/binding/validation.mjs index 499bb1b34..d1ee1fe06 100644 --- a/e2e/binding/validation.mjs +++ b/e2e/binding/validation.mjs @@ -114,7 +114,7 @@ for (const [engine, entry] of [ const summary = { cases: cases.length, positions: positions.length, - functions: 18, + functions: 21, engines: 2, runs: runs.length, failed: runs.filter((run) => run.failed).length, diff --git a/packages/next-plugin/README.md b/packages/next-plugin/README.md index d6313129d..098edf5f0 100644 --- a/packages/next-plugin/README.md +++ b/packages/next-plugin/README.md @@ -218,8 +218,11 @@ The build-time compiler defaults to `native` for Webpack and Turbopack. Select WASM explicitly with `DevupUI(nextConfig, { engine: 'wasm' })`. The chosen engine is retained by configuration, prewarm, extraction, CSS and delayed callbacks. Turbopack loader options carry only the engine string; the coordinator retains -the selected physical compiler. Independent concurrent builds in one process -are unsupported. +the selected physical compiler. Turbopack apps in one process keep one +coordinator and one engine session per resolved `distDir`, so their numbering, +sheet, theme and settings stay apart; give each app its own `distDir`. A config +re-evaluation of the same app replaces only that app's coordinator. Independent +concurrent Webpack builds in one process are unsupported. Native load failures identify the required platform package and retain the original cause. There is no automatic fallback to WASM. Install optional native diff --git a/packages/next-plugin/src/__tests__/coordinator.test.ts b/packages/next-plugin/src/__tests__/coordinator.test.ts index d81971497..4fa657038 100644 --- a/packages/next-plugin/src/__tests__/coordinator.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator.test.ts @@ -16,8 +16,10 @@ import { } from 'bun:test' import { + coordinatorKey, type CoordinatorOptions, flushCoordinatorWrites, + openCoordinatorCompiler, resetCoordinator, startCoordinator, } from '../coordinator' @@ -39,6 +41,7 @@ function makeOptions( overrides: Partial = {}, ): CoordinatorOptions { return { + distDir: tmpDir, wasm, package: '@devup-ui/react', cssDir: join(tmpDir, 'css'), @@ -948,6 +951,83 @@ describe('coordinator', () => { second.close() }) + it('keeps the coordinators of different apps running side by side', async () => { + // Given: two apps in one process, each with its own distDir and port file. + const app = (name: string) => + makeOptions({ + distDir: join(tmpDir, name), + coordinatorPortFile: join(tmpDir, name, 'coordinator.port'), + }) + const portOf = (options: CoordinatorOptions) => + parseInt( + ( + writeFileSyncSpy.mock.calls.findLast( + ([path]) => path === options.coordinatorPortFile, + ) as [string, string] + )[1], + ) + const one = app('one') + const two = app('two') + + // When: the second app starts its coordinator after the first. + const first = startCoordinator(one) + const second = startCoordinator(two) + await new Promise((resolve) => setTimeout(resolve, 100)) + + // Then: both answer, and closing one leaves the other running. + expect(await httpRequest(portOf(one), 'GET', '/health')).toEqual({ + status: 200, + body: 'ok', + }) + first.close() + expect(await httpRequest(portOf(two), 'GET', '/health')).toEqual({ + status: 200, + body: 'ok', + }) + second.close() + }) + + it('keeps one engine session per app until its last coordinator closes', () => { + // Given: one app's engine, read through two spellings of its distDir. + const distDir = join(tmpDir, 'app') + const engine = openCoordinatorCompiler(distDir, wasm) + expect(openCoordinatorCompiler(join(distDir, 'x', '..'), wasm)).toBe(engine) + expect(coordinatorKey(join(distDir, 'x', '..'))).toBe( + coordinatorKey(distDir), + ) + const other = openCoordinatorCompiler(join(tmpDir, 'other'), wasm) + expect(other).not.toBe(engine) + engine.setPrefix('app-') + expect(other.exportSheet()).not.toContain('app-') + + // When: a config re-evaluation starts a second coordinator for the app. + const options = makeOptions({ distDir, wasm: engine }) + const first = startCoordinator(options) + const second = startCoordinator(options) + first.close() + first.close() + + // Then: the session outlives the superseded coordinator, even closed twice. + expect(engine.getCss(null, false)).toBeString() + second.close() + expect(() => engine.getCss(null, false)).toThrow('does not exist') + expect(openCoordinatorCompiler(distDir, wasm)).not.toBe(engine) + }) + + it('opens a new session when an app moves to another engine', () => { + // Given: an app whose session lives in the WASM engine. + const distDir = join(tmpDir, 'moving') + const before = openCoordinatorCompiler(distDir, wasm) + + // When: a re-evaluation hands over another engine namespace. + const after = openCoordinatorCompiler(distDir, new Proxy(wasm, {})) + + // Then: the first session is dropped and a new one is used. + expect(after).not.toBe(before) + expect(() => before.getCss(null, false)).toThrow('does not exist') + expect(after.getCss(null, false)).toBeString() + }) + it('should touch devup-ui.css to invalidate Turbopack cache when singleCss=false and new CSS collected', async () => { codeExtractSpy.mockReturnValue({ code: 'import "./../../df/devup-ui/devup-ui-5.css";\nconst x = 1;', @@ -1729,6 +1809,52 @@ describe('coordinator per-bucket completion', () => { coordinator.close() }) + + it("never completes one app's CSS with another app's extractions", async () => { + // Given: two apps in one process that expect the same base files. + codeExtractSpy.mockReturnValue(extractResult('devup-ui.css')) + getCssSpy.mockReturnValue('base-css') + const app = (name: string) => + makeOptions({ + distDir: join(tmpDir, `wait-${name}`), + coordinatorPortFile: join(tmpDir, `wait-${name}`, 'coordinator.port'), + expectedBaseFiles: ['src/a.tsx', 'src/b.tsx'], + quietMs: 100, + maxWaitMs: 10_000, + }) + const one = app('one') + const two = app('two') + const first = startCoordinator(one) + const second = startCoordinator(two) + await new Promise((r) => setTimeout(r, 100)) + const portOf = (options: CoordinatorOptions) => + parseInt( + ( + writeFileSyncSpy.mock.calls.findLast( + ([path]: unknown[]) => path === options.coordinatorPortFile, + ) as [string, string] + )[1], + ) + + // When: each app extracts a different one of those files. + await extract(portOf(one), 'src/a.tsx') + await extract(portOf(two), 'src/b.tsx') + + // Then: the first app still misses the file only the second extracted, + // so its build fails instead of serving partial CSS. + const res = await httpRequest( + portOf(one), + 'GET', + '/css?importMainCss=false&waitForIdle=true', + ) + expect(res.status).toBe(500) + expect(res.body).toBe( + '[devup-ui] coordinator: base CSS is incomplete (no extraction for 100ms); refusing to serve partial CSS. Files not extracted: src/b.tsx.', + ) + + first.close() + second.close() + }) }) describe('coordinator transform cache', () => { @@ -1842,6 +1968,47 @@ describe('coordinator transform cache', () => { coordinator.close() }) + + it('keeps the cached extractions of different apps apart', async () => { + // Given: two apps in one process with the same CSS directory, so only the + // app tells their extractions of the same source apart. + codeExtractSpy.mockImplementation((_filename: string, code: string) => + extractResult(`out:${code}`), + ) + const app = (name: string) => + makeOptions({ + distDir: join(tmpDir, `cache-${name}`), + coordinatorPortFile: join(tmpDir, `cache-${name}`, 'coordinator.port'), + }) + const one = app('one') + const two = app('two') + const first = startCoordinator(one) + const second = startCoordinator(two) + await new Promise((r) => setTimeout(r, 100)) + const portOf = (options: CoordinatorOptions) => + parseInt( + ( + writeFileSyncSpy.mock.calls.findLast( + ([path]: unknown[]) => path === options.coordinatorPortFile, + ) as [string, string] + )[1], + ) + + // When: the first app extracts a file, then the second app the same file + // with the same source. + await extract(portOf(one), 'v1') + await extract(portOf(two), 'v1') + + // Then: the second app's sheet never saw that source, so it extracts too; + // each app then answers its own repeat from its own cache. + expect(codeExtractSpy).toHaveBeenCalledTimes(2) + await extract(portOf(one), 'v1') + await extract(portOf(two), 'v1') + expect(codeExtractSpy).toHaveBeenCalledTimes(2) + + first.close() + second.close() + }) }) describe('coordinator theme reload', () => { diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 4cc97db5b..8fa834852 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -167,6 +167,7 @@ beforeEach(() => { }) afterEach(() => { + coordinatorModule.resetCoordinator() resetTurboSetupCacheForTesting() setWasmForTesting(undefined) setWebpackPluginForTesting(undefined) @@ -977,7 +978,8 @@ describe('DevupUINextPlugin', () => { }, }) expect(startCoordinatorSpy).toHaveBeenCalledWith({ - wasm, + distDir: 'df', + wasm: coordinatorModule.openCoordinatorCompiler('df', wasm), package: '@devup-ui/react', cssDir: resolve('df', 'devup-ui'), singleCss: false, @@ -1223,7 +1225,10 @@ export const box = style({ color: 'red' })` setNodeEnv('development') DevupUI({}, { include: ['@acme/ui'], singleCss: true }) - expect(reservationSpy).toHaveBeenCalledWith(wasm) + // Into the app's own engine session, never the shared engine. + expect(reservationSpy).toHaveBeenCalledWith( + coordinatorModule.openCoordinatorCompiler('df', wasm), + ) const [discovery, options] = reserve.mock.calls[0] as Parameters< ReturnType< typeof importGraphModule.createSharedSlotReservation @@ -1400,7 +1405,8 @@ export const box = style({ color: 'red' })` // Verify coordinator was started with correct options expect(startCoordinatorSpy).toHaveBeenCalledWith({ - wasm, + distDir: 'df', + wasm: coordinatorModule.openCoordinatorCompiler('df', wasm), package: '@devup-ui/react', cssDir: resolve('df', 'devup-ui'), singleCss: false, diff --git a/packages/next-plugin/src/__tests__/source-map.wasm.test.ts b/packages/next-plugin/src/__tests__/source-map.wasm.test.ts index dd404834e..57b01bf65 100644 --- a/packages/next-plugin/src/__tests__/source-map.wasm.test.ts +++ b/packages/next-plugin/src/__tests__/source-map.wasm.test.ts @@ -206,6 +206,7 @@ describe.each([false, true])('Next singleCss with prewarm=%s', (prewarm) => { try { coordinator = startCoordinator({ ...options, + distDir: root, wasm, sourceMap, importAliases: {}, diff --git a/packages/next-plugin/src/coordinator.ts b/packages/next-plugin/src/coordinator.ts index 086aa9355..974b18d44 100644 --- a/packages/next-plugin/src/coordinator.ts +++ b/packages/next-plugin/src/coordinator.ts @@ -2,13 +2,23 @@ import { existsSync, statSync, writeFile, writeFileSync } from 'node:fs' import { createServer, type IncomingMessage, type Server } from 'node:http' import { basename, dirname, join, relative, resolve } from 'node:path' -import { getFileNumByFilename } from '@devup-ui/plugin-utils' +import { + type CompilerSession, + getFileNumByFilename, + openCompilerSession, +} from '@devup-ui/plugin-utils' import { formatPortFile, removeOwnPortFile } from './coordinator-port' import { elapsedMs, profileStart, reportProfile } from './profile' -import type { DevupWasm } from './wasm' +import { type DevupWasm, withModuleResolver } from './wasm' export interface CoordinatorOptions { + /** + * The app's output directory. Coordinators are kept per resolved `distDir`: + * starting one replaces only the previous coordinator of the same app. + */ + distDir: string + /** The app's engine, from `openCoordinatorCompiler(distDir, ...)`. */ wasm: DevupWasm package: string cssDir: string @@ -196,14 +206,13 @@ function readBody(req: IncomingMessage): Promise { }) } -let server: Server | null = null - // The latest extraction of each file. Next compiles a module that both the // server and the client graph import once per graph, with the same source, // and in no guaranteed order. Re-running WASM for the second copy only // rebuilds the same styles, so it answers from here instead. Keeping only the // latest source per file bounds the cache by the project's file count and -// guarantees the sheet still holds exactly that source's styles. +// guarantees the sheet still holds exactly that source's styles. Each app +// keeps its own in its CoordinatorState, since each app has its own sheet. interface CachedExtract { code: string relCssDir: string @@ -212,16 +221,12 @@ interface CachedExtract { output: ExtractOutputSnapshot } -const transformCache = new Map() - interface ThemeWatch { files: string[] stamp: string reload: () => string[] } -let themeWatch: ThemeWatch | undefined - /** * What changes when one of the files is edited, added or deleted: a theme * input, or a file an extraction read through the resolver. @@ -240,11 +245,12 @@ function fileStamp(files: string[] = []): string { } function cachedExtract( + state: CoordinatorState, filename: string, code: string, relCssDir: string, ): ExtractOutputSnapshot | undefined { - const cached = transformCache.get(filename) + const cached = state.transformCache.get(filename) return cached?.code === code && cached.relCssDir === relCssDir && cached.dependencies === fileStamp(cached.output.dependencies) @@ -252,15 +258,17 @@ function cachedExtract( : undefined } -/** Register the theme again when one of its input files has changed. */ -function refreshTheme(): void { +/** Register the app's theme again when one of its input files has changed. */ +function refreshTheme(state: CoordinatorState): void { + const { themeWatch } = state if (!themeWatch || fileStamp(themeWatch.files) === themeWatch.stamp) return - // Every cached extraction was made under the old theme. Even a reload that - // throws may have changed the registered theme, so drop them first. - transformCache.clear() + // Every cached extraction of this app was made under the old theme. Even a + // reload that throws may have changed the registered theme, so drop them + // first. + state.transformCache.clear() // A throwing reload keeps the old stamp, so the next request retries. const files = themeWatch.reload() - themeWatch = { ...themeWatch, files, stamp: fileStamp(files) } + state.themeWatch = { ...themeWatch, files, stamp: fileStamp(files) } } // Extraction tracking for waitForIdle. @@ -282,39 +290,95 @@ function refreshTheme(): void { // extraction batches. Empirically the gap between Turbopack extraction // "waves" in a 64-route landing build can exceed the previous 500ms, which // caused the snapshot to capture only the early routes' styles. -let activeExtractions = 0 -let totalExtractions = 0 -let lastCompletedAt = 0 -let pendingExtractStarts = 0 -let idleThresholdMs = 2500 -let quietMs = 10_000 -let maxWaitMs = 60_000 +// +// Per-bucket completion tracking (deterministic replacement for waitForIdle on +// collapsed chunks). +// +// Single-importer collapse merges several source files into ONE shared CSS +// chunk (a "bucket"). That chunk is only complete once EVERY member file has +// been extracted. Turbopack, however, may request the chunk as soon as ONE +// member's import resolves. The old global idle heuristic guessed completion +// and dropped late members' atoms when extraction "waves" exceeded the idle +// threshold (flaky CI rendering). Instead we wait for the bucket's KNOWN +// members (from the canonical map) — no guessing, no extra extraction. +// +// Each coordinator keeps this tracking in a state of its own, so two apps in +// one process never count each other's extractions. +interface CoordinatorState { + activeExtractions: number + totalExtractions: number + lastCompletedAt: number + pendingExtractStarts: number + idleThresholdMs: number + quietMs: number + maxWaitMs: number + extractedFiles: Set + // Where each file the loaders posted lives, to notice the ones deleted since + extractedSources: Map + // The latest extraction of each file, for this app's sheet only + transformCache: Map + fileNumToBucket: Map + bucketToMembers: Map> + canonicalMap: Record + // Route-reachable runtime files the base sheet must wait for (cwd-relative + // POSIX). When populated, base-css completion is deterministic; empty falls + // back to the idle heuristic. See CoordinatorOptions.expectedBaseFiles. + expectedBaseFiles: Set + // The files the app's theme was read from, under `next dev` + themeWatch: ThemeWatch | undefined +} + +function createState(options: CoordinatorOptions): CoordinatorState { + return { + activeExtractions: 0, + totalExtractions: 0, + lastCompletedAt: 0, + pendingExtractStarts: 0, + idleThresholdMs: options.idleThresholdMs ?? 2500, + quietMs: options.quietMs ?? 10_000, + maxWaitMs: options.maxWaitMs ?? 60_000, + extractedFiles: new Set(options.prewarmedFiles), + extractedSources: new Map(), + transformCache: new Map(), + fileNumToBucket: new Map(), + bucketToMembers: buildBucketToMembers(options.canonicalMap), + canonicalMap: options.canonicalMap, + expectedBaseFiles: new Set(options.expectedBaseFiles), + themeWatch: options.theme && { + ...options.theme, + stamp: fileStamp(options.theme.files), + }, + } +} // Give-up signal for a wait whose known files never report. It only observes // extraction traffic; it is NOT a Turbopack compilation-complete signal, so it // ends the wait with an error, never with partial CSS. Production builds seed // `extractedFiles` with their prewarmed route closure and never reach it. -function bundlerQuiet(now: number): boolean { +function bundlerQuiet(state: CoordinatorState, now: number): boolean { return ( - totalExtractions > 0 && - activeExtractions === 0 && - pendingExtractStarts === 0 && - now - lastCompletedAt >= quietMs + state.totalExtractions > 0 && + state.activeExtractions === 0 && + state.pendingExtractStarts === 0 && + now - state.lastCompletedAt >= state.quietMs ) } -function baseFilesComplete(): boolean { +function baseFilesComplete(state: CoordinatorState): boolean { // Deterministic: the base sheet is complete once every route-reachable file // has been extracted by either the production prewarm or `/extract`. - if (expectedBaseFiles.size === 0) return false - for (const file of expectedBaseFiles) { - if (!extractedFiles.has(file)) return false + if (state.expectedBaseFiles.size === 0) return false + for (const file of state.expectedBaseFiles) { + if (!state.extractedFiles.has(file)) return false } return true } -function missingFiles(files: Iterable): string[] { - return [...files].filter((file) => !extractedFiles.has(file)) +function missingFiles( + state: CoordinatorState, + files: Iterable, +): string[] { + return [...files].filter((file) => !state.extractedFiles.has(file)) } /** @@ -333,37 +397,37 @@ function incompleteCssError( } /** Resolves with an error message when the base sheet cannot be completed. */ -function waitForBase(): Promise { +function waitForBase(state: CoordinatorState): Promise { const start = Date.now() return new Promise((resolve) => { const check = () => { const now = Date.now() - if (now - start > maxWaitMs) { + if (now - start > state.maxWaitMs) { // Last-resort backstop (see waitForBucket). Never fires on a healthy // build: either every expected file extracts, or the idle fallback // resolves first. resolve( incompleteCssError( 'base CSS', - `not complete after ${maxWaitMs}ms`, - missingFiles(expectedBaseFiles), + `not complete after ${state.maxWaitMs}ms`, + missingFiles(state, state.expectedBaseFiles), ), ) return } // Primary, deterministic path. - if (baseFilesComplete()) { + if (baseFilesComplete(state)) { resolve(undefined) return } // Expected files stopped arriving. The Next plugin's production path // prewarms them and completes above, so this is a real gap. - if (expectedBaseFiles.size > 0 && bundlerQuiet(now)) { + if (state.expectedBaseFiles.size > 0 && bundlerQuiet(state, now)) { resolve( incompleteCssError( 'base CSS', - `no extraction for ${quietMs}ms`, - missingFiles(expectedBaseFiles), + `no extraction for ${state.quietMs}ms`, + missingFiles(state, state.expectedBaseFiles), ), ) return @@ -371,11 +435,11 @@ function waitForBase(): Promise { // Fallback ONLY when no deterministic signal exists (no routes detected / // pre-pass failed -> expectedBaseFiles empty): the legacy idle heuristic. if ( - expectedBaseFiles.size === 0 && - totalExtractions > 0 && - activeExtractions === 0 && - pendingExtractStarts === 0 && - now - lastCompletedAt >= idleThresholdMs + state.expectedBaseFiles.size === 0 && + state.totalExtractions > 0 && + state.activeExtractions === 0 && + state.pendingExtractStarts === 0 && + now - state.lastCompletedAt >= state.idleThresholdMs ) { resolve(undefined) return @@ -386,27 +450,6 @@ function waitForBase(): Promise { }) } -// Per-bucket completion tracking (deterministic replacement for waitForIdle on -// collapsed chunks). -// -// Single-importer collapse merges several source files into ONE shared CSS -// chunk (a "bucket"). That chunk is only complete once EVERY member file has -// been extracted. Turbopack, however, may request the chunk as soon as ONE -// member's import resolves. The old global idle heuristic guessed completion -// and dropped late members' atoms when extraction "waves" exceeded the idle -// threshold (flaky CI rendering). Instead we wait for the bucket's KNOWN -// members (from the canonical map) — no guessing, no extra extraction. -const extractedFiles = new Set() -// Where each file the loaders posted lives, to notice the ones deleted since -const extractedSources = new Map() -const fileNumToBucket = new Map() -let bucketToMembers = new Map>() -let canonicalMapRef: Record = {} -// Route-reachable runtime files the base sheet must wait for (cwd-relative -// POSIX). When populated, base-css completion is deterministic; empty falls back -// to the idle heuristic. See CoordinatorOptions.expectedBaseFiles. -let expectedBaseFiles = new Set() - function buildBucketToMembers( canonicalMap: Record, ): Map> { @@ -426,12 +469,16 @@ function buildBucketToMembers( } /** Resolves with an error message when the bucket cannot be completed. */ -function waitForBucket(bucket: string): Promise { - const members = bucketToMembers.get(bucket) ?? new Set([bucket]) +function waitForBucket( + state: CoordinatorState, + bucket: string, +): Promise { + const { maxWaitMs, quietMs } = state + const members = state.bucketToMembers.get(bucket) ?? new Set([bucket]) const start = Date.now() return new Promise((resolve) => { const check = () => { - const missing = missingFiles(members) + const missing = missingFiles(state, members) if (missing.length === 0) { resolve(undefined) return @@ -440,7 +487,7 @@ function waitForBucket(bucket: string): Promise { // Members stopped arriving. Quiet time alone cannot tell an erased // import from a later Turbopack wave, so serving now could drop a late // member's rules; production builds complete above via prewarming. - if (bundlerQuiet(now)) { + if (bundlerQuiet(state, now)) { resolve( incompleteCssError( `bucket "${bucket}"`, @@ -471,16 +518,75 @@ function waitForBucket(bucket: string): Promise { }) } +// One entry per Next app in this process, keyed by its resolved `distDir`. Each +// app keeps its own engine session, listener and tracking state, so a second +// app never closes the first one's coordinator or reads its sheet. Repeated +// config evaluations of the same app share the entry; it is released when the +// last coordinator started for it closes. +interface CoordinatorApp { + key: string + session: CompilerSession | null + /** The shared engine `session` lives in. */ + engine: DevupWasm | null + server: Server | null + refs: number +} + +const apps = new Map() + +/** @internal The registry key of a `distDir`: absolute and normalized. */ +export function coordinatorKey(distDir: string): string { + const key = resolve(distDir) + return process.platform === 'win32' ? key.toLowerCase() : key +} + +function appFor(distDir: string): CoordinatorApp { + const key = coordinatorKey(distDir) + let app = apps.get(key) + if (!app) { + app = { key, session: null, engine: null, server: null, refs: 0 } + apps.set(key, app) + } + return app +} + +/** + * The engine of the app at `distDir`: one session of its own on the shared + * `compiler`, opened on first use and kept until the app's last coordinator + * closes. Run the app's setup and pass it to `startCoordinator` as `wasm`. + */ +export function openCoordinatorCompiler( + distDir: string, + compiler: DevupWasm, +): DevupWasm { + const app = appFor(distDir) + if (!app.session || app.engine !== compiler) { + // A session lives in one engine: a re-evaluation on another engine + // (`engine` option changed) starts over on that one + app.session?.close() + app.session = openCompilerSession(compiler) + app.engine = compiler + } + return withModuleResolver(app.session.compiler) +} + +function releaseApp(app: CoordinatorApp): void { + app.refs-- + // Every coordinator started for the app has closed its own listener by now + if (app.refs > 0) return + app.session?.close() + apps.delete(app.key) +} + export function startCoordinator(options: CoordinatorOptions): { close: () => void } { + const app = appFor(options.distDir) // Next may evaluate its config more than once in the same process. Close the - // previous listener before replacing it so its HTTP server and request - // closure do not remain live for the rest of the build. - if (server) { - server.close() - server = null - } + // app's previous listener before replacing it so its HTTP server and request + // closure do not remain live for the rest of the build. Other apps' listeners + // stay untouched. + app.server?.close() const { wasm, package: libPackage, @@ -505,21 +611,9 @@ export function startCoordinator(options: CoordinatorOptions): { const extract = options.sourceMap === false ? codeExtractWithoutSourceMap : codeExtract - idleThresholdMs = options.idleThresholdMs ?? 2500 - quietMs = options.quietMs ?? 10_000 - maxWaitMs = options.maxWaitMs ?? 60_000 - canonicalMapRef = options.canonicalMap - bucketToMembers = buildBucketToMembers(options.canonicalMap) - expectedBaseFiles = new Set(options.expectedBaseFiles ?? []) - extractedFiles.clear() - for (const file of options.prewarmedFiles ?? []) extractedFiles.add(file) - extractedSources.clear() - fileNumToBucket.clear() - transformCache.clear() - themeWatch = options.theme && { - ...options.theme, - stamp: fileStamp(options.theme.files), - } + const state = createState(options) + const { extractedFiles, extractedSources, fileNumToBucket, transformCache } = + state // A deleted file is never extracted again, so its styles would stay in the // sheet; its importers are, once they stop importing it. A repeated @@ -562,7 +656,7 @@ export function startCoordinator(options: CoordinatorOptions): { const fileNum = fileNumParam != null ? parseInt(fileNumParam) : undefined try { - refreshTheme() + refreshTheme(state) } catch (error) { res.writeHead(500, { 'Content-Type': 'text/plain' }) res.end(error instanceof Error ? error.message : String(error)) @@ -574,11 +668,11 @@ export function startCoordinator(options: CoordinatorOptions): { fileNum != null && fileNumToBucket.has(fileNum) ? // Deterministic: block until every member of this collapsed // bucket has been extracted, then serve the complete chunk. - await waitForBucket(fileNumToBucket.get(fileNum)!) + await waitForBucket(state, fileNumToBucket.get(fileNum)!) : // Base css (no fileNum) or a bucket no member has reported yet: // wait for the deterministic route-reachable file set (idle // fallback only when that set is unavailable). - await waitForBase() + await waitForBase(state) if (incomplete !== undefined) { // Only builds wait (`next dev` never asks to), and a build bundles // this answer as its stylesheet: fail it rather than ship gaps. @@ -611,15 +705,15 @@ export function startCoordinator(options: CoordinatorOptions): { // window between the request hitting this handler and `activeExtractions++` // below — making it falsely conclude the build is idle even though // more extractions are imminent. - pendingExtractStarts++ + state.pendingExtractStarts++ let promotedToActive = false let extractedFilename: string | undefined try { const bodyStartedAt = profileStart() const body = JSON.parse(await readBody(req)) const bodyDurationMs = elapsedMs(bodyStartedAt) - activeExtractions++ - pendingExtractStarts-- + state.activeExtractions++ + state.pendingExtractStarts-- promotedToActive = true const { filename, code, resourcePath } = body as { filename: string @@ -627,7 +721,7 @@ export function startCoordinator(options: CoordinatorOptions): { resourcePath: string } extractedFilename = filename - refreshTheme() + refreshTheme(state) const repeated = extractedSources.has(filename) extractedSources.set(filename, resourcePath) @@ -647,7 +741,7 @@ export function startCoordinator(options: CoordinatorOptions): { const cached = prewarmed?.source === code ? prewarmed - : cachedExtract(filename, code, relCssDir) + : cachedExtract(state, filename, code, relCssDir) const cacheHit = cached !== undefined let result: ExtractOutputSnapshot if (cached) { @@ -718,7 +812,10 @@ export function startCoordinator(options: CoordinatorOptions): { if (fileNum != null) { // Record this bucket's fileNum -> canonical bucket path so /css can // wait for the bucket's members before serving it. - fileNumToBucket.set(fileNum, canonicalMapRef[filename] ?? filename) + fileNumToBucket.set( + fileNum, + state.canonicalMap[filename] ?? filename, + ) } } @@ -837,17 +934,17 @@ export function startCoordinator(options: CoordinatorOptions): { ) } finally { if (promotedToActive) { - activeExtractions-- + state.activeExtractions-- } else { // readBody/JSON.parse threw before we promoted to active, so the // pending slot is still ours to release. - pendingExtractStarts-- + state.pendingExtractStarts-- } // Mark the file processed (success OR failure) so per-bucket waiters // never hang on a file that errored — fail open, like the idle path. if (extractedFilename != null) extractedFiles.add(extractedFilename) - totalExtractions++ - lastCompletedAt = Date.now() + state.totalExtractions++ + state.lastCompletedAt = Date.now() } return } @@ -856,7 +953,8 @@ export function startCoordinator(options: CoordinatorOptions): { res.end('Not Found') }) - server = coordinatorServer + app.server = coordinatorServer + app.refs++ coordinatorServer.listen(0, '127.0.0.1', () => { const addr = coordinatorServer.address() if (addr && typeof addr !== 'string') { @@ -864,16 +962,20 @@ export function startCoordinator(options: CoordinatorOptions): { } }) + let closed = false return { close: () => { coordinatorServer.close() - if (server === coordinatorServer) { - server = null + if (closed) return + closed = true + if (app.server === coordinatorServer) { + app.server = null removeOwnPortFile(coordinatorPortFile) } // The last CSS and state on disk must hold the final sheet, though // `close` cannot await the writes still pending writePendingSync() + releaseApp(app) }, } } @@ -883,25 +985,11 @@ export const flushCoordinatorWrites = (): Promise => flushPendingWrites() /** @internal Reset coordinator state for testing purposes only */ export const resetCoordinator = () => { - if (server) { - server.close() - server = null + for (const app of apps.values()) { + app.server?.close() + app.session?.close() } - activeExtractions = 0 - totalExtractions = 0 - lastCompletedAt = 0 - pendingExtractStarts = 0 - idleThresholdMs = 2500 - quietMs = 10_000 - maxWaitMs = 60_000 - extractedFiles.clear() - extractedSources.clear() - fileNumToBucket.clear() - bucketToMembers = new Map() - canonicalMapRef = {} - expectedBaseFiles = new Set() - transformCache.clear() - themeWatch = undefined + apps.clear() writeChain.clear() latestContent.clear() writingContent.clear() diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index c46b0aa7e..2a66aa71e 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -32,6 +32,7 @@ import { import { type NextConfig } from 'next' import { + openCoordinatorCompiler, type PrewarmedOutput, startCoordinator, takeExtractOutput, @@ -333,7 +334,10 @@ export function DevupUI( }) if (!existsSync(gitignoreFile)) writeFileSync(gitignoreFile, '*') - const wasm = loadWasm(engine) + // The engine is shared by every Next app in this process; this app works + // on an engine session of its own, so another app's setup or extraction + // never changes its numbering, sheet, theme or settings. + const wasm = openCoordinatorCompiler(distDir, loadWasm(engine)) const { codeExtract, codeExtractWithoutSourceMap, @@ -604,6 +608,7 @@ export function DevupUI( removeStalePortFile(coordinatorPortFile) const coordinator = startCoordinator({ + distDir, wasm, package: libPackage, cssDir, diff --git a/packages/plugin-utils/src/__tests__/compiler-session.test.ts b/packages/plugin-utils/src/__tests__/compiler-session.test.ts new file mode 100644 index 000000000..519695be4 --- /dev/null +++ b/packages/plugin-utils/src/__tests__/compiler-session.test.ts @@ -0,0 +1,78 @@ +import * as wasm from '@devup-ui/wasm' +import { afterEach, describe, expect, it } from 'bun:test' + +import { openCompilerSession } from '../compiler-session' +import type { DevupCompiler } from '../compiler-types' + +const compiler: DevupCompiler = wasm +const pkg = '@devup-ui/react' +const source = `import { Box } from '${pkg}'; export const view = ` + +function extract(target: DevupCompiler, file: string) { + const output = target.codeExtract( + file, + source, + pkg, + 'df', + true, + false, + false, + {}, + ) + try { + return output.code + } finally { + output.free() + } +} + +afterEach(() => { + compiler.resetStateForTesting() +}) + +describe('openCompilerSession', () => { + it('keeps each session apart on one shared compiler', () => { + // Given: two apps, each with its own session and prefix. + const first = openCompilerSession(compiler) + const second = openCompilerSession(compiler) + expect(first.id).not.toBe(second.id) + first.compiler.setPrefix('one-') + second.compiler.setPrefix('two-') + + // When: their calls interleave on the shared compiler. + const one = extract(first.compiler, 'src/one.tsx') + const two = extract(second.compiler, 'src/two.tsx') + const again = extract(first.compiler, 'src/one.tsx') + + // Then: each call ran on its own session's state. + expect(one).toContain('one-') + expect(two).toContain('two-') + expect(again).toBe(one) + expect(first.compiler.getCss(null, false)).not.toContain('two-') + expect(second.compiler.getCss(null, false)).not.toContain('one-') + first.close() + second.close() + }) + + it('reuses one wrapper per function and passes other values through', () => { + // Given: a session over the module namespace. + const session = openCompilerSession(compiler) + + // When / Then: function reads stay stable, other reads are untouched. + expect(session.compiler.getCss).toBe(session.compiler.getCss) + expect(Reflect.get(session.compiler, Symbol.toStringTag)).toBe('Module') + session.close() + }) + + it('drops the session on close so later calls through it throw', () => { + // Given: a closed session. + const session = openCompilerSession(compiler) + session.close() + + // When / Then: selecting it again is refused by the engine. + expect(() => session.compiler.getCss(null, false)).toThrow( + `engine session ${session.id} does not exist`, + ) + expect(compiler.getCss(null, false)).toBeString() + }) +}) diff --git a/packages/plugin-utils/src/__tests__/compiler-types.test.ts b/packages/plugin-utils/src/__tests__/compiler-types.test.ts index c6b757714..adf52c2ff 100644 --- a/packages/plugin-utils/src/__tests__/compiler-types.test.ts +++ b/packages/plugin-utils/src/__tests__/compiler-types.test.ts @@ -78,7 +78,7 @@ describe('compiler structural types', () => { Object.keys(compiler).filter( (key) => key !== 'Output' && key !== 'default', ), - ).toHaveLength(29) + ).toHaveLength(32) } finally { compiler.resetStateForTesting() } diff --git a/packages/plugin-utils/src/__tests__/wasm-abi.test.ts b/packages/plugin-utils/src/__tests__/wasm-abi.test.ts index 45390ebfc..156cf48be 100644 --- a/packages/plugin-utils/src/__tests__/wasm-abi.test.ts +++ b/packages/plugin-utils/src/__tests__/wasm-abi.test.ts @@ -44,6 +44,8 @@ describe('@devup-ui/wasm exports', () => { 'Output', 'codeExtract', 'codeExtractWithoutSourceMap', + 'createSession', + 'dropSession', 'exportCanonicalMap', 'exportClassMap', 'exportFileMap', @@ -67,6 +69,7 @@ describe('@devup-ui/wasm exports', () => { 'resetBuildState', 'resetStateForTesting', 'seedFileMap', + 'selectSession', 'setAtomHoist', 'setDebug', 'setModuleResolver', @@ -394,3 +397,29 @@ describe('@devup-ui/wasm reservation', () => { expect(wasm.getUnreservedClassSlotCount()).toBe(0) }) }) + +describe('@devup-ui/wasm sessions', () => { + it('keeps each session apart until it is dropped', () => { + const prefix = wasm.getPrefix() + const session = wasm.createSession() + wasm.selectSession(session) + expect(wasm.getPrefix()).toBeUndefined() + wasm.setPrefix('app-') + wasm.selectSession(0) + expect(wasm.getPrefix()).toBe(prefix) + expect(() => wasm.selectSession(session + 1)).toThrow( + `engine session ${session + 1} does not exist`, + ) + expect(() => wasm.selectSession(-1)).toThrow( + 'selectSession(): argument id must be a session id', + ) + wasm.selectSession(session) + expect(wasm.getPrefix()).toBe('app-') + expect(wasm.dropSession(session)).toBe(true) + expect(wasm.getPrefix()).toBe(prefix) + expect(wasm.dropSession(session)).toBe(false) + expect(() => wasm.dropSession(0)).toThrow( + 'engine session 0 cannot be dropped', + ) + }) +}) diff --git a/packages/plugin-utils/src/compiler-session.ts b/packages/plugin-utils/src/compiler-session.ts new file mode 100644 index 000000000..3b514efb6 --- /dev/null +++ b/packages/plugin-utils/src/compiler-session.ts @@ -0,0 +1,46 @@ +import type { DevupCompiler } from './compiler-types' + +/** One app's engine session on a shared compiler. */ +export interface CompilerSession { + /** The engine session every call through `compiler` selects first. */ + readonly id: number + /** + * The shared compiler, scoped to this session: each function selects the + * session, then runs. JavaScript is single-threaded and the calls are + * synchronous, so no other session's call can run between the two. + */ + readonly compiler: DevupCompiler + /** Drop the session and its engine state; later calls through `compiler` throw. */ + close(): void +} + +/** + * Open an engine session of its own on `compiler`, so two apps in one process + * never read or change each other's numbering, sheet, theme or settings. + */ +export function openCompilerSession(compiler: DevupCompiler): CompilerSession { + const id = compiler.createSession() + const scoped = new Map() + return { + id, + compiler: new Proxy(compiler, { + get(target, key) { + const value: unknown = Reflect.get(target, key) + if (typeof value !== 'function') return value + let wrapper = scoped.get(key) + if (!wrapper) { + // Read the function at call time, so a later replacement applies + wrapper = function (this: unknown, ...args: unknown[]) { + target.selectSession(id) + return Reflect.apply(Reflect.get(target, key), this, args) + } + scoped.set(key, wrapper) + } + return wrapper + }, + }), + close() { + compiler.dropSession(id) + }, + } +} diff --git a/packages/plugin-utils/src/index.ts b/packages/plugin-utils/src/index.ts index 74abb94b0..2a76a3b24 100644 --- a/packages/plugin-utils/src/index.ts +++ b/packages/plugin-utils/src/index.ts @@ -4,6 +4,7 @@ export { type ResettableEngine, } from './build-session' export { loadCompiler } from './compiler' +export { type CompilerSession, openCompilerSession } from './compiler-session' export type { DevupCompiler, DevupCompilerOutput,