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): refuse --arch without a value instead of building for the host

Christian Schmittel19f57dbeefbf
diff --git a/scripts/build-windows-compositor-addon.mjs b/scripts/build-windows-compositor-addon.mjs
index 5a6da5df4..37a3f55ae 100644
--- a/scripts/build-windows-compositor-addon.mjs
+++ b/scripts/build-windows-compositor-addon.mjs
@@ -19,7 +19,12 @@ import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { findVcVarsAll, run as spawnStep } from "./msvcEnv.mjs";
-import { resolveTargetArch, resolveVcvarsArch, winBinDirName } from "./windows-helper-arch.mjs";
+import {
+ parseArchFlag,
+ resolveTargetArch,
+ resolveVcvarsArch,
+ winBinDirName,
+} from "./windows-helper-arch.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.join(__dirname, "..");
@@ -31,14 +36,8 @@ const BUILD_OUT_DIR = path.join(ROOT, "electron", "native", "compositor-view", "
// app ships. Without this the addon was always built x64 and copied into
// win32-x64/, so an arm64 package either missed it or shipped one the ARM64
// runtime cannot load.
-const cliArch = (() => {
- const eq = process.argv.find((a) => a.startsWith("--arch="));
- if (eq) return eq.slice("--arch=".length);
- const idx = process.argv.indexOf("--arch");
- return idx !== -1 ? process.argv[idx + 1] : undefined;
-})();
const TARGET_ARCH = resolveTargetArch({
- cliArch,
+ cliArch: parseArchFlag(process.argv.slice(2)),
envArch: process.env.OPENSCREEN_WIN_HELPER_ARCH,
hostArch: process.arch,
});
diff --git a/scripts/build-windows-wgc-helper.mjs b/scripts/build-windows-wgc-helper.mjs
index db9b5cb7c..a1f14eb0a 100644
--- a/scripts/build-windows-wgc-helper.mjs
+++ b/scripts/build-windows-wgc-helper.mjs
@@ -5,23 +5,12 @@ import { fileURLToPath } from "node:url";
import { findVcVarsAll, run as spawnStep } from "./msvcEnv.mjs";
import {
+ parseArchFlag,
resolveTargetArch,
resolveVcvarsArch,
winBinDirName,
} from "./windows-helper-arch.mjs";
-function parseArchFlag(argv) {
- const eq = argv.find((a) => a.startsWith("--arch="));
- if (eq) {
- return eq.slice("--arch=".length);
- }
- const idx = argv.indexOf("--arch");
- if (idx !== -1) {
- return argv[idx + 1];
- }
- return undefined;
-}
-
const TARGET_ARCH = resolveTargetArch({
cliArch: parseArchFlag(process.argv.slice(2)),
envArch: process.env.OPENSCREEN_WIN_HELPER_ARCH,
diff --git a/scripts/stage-vcomp-runtime.mjs b/scripts/stage-vcomp-runtime.mjs
index 274d17024..72c8b2fd9 100644
--- a/scripts/stage-vcomp-runtime.mjs
+++ b/scripts/stage-vcomp-runtime.mjs
@@ -35,7 +35,7 @@ import fs from "node:fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { findVcVarsAll } from "./msvcEnv.mjs";
-import { resolveTargetArch, winBinDirName } from "./windows-helper-arch.mjs";
+import { parseArchFlag, resolveTargetArch, winBinDirName } from "./windows-helper-arch.mjs";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.join(__dirname, "..");
@@ -46,12 +46,7 @@ const ROOT = path.join(__dirname, "..");
// would satisfy before-pack's name check and still fail in the loader on the
// target machine.
const TARGET_ARCH = resolveTargetArch({
- cliArch: (() => {
- const eq = process.argv.find((a) => a.startsWith("--arch="));
- if (eq) return eq.slice("--arch=".length);
- const idx = process.argv.indexOf("--arch");
- return idx !== -1 ? process.argv[idx + 1] : undefined;
- })(),
+ cliArch: parseArchFlag(process.argv.slice(2)),
envArch: process.env.OPENSCREEN_WIN_HELPER_ARCH,
hostArch: process.arch,
});
diff --git a/scripts/windows-helper-arch.mjs b/scripts/windows-helper-arch.mjs
index e9f0503a6..e0998acdd 100644
--- a/scripts/windows-helper-arch.mjs
+++ b/scripts/windows-helper-arch.mjs
@@ -19,6 +19,21 @@ export function normalizeArch(value) {
return ALIASES.get(String(value).toLowerCase());
}
+// `--arch` with nothing after it must not read as "flag absent": that would fall
+// through to the host and quietly produce a binary for the wrong architecture.
+export function parseArchFlag(argv) {
+ const eq = argv.find((a) => a.startsWith("--arch="));
+ const idx = argv.indexOf("--arch");
+ if (eq === undefined && idx === -1) {
+ return undefined;
+ }
+ const value = eq !== undefined ? eq.slice("--arch=".length) : argv[idx + 1];
+ if (!value || value.startsWith("-")) {
+ throw new Error(`--arch requires a value (${SUPPORTED_ARCHES.join(" or ")}).`);
+ }
+ return value;
+}
+
export function resolveTargetArch({ cliArch, envArch, hostArch } = {}) {
for (const [label, raw] of [
["--arch", cliArch],
diff --git a/scripts/windows-helper-arch.test.mjs b/scripts/windows-helper-arch.test.mjs
index 078620347..510cfcb20 100644
--- a/scripts/windows-helper-arch.test.mjs
+++ b/scripts/windows-helper-arch.test.mjs
@@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
SUPPORTED_ARCHES,
normalizeArch,
+ parseArchFlag,
resolveTargetArch,
resolveVcvarsArch,
winBinDirName,
@@ -24,6 +25,24 @@ describe("normalizeArch", () => {
});
});
+describe("parseArchFlag", () => {
+ it("reads both spellings", () => {
+ expect(parseArchFlag(["--arch", "arm64"])).toBe("arm64");
+ expect(parseArchFlag(["--arch=arm64"])).toBe("arm64");
+ });
+
+ it("returns undefined when the flag is absent, so the env and host still apply", () => {
+ expect(parseArchFlag([])).toBeUndefined();
+ expect(parseArchFlag(["--clean"])).toBeUndefined();
+ });
+
+ it("refuses a flag without a value instead of silently building for the host", () => {
+ expect(() => parseArchFlag(["--arch"])).toThrow(/requires a value/);
+ expect(() => parseArchFlag(["--arch", "--clean"])).toThrow(/requires a value/);
+ expect(() => parseArchFlag(["--arch="])).toThrow(/requires a value/);
+ });
+});
+
describe("resolveTargetArch", () => {
it("prefers the CLI arch over env and host", () => {
expect(resolveTargetArch({ cliArch: "arm64", envArch: "x64", hostArch: "x64" })).toBe("arm64");