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(mac): write app-update.yml into the .app so it can update itself

EtienneLescot285d61642dcc
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index d97491120..4adf2dbaa 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -503,6 +503,14 @@ jobs:
exit 1
fi
+ # Without it the installed app can never update itself, and nothing else says so: the
+ # probe fails a minute after launch and falls back to the release page (#1431).
+ - name: Verify .app carries app-update.yml
+ run: |
+ FILE="${{ steps.find_app.outputs.app_bundle }}/Contents/Resources/app-update.yml"
+ [[ -f "$FILE" ]] || { echo "::error::${FILE} is missing — scripts/after-pack.cjs did not run"; exit 1; }
+ cat "$FILE"
+
# electron-builder used to do this itself. Its macPackager carried a
# `noIdentity && fallBackToAdhoc` branch that handed back `Identity("-")`
# whenever no certificate was found — mandatory on arm64, where an unsigned
diff --git a/electron-builder.json5 b/electron-builder.json5
index 8b11d8cc7..9b17919cc 100644
--- a/electron-builder.json5
+++ b/electron-builder.json5
@@ -47,6 +47,8 @@
// (unknown scene fields are #[serde(default)]) rather than erroring. See the script header
// and technical-documentation/engineering/build-and-packaging.md.
"beforePack": "scripts/before-pack.cjs",
+ // Writes app-update.yml into the macOS bundle, which `--mac --dir` never gets (#1431).
+ "afterPack": "scripts/after-pack.cjs",
// Standard AppImage updates need .upd_info and a .zsync alongside electron-updater's blockmap.
"artifactBuildCompleted": "scripts/appimage-updates.cjs",
"afterAllArtifactBuild": "scripts/appimage-updates.cjs",
diff --git a/scripts/after-pack.cjs b/scripts/after-pack.cjs
new file mode 100644
index 000000000..a31834549
--- /dev/null
+++ b/scripts/after-pack.cjs
@@ -0,0 +1,34 @@
+// electron-builder afterPack hook: give the macOS bundle the `app-update.yml` electron-updater
+// reads at startup.
+//
+// electron-builder writes that file itself only when the mac targets include `dmg` or `zip`
+// (PublishManager's afterPack handler). The release job packs with `--mac --dir`, so its target
+// list is just `dir` and the file was never written: every shipped `.app` logged `ENOENT …
+// app-update.yml` a minute after launch and every self-update fell back to the release page
+// (#1431). afterPack runs before electron-builder signs the bundle, and before build.yml's ad-hoc
+// signing, so the file is sealed into the signature like any other resource.
+//
+// macOS only on purpose. Windows gets the file from nsis, and the Store (appx) build must NOT get
+// one — see the `publish` comment in electron-builder.json5. Linux writes it per target in fpm.
+
+const fs = require("node:fs");
+const path = require("node:path");
+
+/** The YAML electron-builder would have written: the first publish config + the cache dir. */
+function appUpdateYaml(publish, updaterCacheDirName) {
+ const config = { ...publish, updaterCacheDirName };
+ return Object.entries(config)
+ .map(([key, value]) => `${key}: ${value}\n`)
+ .join("");
+}
+
+exports.default = async function afterPack(context) {
+ if (context.electronPlatformName !== "darwin") return;
+ const { packager } = context;
+ const file = path.join(packager.getResourcesDir(context.appOutDir), "app-update.yml");
+ if (fs.existsSync(file)) return;
+ const [publish] = packager.config.publish ?? [];
+ if (publish == null) throw new Error("afterPack: no publish config to write app-update.yml from");
+ fs.writeFileSync(file, appUpdateYaml(publish, packager.appInfo.updaterCacheDirName));
+ console.log(` • wrote ${path.relative(context.appOutDir, file)}`);
+};
diff --git a/scripts/after-pack.test.mjs b/scripts/after-pack.test.mjs
new file mode 100644
index 000000000..50a5b9e25
--- /dev/null
+++ b/scripts/after-pack.test.mjs
@@ -0,0 +1,61 @@
+// The macOS release job packs with `--dir`, which electron-builder treats as "no updater target"
+// and so writes no app-update.yml (#1431). after-pack.cjs fills that gap; these pin that it writes
+// the file on macOS only, never overwrites electron-builder's own, and writes what electron-updater
+// reads.
+
+import fs from "node:fs";
+import { createRequire } from "node:module";
+import os from "node:os";
+import path from "node:path";
+
+import { afterEach, describe, expect, it } from "vitest";
+
+const require = createRequire(import.meta.url);
+const afterPack = require("./after-pack.cjs").default;
+
+const dirs = [];
+afterEach(() => {
+ for (const dir of dirs.splice(0)) fs.rmSync(dir, { recursive: true, force: true });
+});
+
+function context(electronPlatformName) {
+ const appOutDir = fs.mkdtempSync(path.join(os.tmpdir(), "after-pack-"));
+ dirs.push(appOutDir);
+ const resources = path.join(appOutDir, "Openscreen.app/Contents/Resources");
+ fs.mkdirSync(resources, { recursive: true });
+ return {
+ electronPlatformName,
+ appOutDir,
+ file: path.join(resources, "app-update.yml"),
+ packager: {
+ getResourcesDir: () => resources,
+ config: { publish: [{ provider: "github", owner: "getopenscreen", repo: "openscreen" }] },
+ appInfo: { updaterCacheDirName: "openscreen-updater" },
+ },
+ };
+}
+
+describe("afterPack app-update.yml", () => {
+ it("writes the file into a macOS bundle", async () => {
+ const ctx = context("darwin");
+ await afterPack(ctx);
+ expect(fs.readFileSync(ctx.file, "utf8")).toBe(
+ "provider: github\nowner: getopenscreen\nrepo: openscreen\nupdaterCacheDirName: openscreen-updater\n",
+ );
+ });
+
+ it("leaves electron-builder's own file alone", async () => {
+ const ctx = context("darwin");
+ fs.writeFileSync(ctx.file, "provider: generic\n");
+ await afterPack(ctx);
+ expect(fs.readFileSync(ctx.file, "utf8")).toBe("provider: generic\n");
+ });
+
+ it("writes nothing on Windows or Linux", async () => {
+ for (const platform of ["win32", "linux"]) {
+ const ctx = context(platform);
+ await afterPack(ctx);
+ expect(fs.existsSync(ctx.file)).toBe(false);
+ }
+ });
+});