Skip to content

Self-improving AI Employees for everyone

Keep React Compiler and renderer on a single React runtime

trohitg138207332755
diff --git a/client/src/test/reactRuntime.test.ts b/client/src/test/reactRuntime.test.ts
new file mode 100644
index 00000000..4d9fbd29
--- /dev/null
+++ b/client/src/test/reactRuntime.test.ts
@@ -0,0 +1,60 @@
+// @vitest-environment node
+
+import { cpSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs';
+import { createRequire } from 'node:module';
+import { tmpdir } from 'node:os';
+import { dirname, join, resolve } from 'node:path';
+import { resolveConfig } from 'vite';
+import { expect, it, vi } from 'vitest';
+
+it('renders compiler-generated hooks from a package with its own React copy', async () => {
+ const clientRoot = resolve(import.meta.dirname, '../..');
+ const clientRequire = createRequire(join(clientRoot, 'package.json'));
+ // Keep Vite/esbuild in Node's realm; only the renderer needs a DOM.
+ const { JSDOM } = clientRequire('jsdom');
+ const dom = new JSDOM('<!doctype html><html><body></body></html>');
+ vi.stubGlobal('window', dom.window);
+ vi.stubGlobal('document', dom.window.document);
+ vi.stubGlobal('navigator', dom.window.navigator);
+ vi.stubGlobal('IS_REACT_ACT_ENVIRONMENT', true);
+ vi.stubEnv('VITE_CLIENT_PORT', '5678');
+ vi.stubEnv('PYTHON_BACKEND_PORT', '5679');
+ const fixture = mkdtempSync(join(tmpdir(), 'opencompany-react-runtime-'));
+ const importer = join(fixture, 'HomeShell.js');
+ let unmount: (() => void) | undefined;
+ try {
+ // Linked and isolated packages may resolve a second copy even when the
+ // version matches. Its compiler hook sees a null renderer dispatcher.
+ cpSync(dirname(clientRequire.resolve('react/package.json')), join(fixture, 'node_modules/react'), { recursive: true });
+ writeFileSync(importer, 'export default function HomeShell() {}');
+ const config = await resolveConfig({ configFile: join(clientRoot, 'vite.config.js'), root: clientRoot }, 'serve', 'test');
+ const resolveRuntime = config.createResolver();
+ const compilerPath = await resolveRuntime('react/compiler-runtime', importer);
+ expect(compilerPath).toBeDefined();
+ const compiler = clientRequire(compilerPath!);
+ const React = clientRequire('react') as typeof import('react');
+ const { createRoot } = clientRequire('react-dom/client') as typeof import('react-dom/client');
+ const container = document.createElement('div');
+ const root = createRoot(container);
+ unmount = () => root.unmount();
+ function CompiledHome() {
+ // This is the first hook emitted by React Compiler, before user hooks.
+ compiler.c(1);
+ return React.createElement('p', null, 'Home is ready');
+ }
+ await React.act(async () => root.render(React.createElement(CompiledHome)));
+ expect(container.textContent).toBe('Home is ready');
+ await React.act(async () => root.unmount());
+ unmount = undefined;
+
+ for (const id of ['react', 'react/jsx-runtime', 'react/jsx-dev-runtime', 'react/compiler-runtime', 'react-dom/client']) {
+ expect(await resolveRuntime(id, importer)).toBe(await resolveRuntime(id, join(clientRoot, 'src/main.tsx')));
+ }
+ } finally {
+ unmount?.();
+ rmSync(fixture, { recursive: true, force: true });
+ dom.window.close();
+ vi.unstubAllGlobals();
+ vi.unstubAllEnvs();
+ }
+});
diff --git a/client/vite.config.js b/client/vite.config.js
index d0070117..fcaf07cd 100644
--- a/client/vite.config.js
+++ b/client/vite.config.js
@@ -97,6 +97,9 @@ export default defineConfig(({ mode }) => {
'import.meta.env.VITE_ANDROID_RELAY_URL': JSON.stringify(getEnv('VITE_ANDROID_RELAY_URL', '')),
},
resolve: {
+ // Compiler hooks and React DOM must share one React dispatcher, even
+ // when Bun's isolated or linked packages contain another React copy.
+ dedupe: ['react', 'react-dom'],
alias: {
'@': resolve(process.cwd(), 'src'),
},