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(sdk): finish plugin builds while old Windows addons are loaded (#2502)

Michael D'Angelo8716bbb373b0
diff --git a/sdk/typescript/.gitignore b/sdk/typescript/.gitignore
index 973a09c34..b6b735090 100644
--- a/sdk/typescript/.gitignore
+++ b/sdk/typescript/.gitignore
@@ -1,5 +1,6 @@
/dist/
/_bundled_plugin/
+/.plugin-build-*/
/node_modules
/coverage/
/reports/
diff --git a/sdk/typescript/scripts/build-plugin.mjs b/sdk/typescript/scripts/build-plugin.mjs
index 9adde02a9..47afa5027 100644
--- a/sdk/typescript/scripts/build-plugin.mjs
+++ b/sdk/typescript/scripts/build-plugin.mjs
@@ -134,9 +134,24 @@ export async function buildBundledPlugin({
}
},
);
+ if (preservePrevious && process.platform === "win32") {
+ // A loaded addon can still prevent unlinking. Use the publication backoff
+ // for the whole old tree because recursive rm retries vary by runtime.
+ try {
+ await retryWindowsRename(() =>
+ rm(previous, { recursive: true, force: true }),
+ );
+ } catch (error) {
+ if (!["EPERM", "EACCES", "EBUSY"].includes(error.code)) throw error;
+ console.warn(
+ `Published bundled plugin at ${destination}. Remove staging directory ${staging} after readers exit. ${error.message}`,
+ );
+ return files;
+ }
+ }
preservePrevious = false;
} finally {
- // Leave the previous bundle available if restoring it fails.
+ // Retain the previous bundle if rollback fails or a reader keeps it open.
if (!preservePrevious) await rm(staging, { recursive: true, force: true });
}
diff --git a/sdk/typescript/tests-ts/build-plugin.test.ts b/sdk/typescript/tests-ts/build-plugin.test.ts
index 97de0726b..465b62988 100644
--- a/sdk/typescript/tests-ts/build-plugin.test.ts
+++ b/sdk/typescript/tests-ts/build-plugin.test.ts
@@ -445,6 +445,56 @@ describe("bundled plugin build", () => {
},
);
+ test.skipIf(process.platform !== "win32").each(["released", "held"])(
+ "publishes the bundle while the previous Windows addon is %s",
+ async (mode) => {
+ const root = await temporaryDirectory();
+ const source = join(root, "source");
+ const destination = join(root, "_bundled_plugin");
+ const manifest = ".codex-plugin/plugin.json";
+ const otherStage = ".plugin-build-other";
+ await writeFixture(root, `${otherStage}/keep.txt`, "other build");
+ await writeFixture(source, manifest, "replacement");
+ await writeFixture(destination, manifest, "previous");
+ await writeFixture(
+ source,
+ "contract.json",
+ JSON.stringify({ externalOwnedExact: [manifest], shippedExact: [] }),
+ );
+ const fixture = fileURLToPath(
+ new URL("./fixtures/loaded-bundle-build.mjs", import.meta.url),
+ );
+ const { stdout, stderr } = await execFileAsync("node", [
+ fixture,
+ root,
+ mode,
+ ]);
+ const outcome = JSON.parse(stdout);
+ expect(["EPERM", "EACCES", "EBUSY"]).toContain(outcome.lockCode);
+ expect(outcome.files).toEqual([manifest]);
+ expect(await files(destination)).toEqual([manifest]);
+ expect(await readFile(join(destination, manifest), "utf8")).toBe(
+ "replacement",
+ );
+ expect(await readFile(join(root, otherStage, "keep.txt"), "utf8")).toBe(
+ "other build",
+ );
+ const stages = (await readdir(root)).filter(
+ (name) => name.startsWith(".plugin-build-") && name !== otherStage,
+ );
+ if (mode === "released") {
+ expect(stages).toEqual([]);
+ expect(stderr).toBe("");
+ } else {
+ expect(stages).toHaveLength(1);
+ const stage = join(root, stages[0]!);
+ expect(await files(stage)).toEqual([`previous/${outcome.nativePath}`]);
+ expect(stderr).toContain(stage);
+ expect(stderr).toContain(outcome.lockCode);
+ }
+ },
+ );
+
test.each(["compiler", "file contract"])(
"preserves the previous bundle when the %s fails",
async (failure) => {
diff --git a/sdk/typescript/tests-ts/fixtures/loaded-bundle-build.mjs b/sdk/typescript/tests-ts/fixtures/loaded-bundle-build.mjs
new file mode 100644
index 000000000..75d0d5aca
--- /dev/null
+++ b/sdk/typescript/tests-ts/fixtures/loaded-bundle-build.mjs
@@ -0,0 +1,53 @@
+import { once } from "node:events";
+import fs from "node:fs/promises";
+import { syncBuiltinESMExports } from "node:module";
+import { basename, dirname, join } from "node:path";
+import { Worker } from "node:worker_threads";
+import { buildBundledPlugin } from "../../scripts/build-plugin.mjs";
+
+const [root, mode] = process.argv.slice(2);
+const source = join(root, "source");
+const destination = join(root, "_bundled_plugin");
+const nativePath = `mcp/native/win32-${process.arch}/windows.node`;
+const binding = join(destination, nativePath);
+await fs.mkdir(dirname(binding), { recursive: true });
+await fs.copyFile(
+ new URL(`../../_bundled_plugin/${nativePath}`, import.meta.url),
+ binding,
+);
+
+const reader = new Worker(
+ `const { parentPort, workerData } = require("node:worker_threads");
+ require(workerData);
+ parentPort.on("message", () => {});
+ parentPort.postMessage(process.version);`,
+ { eval: true, execArgv: [], workerData: binding },
+);
+try {
+ const [readerVersion] = await once(reader, "message");
+ let lockCode;
+ const remove = fs.rm;
+ fs.rm = async (...args) => {
+ try {
+ return await remove(...args);
+ } catch (error) {
+ if (
+ error.syscall === "unlink" &&
+ basename(error.path) === "windows.node"
+ ) {
+ lockCode = error.code;
+ if (mode === "released") await reader.terminate();
+ }
+ throw error;
+ }
+ };
+ syncBuiltinESMExports();
+ const files = await buildBundledPlugin({
+ source,
+ destination,
+ contractPath: join(source, "contract.json"),
+ });
+ console.log(JSON.stringify({ files, lockCode, nativePath, readerVersion }));
+} finally {
+ await reader.terminate();
+}