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
This commit is contained in:
Ingo Wagner 2026-09-23 13:48:37 +00:00
parent bd3c6c2d47
commit fccb4cc6c7
5 changed files with 16 additions and 4 deletions

View File

@ -38,6 +38,11 @@
"types": "./src/node/screen-capability.ts", "types": "./src/node/screen-capability.ts",
"development": "./src/node/screen-capability.ts", "development": "./src/node/screen-capability.ts",
"default": "./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": { "scripts": {

View File

@ -1,9 +1,16 @@
import { spawn } from "node:child_process";
import { access } from "node:fs/promises"; import { access } from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { gitStatusArgv, gitUntrackedFilesArgv, parseGitNameOnly } from "./compose-update.js"; import { gitStatusArgv, gitUntrackedFilesArgv, parseGitNameOnly } from "./compose-update.js";
import { DEFAULT_UPDATE_REMOTE, parseGitStatusPorcelain } from "./self-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 { export interface GitCheckoutStatus {
present: boolean; present: boolean;
remoteUrl: string | null; remoteUrl: string | null;
@ -22,6 +29,7 @@ export type GitRunner = (args: string[], cwd: string) => Promise<GitRunnerResult
export async function defaultGitRunner(args: string[], cwd: string): Promise<GitRunnerResult> { export async function defaultGitRunner(args: string[], cwd: string): Promise<GitRunnerResult> {
return new Promise((resolve) => { return new Promise((resolve) => {
const spawn = getSpawn();
const child = spawn("git", args, { cwd, env: process.env }); const child = spawn("git", args, { cwd, env: process.env });
let output = ""; let output = "";
child.stdout.on("data", (chunk: Buffer | string) => { child.stdout.on("data", (chunk: Buffer | string) => {

View File

@ -18,7 +18,6 @@ export * from "./computer-updates.js";
export * from "./cron.js"; export * from "./cron.js";
export * from "./events.js"; export * from "./events.js";
export * from "./featured-connectors.js"; export * from "./featured-connectors.js";
export * from "./git-checkout.js";
export * from "./group-mentions.js"; export * from "./group-mentions.js";
export * from "./http-response.js"; export * from "./http-response.js";
export * from "./mcp.js"; export * from "./mcp.js";

View File

@ -10,7 +10,7 @@ import {
wireProjectWorkspace, wireProjectWorkspace,
} from "./project-workspace.js"; } from "./project-workspace.js";
vi.mock("@rakazo/core", () => ({ vi.mock("@rakazo/core/git-checkout.js", () => ({
readGitCheckout: vi.fn(async () => ({ readGitCheckout: vi.fn(async () => ({
present: true, present: true,
remoteUrl: "https://github.com/example/demo.git", remoteUrl: "https://github.com/example/demo.git",

View File

@ -1,6 +1,6 @@
import { access, stat } from "node:fs/promises"; import { access, stat } from "node:fs/promises";
import path from "node:path"; import path from "node:path";
import { readGitCheckout } from "@rakazo/core"; import { readGitCheckout } from "@rakazo/core/git-checkout";
import type { PrismaClient } from "./client.js"; import type { PrismaClient } from "./client.js";
export class InvalidProjectWorkspacePathError extends Error { export class InvalidProjectWorkspacePathError extends Error {