From fccb4cc6c754fc9e6d2e72e9a9261b816a10b031 Mon Sep 17 00:00:00 2001 From: Ingo Wagner Date: Wed, 23 Sep 2026 13:48:37 +0000 Subject: [PATCH] fix(core): lazy-load child_process to prevent browser bundling - Move spawn import in git-checkout.ts to lazy require() to prevent node:child_process from being included in browser bundles - Add ./git-checkout subpath export in package.json - Update project-workspace.ts to use direct subpath import - Update test mock to match new import path Fixes: Vite crash from child_process externalization error, API crashloop from missing readGitCheckout export --- packages/core/package.json | 5 +++++ packages/core/src/git-checkout.ts | 10 +++++++++- packages/core/src/index.ts | 1 - packages/db/src/project-workspace.test.ts | 2 +- packages/db/src/project-workspace.ts | 2 +- 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/core/package.json b/packages/core/package.json index 379b6dd..5f07ded 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -38,6 +38,11 @@ "types": "./src/node/screen-capability.ts", "development": "./src/node/screen-capability.ts", "default": "./src/node/screen-capability.ts" + }, + "./git-checkout": { + "types": "./src/git-checkout.ts", + "development": "./src/git-checkout.ts", + "default": "./src/git-checkout.ts" } }, "scripts": { diff --git a/packages/core/src/git-checkout.ts b/packages/core/src/git-checkout.ts index 66ed8c0..a0dca20 100644 --- a/packages/core/src/git-checkout.ts +++ b/packages/core/src/git-checkout.ts @@ -1,9 +1,16 @@ -import { spawn } from "node:child_process"; import { access } from "node:fs/promises"; import path from "node:path"; import { gitStatusArgv, gitUntrackedFilesArgv, parseGitNameOnly } from "./compose-update.js"; import { DEFAULT_UPDATE_REMOTE, parseGitStatusPorcelain } from "./self-update.js"; +// Lazy import to avoid bundling `node:child_process` in browser bundles. +// `defaultGitRunner` is server-only; lazy-loading ensures tree-shaking works. +let _spawn: typeof import("node:child_process").spawn | null = null; +const getSpawn = (): typeof import("node:child_process").spawn => { + if (!_spawn) _spawn = require("node:child_process").spawn; + return _spawn; +}; + export interface GitCheckoutStatus { present: boolean; remoteUrl: string | null; @@ -22,6 +29,7 @@ export type GitRunner = (args: string[], cwd: string) => Promise { return new Promise((resolve) => { + const spawn = getSpawn(); const child = spawn("git", args, { cwd, env: process.env }); let output = ""; child.stdout.on("data", (chunk: Buffer | string) => { diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 90fb9f1..9109f99 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -18,7 +18,6 @@ export * from "./computer-updates.js"; export * from "./cron.js"; export * from "./events.js"; export * from "./featured-connectors.js"; -export * from "./git-checkout.js"; export * from "./group-mentions.js"; export * from "./http-response.js"; export * from "./mcp.js"; diff --git a/packages/db/src/project-workspace.test.ts b/packages/db/src/project-workspace.test.ts index 24e0e75..85662b9 100644 --- a/packages/db/src/project-workspace.test.ts +++ b/packages/db/src/project-workspace.test.ts @@ -10,7 +10,7 @@ import { wireProjectWorkspace, } from "./project-workspace.js"; -vi.mock("@rakazo/core", () => ({ +vi.mock("@rakazo/core/git-checkout.js", () => ({ readGitCheckout: vi.fn(async () => ({ present: true, remoteUrl: "https://github.com/example/demo.git", diff --git a/packages/db/src/project-workspace.ts b/packages/db/src/project-workspace.ts index 15fd3a6..5a902dd 100644 --- a/packages/db/src/project-workspace.ts +++ b/packages/db/src/project-workspace.ts @@ -1,6 +1,6 @@ import { access, stat } from "node:fs/promises"; import path from "node:path"; -import { readGitCheckout } from "@rakazo/core"; +import { readGitCheckout } from "@rakazo/core/git-checkout"; import type { PrismaClient } from "./client.js"; export class InvalidProjectWorkspacePathError extends Error {