Skip to content
TypeScript

fix(desktop): run the packaged bundle self-check on macOS artifacts (#25900)

Stephen Benjamin1152e9171a9c
diff --git a/scripts/build-desktop-artifact.test.ts b/scripts/build-desktop-artifact.test.ts
index 6ad4413adb5..a4cef9ba03b 100644
--- a/scripts/build-desktop-artifact.test.ts
+++ b/scripts/build-desktop-artifact.test.ts
@@ -3,7 +3,7 @@ import * as NodeCrypto from "node:crypto";
import * as NodePath from "node:path";
import * as NodeServices from "@effect/platform-node/NodeServices";
-import { extractFile } from "@electron/asar";
+import { createPackageWithOptions, extractFile } from "@electron/asar";
import { assert, it } from "@effect/vitest";
import * as ConfigProvider from "effect/ConfigProvider";
import * as Deferred from "effect/Deferred";
@@ -45,7 +45,9 @@ import {
ancestorNodeModulesPaths,
copyDirectoryPreservingSymlinks,
LinuxBrowserSecretHostError,
+ MacPackagedAppMissingError,
stageBrowserSecret,
+ validateMacPackagedPayload,
validateWindowsPackagedPayload,
verifyPackagedBundleIsSelfContained,
WindowsPrimaryNativeProbeError,
@@ -211,6 +213,26 @@ const makeWindowsPayloadFixture = Effect.fn("test.makeWindowsPayloadFixture")(fu
} as const;
});
+const makeMacPayloadFixture = Effect.fn("test.makeMacPayloadFixture")(function* (input: {
+ readonly serverEntrySource?: string;
+}) {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mac-payload-test-" });
+ const sourceDir = path.join(tempDir, "app-source");
+ const serverEntryPath = path.join(sourceDir, "apps/server/dist/bin.mjs");
+ yield* fs.makeDirectory(path.dirname(serverEntryPath), { recursive: true });
+ yield* fs.writeFileString(serverEntryPath, input.serverEntrySource ?? "console.log('server');\n");
+
+ const stageDistDir = path.join(tempDir, "dist");
+ const resourcesDir = path.join(stageDistDir, "mac-arm64/T3 Code.app/Contents/Resources");
+ yield* fs.makeDirectory(resourcesDir, { recursive: true });
+ yield* Effect.promise(() =>
+ createPackageWithOptions(sourceDir, path.join(resourcesDir, "app.asar"), {}),
+ );
+ return { stageDistDir } as const;
+});
+
it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
it.effect("rejects overlapping builds from another process until restoration finishes", () =>
Effect.gen(function* () {
@@ -1283,6 +1305,58 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
),
);
+ it.effect("rejects a macOS app whose packaged server bundle cannot resolve", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const fixture = yield* makeMacPayloadFixture({
+ serverEntrySource: 'import "t3code-deliberately-missing-package";\n',
+ });
+ const error = yield* validateMacPackagedPayload({
+ stageDistDir: fixture.stageDistDir,
+ }).pipe(Effect.flip);
+
+ assert.instanceOf(error, BundleNotSelfContainedError);
+ assert.include(error.output, "t3code-deliberately-missing-package");
+ }),
+ ),
+ );
+
+ it.effect("accepts a macOS app whose packaged server bundle resolves", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const fixture = yield* makeMacPayloadFixture({});
+ yield* validateMacPackagedPayload({ stageDistDir: fixture.stageDistDir });
+ }),
+ ),
+ );
+
+ it.effect("rejects a macOS stage without a packaged app", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const stageDistDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mac-payload-test-" });
+ const error = yield* validateMacPackagedPayload({ stageDistDir }).pipe(Effect.flip);
+
+ assert.instanceOf(error, MacPackagedAppMissingError);
+ }),
+ ),
+ );
+
+ it.effect("rejects a macOS app without app.asar", () =>
+ Effect.scoped(
+ Effect.gen(function* () {
+ const fs = yield* FileSystem.FileSystem;
+ const stageDistDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mac-payload-test-" });
+ yield* fs.makeDirectory(`${stageDistDir}/mac-arm64/T3 Code.app/Contents/Resources`, {
+ recursive: true,
+ });
+ const error = yield* validateMacPackagedPayload({ stageDistDir }).pipe(Effect.flip);
+
+ assert.instanceOf(error, BundleNotSelfContainedError);
+ }),
+ ),
+ );
+
it.effect("leaves the bundle self-check of a cross-built payload to the verify job", () =>
Effect.scoped(
Effect.gen(function* () {
diff --git a/scripts/build-desktop-artifact.ts b/scripts/build-desktop-artifact.ts
index ac2ed7826f7..3a8686f248f 100644
--- a/scripts/build-desktop-artifact.ts
+++ b/scripts/build-desktop-artifact.ts
@@ -3521,6 +3521,42 @@ export const validateWindowsPackagedPayload = Effect.fn(
return { packagedAppDir, fileCount, unpackedFiles } as const;
});
+export class MacPackagedAppMissingError extends Schema.TaggedError<MacPackagedAppMissingError>()(
+ "MacPackagedAppMissingError",
+ { stageDistDir: Schema.String },
+) {
+ override get message(): string {
+ return `Expected electron-builder to leave a packaged .app with Contents/Resources/app.asar under ${this.stageDistDir}/mac*.`;
+ }
+}
+
+export const validateMacPackagedPayload = Effect.fn("desktopArtifact.validateMacPackagedPayload")(
+ function* (input: { readonly stageDistDir: string; readonly verbose?: boolean }) {
+ const fs = yield* FileSystem.FileSystem;
+ const path = yield* Path.Path;
+ const asarPaths: string[] = [];
+ for (const entry of yield* fs.readDirectory(input.stageDistDir)) {
+ if (!entry.startsWith("mac")) continue;
+ const outputDir = path.join(input.stageDistDir, entry);
+ const apps = yield* fs.readDirectory(outputDir).pipe(Effect.orElseSucceed(() => []));
+ for (const app of apps) {
+ if (app.endsWith(".app")) {
+ asarPaths.push(path.join(outputDir, app, "Contents/Resources/app.asar"));
+ }
+ }
+ }
+ if (asarPaths.length === 0) {
+ return yield* new MacPackagedAppMissingError({ stageDistDir: input.stageDistDir });
+ }
+ for (const asarPath of asarPaths) {
+ yield* verifyPackagedBundleIsSelfContained({ asarPath, verbose: input.verbose ?? false });
+ }
+ yield* Effect.log(
+ `[desktop-artifact] Validated macOS payload (${String(asarPaths.length)} app bundle(s)).`,
+ );
+ },
+);
+
const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(function* (
options: ResolvedBuildOptions,
) {
@@ -4016,6 +4052,10 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f
verbose: options.verbose,
});
}
+ // x64 builds on arm64 runners; the arm64 check covers the shared JS bundle.
+ if (options.platform === "mac" && options.arch === "arm64" && hostPlatform === "darwin") {
+ yield* validateMacPackagedPayload({ stageDistDir, verbose: options.verbose });
+ }
const stageEntries = yield* fs.readDirectory(stageDistDir);
yield* fs.makeDirectory(options.outputDir, { recursive: true });