Skip to content

OpenAI's Codex Security CLI and TypeScript SDK for finding, validating, and fixing security vulnerabilities. npm: https://www.npmjs.com/package/@openai/codex-security

fix(package): build the bundled plugin in long Windows checkouts (#2508)

Michael D'Angelof3efffee06ab
diff --git a/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs b/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
index e8979962a..84762fb86 100644
--- a/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
+++ b/plugins/codex-security/mcp-app/scripts/build_mcp_app.mjs
@@ -1,14 +1,50 @@
#!/usr/bin/env node
-import { realpathSync } from "node:fs";
+import { realpathSync, statSync } from "node:fs";
import { copyFile, mkdir, readFile, rm, writeFile } from "node:fs/promises";
-import { dirname, join, resolve } from "node:path";
+import { delimiter, dirname, join, resolve } from "node:path";
import { brotliCompressSync, constants as zlibConstants } from "node:zlib";
-import { execFileSync } from "node:child_process";
+import { execFileSync, execSync } from "node:child_process";
import { build } from "esbuild";
import { buildNativeWrappers } from "./build_native_wrappers.mjs";
const root = resolve(import.meta.dirname, "..");
+async function runAppBuild() {
+ const options = { cwd: root, stdio: "inherit" };
+ const binDirectory = join("node_modules", ".bin");
+ if (process.platform !== "win32" || join(root, binDirectory).length < 260) {
+ execFileSync(process.execPath, ["--run", "build"], options);
+ return;
+ }
+ if (process.versions.bun) {
+ // Bun reserves `build` for its bundler unless `run` is explicit.
+ execFileSync(process.execPath, ["run", "build"], options);
+ return;
+ }
+
+ // Node's Windows task runner can skip a long package.json or .bin directory
+ // and select an ancestor's script or compiler.
+ const manifest = join(root, "package.json");
+ const { scripts } = JSON.parse(await readFile(manifest, "utf8"));
+ const bins = [];
+ for (let dir = root; dir !== dirname(dir); dir = dirname(dir)) {
+ const bin = join(dir, binDirectory);
+ if (statSync(bin, { throwIfNoEntry: false })?.isDirectory()) bins.push(bin);
+ }
+ const env = { ...process.env };
+ const pathKey = Object.keys(env).find((key) => key.toLowerCase() === "path");
+ if (pathKey !== undefined)
+ env[pathKey] = [...bins, env[pathKey]].join(delimiter);
+ execSync(scripts.build, {
+ ...options,
+ env: {
+ ...env,
+ NODE_RUN_SCRIPT_NAME: "build",
+ NODE_RUN_PACKAGE_JSON_PATH: manifest,
+ },
+ });
+}
+
export async function buildMcpApp({ output, native = "universal" }) {
if (native !== "universal" && native !== "host") {
throw new Error("Native packaging must be universal or host.");
@@ -38,10 +74,7 @@ export async function buildMcpApp({ output, native = "universal" }) {
throw new Error(`Unsupported native target: ${hostTarget}.`);
}
- execFileSync(process.execPath, ["--run", "build"], {
- cwd: root,
- stdio: "inherit",
- });
+ await runAppBuild();
await rm(mcpDir, { recursive: true, force: true });
await mkdir(mcpDir, { recursive: true });
// A standalone plugin can live on a UNC share. End Node's package lookup
diff --git a/sdk/typescript/scripts/build-plugin.mjs b/sdk/typescript/scripts/build-plugin.mjs
index 47afa5027..33d48feae 100644
--- a/sdk/typescript/scripts/build-plugin.mjs
+++ b/sdk/typescript/scripts/build-plugin.mjs
@@ -1,4 +1,5 @@
import { execFileSync } from "node:child_process";
+import { basename, toNamespacedPath } from "node:path/win32";
import { isDeepStrictEqual } from "node:util";
import {
chmod,
@@ -86,8 +87,15 @@ export async function buildBundledPlugin({
}),
);
- await mkdir(dirname(destination), { recursive: true });
- const staging = await mkdtemp(join(dirname(destination), ".plugin-build-"));
+ const parent = dirname(destination);
+ await mkdir(parent, { recursive: true });
+ const prefix = join(parent, ".plugin-build-");
+ // Windows mkdtemp needs the extended-length prefix. Keep the caller's path
+ // spelling for the compiler, publication, and cleanup.
+ const staging =
+ process.platform === "win32"
+ ? join(parent, basename(await mkdtemp(toNamespacedPath(prefix))))
+ : await mkdtemp(prefix);
const staged = join(staging, "bundle");
const previous = join(staging, "previous");
let preservePrevious = false;
diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.ts
index 465b62988..5e4e69b80 100644
--- a/sdk/typescript/tests-ts/build-plugin.test.ts
+++ b/sdk/typescript/tests-ts/build-plugin.test.ts
@@ -6,6 +6,7 @@ import {
mkdir,
readFile,
readdir,
+ realpath,
stat,
symlink,
writeFile,
@@ -129,7 +130,16 @@ describe("bundled plugin build", () => {
const plugin = fileURLToPath(
new URL("../../../plugins/codex-security/", import.meta.url),
);
- const source = join(root, "plugin");
+ const ordinarySource = join(root, "plugin");
+ const manifestLength = join(
+ ordinarySource,
+ "mcp-app",
+ "package.json",
+ ).length;
+ const source =
+ process.platform === "win32"
+ ? ordinarySource + "p".repeat(Math.max(0, 263 - manifestLength))
+ : ordinarySource;
await cp(join(plugin, "mcp-app"), join(source, "mcp-app"), {
recursive: true,
filter: (path) => !["node_modules", ".preview"].includes(basename(path)),
@@ -196,24 +206,56 @@ describe("bundled plugin build", () => {
process.platform === "win32" ? "@exit /b 91\r\n" : "#!/bin/sh\nexit 91\n",
);
if (process.platform !== "win32") await chmod(join(bin, launcher), 0o755);
+ if (process.platform === "win32") {
+ const manifest = join(source, "mcp-app", "package.json");
+ const pkg = JSON.parse(await readFile(manifest, "utf8"));
+ pkg.scripts.build += " && node scripts/record-build.mjs";
+ await writeFile(manifest, JSON.stringify(pkg));
+ await writeFixture(
+ source,
+ "mcp-app/scripts/record-build.mjs",
+ 'import { writeFileSync } from "node:fs"; writeFileSync("build-receipt.json", JSON.stringify([process.env.NODE_RUN_SCRIPT_NAME, process.env.NODE_RUN_PACKAGE_JSON_PATH]));\n',
+ );
+ await writeFixture(
+ root,
+ "package.json",
+ JSON.stringify({ scripts: { build: 'node -e "process.exit(91)"' } }),
+ );
+ await writeFixture(bin, "tsc.cmd", "@exit /b 92\r\n");
+ }
const destination = join(root, "mcp");
await writeFixture(destination, "helpers.mjs.br.part-000", "stale");
- await execFileAsync(
- "node",
- [
- join(source, "mcp-app", "scripts", "build_mcp_app.mjs"),
- "--output",
- destination,
- ],
- {
- env: {
- ...process.env,
- PATH: [bin, process.env["PATH"]].filter(Boolean).join(delimiter),
- },
+ const buildArgs = [
+ join(source, "mcp-app", "scripts", "build_mcp_app.mjs"),
+ "--output",
+ destination,
+ ];
+ await execFileAsync("node", buildArgs, {
+ env: {
+ ...process.env,
+ PATH: [bin, process.env["PATH"]].filter(Boolean).join(delimiter),
},
- );
+ });
+ if (process.platform === "win32") {
+ const [script, manifest] = JSON.parse(
+ await readFile(join(source, "mcp-app", "build-receipt.json"), "utf8"),
+ );
+ expect(script).toBe("build");
+ expect(await realpath(manifest)).toBe(
+ await realpath(join(source, "mcp-app", "package.json")),
+ );
+ const pkg = JSON.parse(await readFile(manifest, "utf8"));
+ pkg.scripts.build = `node -e "require('node:fs').writeFileSync('bun-build-receipt', process.env.npm_lifecycle_event); process.exit(72)"`;
+ await writeFile(manifest, JSON.stringify(pkg));
+ await expect(
+ execFileAsync(process.execPath, buildArgs),
+ ).rejects.toThrow();
+ expect(
+ await readFile(join(source, "mcp-app", "bun-build-receipt"), "utf8"),
+ ).toBe("build");
+ }
const contract = JSON.parse(
await readFile(
new URL(
@@ -545,7 +587,11 @@ await writeFile(join(output, "unexpected.txt"), "undeclared output");\n`,
);
test("builds from a source snapshot without Git metadata", async () => {
- const root = await temporaryDirectory();
+ const temporary = await temporaryDirectory();
+ const root =
+ process.platform === "win32"
+ ? join(temporary, "p".repeat(Math.max(1, 260 - temporary.length)))
+ : temporary;
const packageRoot = join(root, "sdk", "typescript");
const source = join(root, "plugins", "codex-security");
@@ -571,6 +617,11 @@ await writeFile(join(output, "unexpected.txt"), "undeclared output");\n`,
"utf8",
),
).toBe("#!/bin/sh\nexit 0\n");
+ expect(
+ (await readdir(packageRoot)).filter((name) =>
+ name.startsWith(".plugin-build-"),
+ ),
+ ).toEqual([]);
});
test("stages only declared runtime files from the canonical plugin source", async () => {