Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(build): re-fetch a staged ONNX Runtime that is the right version from the wrong archive

Etienne Lescotfe5f9dc18cac
diff --git a/scripts/fetch-onnxruntime.mjs b/scripts/fetch-onnxruntime.mjs
index b97af4d40..b5a7ddb2c 100644
--- a/scripts/fetch-onnxruntime.mjs
+++ b/scripts/fetch-onnxruntime.mjs
@@ -38,7 +38,7 @@ import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
-import { fileURLToPath } from "node:url";
+import { fileURLToPath, pathToFileURL } from "node:url";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.join(__dirname, "..");
@@ -275,6 +275,43 @@ async function download(spec) {
return tmp;
}
+/**
+ * Where the fast path learns WHICH archive a staged library came from.
+ *
+ * The version string cannot tell two builds of one release apart, and on darwin-arm64
+ * that is the whole difference: Microsoft's osx-arm64 1.27.1 and the floor-pinned one
+ * built here both carry `\01.27.1\0`, but only the second passes before-pack.cjs's
+ * macOS floor. A checkout that staged the upstream library kept hearing "Already
+ * present" and then failed to package. `spec.sha256` is the ARCHIVE's digest, so the
+ * staged file cannot be compared to it directly — this record ties the two together.
+ *
+ * Beside the per-target directories rather than inside them: every `extraResources`
+ * filter in electron-builder.json5 ships the contents of `<platform>-<arch>/` only.
+ */
+export const stampPathFor = (tag) =>
+ path.join(ROOT, "electron", "native", "bin", `.onnxruntime-${tag}.json`);
+
+const sha256Of = (file) => crypto.createHash("sha256").update(fs.readFileSync(file)).digest("hex");
+
+/** Records that `dest` was vendored from the archive `spec` pins. */
+export function writeStamp(dest, stamp, spec) {
+ const record = { archive: spec.sha256, library: sha256Of(dest) };
+ fs.writeFileSync(stamp, `${JSON.stringify(record, null, "\t")}\n`);
+}
+
+/** Why the library at `dest` is not the one `spec` pins, or null when it is. */
+export function stagedMismatch(dest, stamp, spec) {
+ let record;
+ try {
+ record = JSON.parse(fs.readFileSync(stamp, "utf8"));
+ } catch {
+ return "no record of the archive it came from";
+ }
+ if (record?.archive !== spec.sha256) return "vendored from a different archive";
+ if (record.library !== sha256Of(dest)) return "replaced since it was vendored";
+ return null;
+}
+
async function main() {
// `--target` exists for CI, which provisions for the runner it is on; without it
// the host is the target, which is what every local build wants.
@@ -311,21 +348,24 @@ async function main() {
const binDir = path.join(ROOT, "electron", "native", "bin", tag);
const dest = path.join(binDir, spec.out);
+ const stamp = stampPathFor(tag);
+
if (fs.existsSync(dest) && !process.argv.includes("--force")) {
// Re-verify rather than trusting the filename: this directory is gitignored
- // scratch space that a half-finished run or a hand copy can leave anything in.
- const buf = fs.readFileSync(dest);
- const magic = MAGIC[targetPlatform];
- const looksRight =
- magic.bytes.every((b, i) => buf[i] === b) &&
- buf.includes(Buffer.from(`\0${VERSION}\0`, "latin1"));
- if (looksRight) {
+ // scratch space that a half-finished run, a hand copy or an older pin can leave
+ // anything in — including the right version from the wrong archive.
+ const why = stagedMismatch(dest, stamp, spec);
+ if (!why) {
console.log(`Already present: ${dest}`);
- console.log(` ONNX Runtime ${VERSION}, ${(buf.length / 1048576).toFixed(1)} MB`);
+ console.log(
+ ` ONNX Runtime ${VERSION} from ${assetName(spec)}, ${(fs.statSync(dest).size / 1048576).toFixed(1)} MB`,
+ );
console.log("Use --force to re-download.");
return;
}
- console.log(`Present but not ONNX Runtime ${VERSION} — re-fetching: ${dest}`);
+ console.log(
+ `Present but not the pinned ONNX Runtime ${VERSION} (${why}) — re-fetching: ${dest}`,
+ );
}
const tmp = await download(spec);
@@ -343,6 +383,7 @@ async function main() {
fs.mkdirSync(binDir, { recursive: true });
fs.copyFileSync(lib, dest);
if (targetPlatform !== "win32") fs.chmodSync(dest, 0o755);
+ writeStamp(dest, stamp, spec);
console.log(` ${banner}`);
// Où que viennent les octets, dire de quelle source ils sortent. Pour un artefact
@@ -358,7 +399,11 @@ async function main() {
}
}
-main().catch((err) => {
- console.error(`\n${err.message}`);
- process.exit(1);
-});
+// Only when run as a CLI — importing this from a test must not start downloading.
+// realpath because Node resolves the entry point through symlinks before naming it.
+if (process.argv[1] && import.meta.url === pathToFileURL(fs.realpathSync(process.argv[1])).href) {
+ main().catch((err) => {
+ console.error(`\n${err.message}`);
+ process.exit(1);
+ });
+}
diff --git a/scripts/fetch-onnxruntime.test.mjs b/scripts/fetch-onnxruntime.test.mjs
index 6424e4c7f..a6c7e6eb4 100644
--- a/scripts/fetch-onnxruntime.test.mjs
+++ b/scripts/fetch-onnxruntime.test.mjs
@@ -16,14 +16,15 @@
// Neither direction is visible in review — the two lines are in different files, in
// different languages, edited by different tasks. This test is the thing that sees it.
//
-// Read as source text rather than imported: fetch-onnxruntime.mjs calls main() at
-// import and would start downloading. The property under test is a property of the
-// literal table anyway.
+// The pin table is read as source text: the property under test is a property of the
+// literal table. The staged-library check is imported — main() only runs as a CLI.
import fs from "node:fs";
+import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
-import { describe, expect, it } from "vitest";
+import { afterEach, beforeEach, describe, expect, it } from "vitest";
+import { stagedMismatch, stampPathFor, writeStamp } from "./fetch-onnxruntime.mjs";
const HERE = path.dirname(fileURLToPath(import.meta.url));
const source = fs.readFileSync(path.join(HERE, "fetch-onnxruntime.mjs"), "utf8");
@@ -114,3 +115,60 @@ describe("fetch-onnxruntime pins", () => {
expect(source).toMatch(/tag === "darwin-x64"/);
});
});
+
+// The version string cannot tell two builds of one release apart: Microsoft's osx-arm64
+// 1.27.1 (minos 14.0) and the floor-pinned one built here (minos 13.0) both carry it, and
+// a checkout that had staged the first kept hearing "Already present" until
+// before-pack.cjs refused to package it.
+describe("fetch-onnxruntime staged library", () => {
+ const pinned = { sha256: "a".repeat(64) };
+ let dir;
+ let dest;
+ let stamp;
+
+ /** A library the version-string check alone accepted: Mach-O magic + `\0VERSION\0`. */
+ const stage = (build) =>
+ fs.writeFileSync(
+ dest,
+ Buffer.concat([
+ Buffer.from([0xcf, 0xfa, 0xed, 0xfe]),
+ Buffer.from(`\0${version}\0${build}`, "latin1"),
+ ]),
+ );
+
+ beforeEach(() => {
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), "openscreen-ort-test-"));
+ dest = path.join(dir, "libonnxruntime.dylib");
+ stamp = path.join(dir, ".onnxruntime-darwin-arm64.json");
+ });
+ afterEach(() => fs.rmSync(dir, { recursive: true, force: true }));
+
+ it("keeps the library it vendored", () => {
+ stage("floor-pinned");
+ writeStamp(dest, stamp, pinned);
+ expect(stagedMismatch(dest, stamp, pinned)).toBeNull();
+ });
+
+ it("re-fetches the right version string with no record of its archive", () => {
+ stage("upstream");
+ expect(stagedMismatch(dest, stamp, pinned)).toMatch(/no record/);
+ });
+
+ it("re-fetches the right version string vendored from another archive", () => {
+ stage("upstream");
+ writeStamp(dest, stamp, { sha256: "b".repeat(64) });
+ expect(stagedMismatch(dest, stamp, pinned)).toMatch(/different archive/);
+ });
+
+ it("re-fetches the right version string copied over a vendored library", () => {
+ stage("floor-pinned");
+ writeStamp(dest, stamp, pinned);
+ stage("upstream");
+ expect(stagedMismatch(dest, stamp, pinned)).toMatch(/replaced/);
+ });
+
+ it("keeps the stamp out of the per-target directories electron-builder ships", () => {
+ const bin = path.join(HERE, "..", "electron", "native", "bin");
+ expect(path.dirname(stampPathFor("darwin-arm64"))).toBe(bin);
+ });
+});