Skip to content

Commit 1152e91

Browse files
Stephen Benjaminclaudet3dotgg
authored
fix(desktop): run the packaged bundle self-check on macOS artifacts (#25900)
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Theo Browne <[email protected]>
1 parent 58c31f2 commit 1152e91

2 files changed

Lines changed: 115 additions & 1 deletion

File tree

‎scripts/build-desktop-artifact.test.ts‎

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as NodeCrypto from "node:crypto";
33
import * as NodePath from "node:path";
44

55
import * as NodeServices from "@effect/platform-node/NodeServices";
6-
import { extractFile } from "@electron/asar";
6+
import { createPackageWithOptions, extractFile } from "@electron/asar";
77
import { assert, it } from "@effect/vitest";
88
import * as ConfigProvider from "effect/ConfigProvider";
99
import * as Deferred from "effect/Deferred";
@@ -45,7 +45,9 @@ import {
4545
ancestorNodeModulesPaths,
4646
copyDirectoryPreservingSymlinks,
4747
LinuxBrowserSecretHostError,
48+
MacPackagedAppMissingError,
4849
stageBrowserSecret,
50+
validateMacPackagedPayload,
4951
validateWindowsPackagedPayload,
5052
verifyPackagedBundleIsSelfContained,
5153
WindowsPrimaryNativeProbeError,
@@ -211,6 +213,26 @@ const makeWindowsPayloadFixture = Effect.fn("test.makeWindowsPayloadFixture")(fu
211213
} as const;
212214
});
213215

216+
const makeMacPayloadFixture = Effect.fn("test.makeMacPayloadFixture")(function* (input: {
217+
readonly serverEntrySource?: string;
218+
}) {
219+
const fs = yield* FileSystem.FileSystem;
220+
const path = yield* Path.Path;
221+
const tempDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mac-payload-test-" });
222+
const sourceDir = path.join(tempDir, "app-source");
223+
const serverEntryPath = path.join(sourceDir, "apps/server/dist/bin.mjs");
224+
yield* fs.makeDirectory(path.dirname(serverEntryPath), { recursive: true });
225+
yield* fs.writeFileString(serverEntryPath, input.serverEntrySource ?? "console.log('server');\n");
226+
227+
const stageDistDir = path.join(tempDir, "dist");
228+
const resourcesDir = path.join(stageDistDir, "mac-arm64/T3 Code.app/Contents/Resources");
229+
yield* fs.makeDirectory(resourcesDir, { recursive: true });
230+
yield* Effect.promise(() =>
231+
createPackageWithOptions(sourceDir, path.join(resourcesDir, "app.asar"), {}),
232+
);
233+
return { stageDistDir } as const;
234+
});
235+
214236
it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
215237
it.effect("rejects overlapping builds from another process until restoration finishes", () =>
216238
Effect.gen(function* () {
@@ -1283,6 +1305,58 @@ it.layer(NodeServices.layer)("build-desktop-artifact", (it) => {
12831305
),
12841306
);
12851307

1308+
it.effect("rejects a macOS app whose packaged server bundle cannot resolve", () =>
1309+
Effect.scoped(
1310+
Effect.gen(function* () {
1311+
const fixture = yield* makeMacPayloadFixture({
1312+
serverEntrySource: 'import "t3code-deliberately-missing-package";\n',
1313+
});
1314+
const error = yield* validateMacPackagedPayload({
1315+
stageDistDir: fixture.stageDistDir,
1316+
}).pipe(Effect.flip);
1317+
1318+
assert.instanceOf(error, BundleNotSelfContainedError);
1319+
assert.include(error.output, "t3code-deliberately-missing-package");
1320+
}),
1321+
),
1322+
);
1323+
1324+
it.effect("accepts a macOS app whose packaged server bundle resolves", () =>
1325+
Effect.scoped(
1326+
Effect.gen(function* () {
1327+
const fixture = yield* makeMacPayloadFixture({});
1328+
yield* validateMacPackagedPayload({ stageDistDir: fixture.stageDistDir });
1329+
}),
1330+
),
1331+
);
1332+
1333+
it.effect("rejects a macOS stage without a packaged app", () =>
1334+
Effect.scoped(
1335+
Effect.gen(function* () {
1336+
const fs = yield* FileSystem.FileSystem;
1337+
const stageDistDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mac-payload-test-" });
1338+
const error = yield* validateMacPackagedPayload({ stageDistDir }).pipe(Effect.flip);
1339+
1340+
assert.instanceOf(error, MacPackagedAppMissingError);
1341+
}),
1342+
),
1343+
);
1344+
1345+
it.effect("rejects a macOS app without app.asar", () =>
1346+
Effect.scoped(
1347+
Effect.gen(function* () {
1348+
const fs = yield* FileSystem.FileSystem;
1349+
const stageDistDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-mac-payload-test-" });
1350+
yield* fs.makeDirectory(`${stageDistDir}/mac-arm64/T3 Code.app/Contents/Resources`, {
1351+
recursive: true,
1352+
});
1353+
const error = yield* validateMacPackagedPayload({ stageDistDir }).pipe(Effect.flip);
1354+
1355+
assert.instanceOf(error, BundleNotSelfContainedError);
1356+
}),
1357+
),
1358+
);
1359+
12861360
it.effect("leaves the bundle self-check of a cross-built payload to the verify job", () =>
12871361
Effect.scoped(
12881362
Effect.gen(function* () {

‎scripts/build-desktop-artifact.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3521,6 +3521,42 @@ export const validateWindowsPackagedPayload = Effect.fn(
35213521
return { packagedAppDir, fileCount, unpackedFiles } as const;
35223522
});
35233523

3524+
export class MacPackagedAppMissingError extends Schema.TaggedError<MacPackagedAppMissingError>()(
3525+
"MacPackagedAppMissingError",
3526+
{ stageDistDir: Schema.String },
3527+
) {
3528+
override get message(): string {
3529+
return `Expected electron-builder to leave a packaged .app with Contents/Resources/app.asar under ${this.stageDistDir}/mac*.`;
3530+
}
3531+
}
3532+
3533+
export const validateMacPackagedPayload = Effect.fn("desktopArtifact.validateMacPackagedPayload")(
3534+
function* (input: { readonly stageDistDir: string; readonly verbose?: boolean }) {
3535+
const fs = yield* FileSystem.FileSystem;
3536+
const path = yield* Path.Path;
3537+
const asarPaths: string[] = [];
3538+
for (const entry of yield* fs.readDirectory(input.stageDistDir)) {
3539+
if (!entry.startsWith("mac")) continue;
3540+
const outputDir = path.join(input.stageDistDir, entry);
3541+
const apps = yield* fs.readDirectory(outputDir).pipe(Effect.orElseSucceed(() => []));
3542+
for (const app of apps) {
3543+
if (app.endsWith(".app")) {
3544+
asarPaths.push(path.join(outputDir, app, "Contents/Resources/app.asar"));
3545+
}
3546+
}
3547+
}
3548+
if (asarPaths.length === 0) {
3549+
return yield* new MacPackagedAppMissingError({ stageDistDir: input.stageDistDir });
3550+
}
3551+
for (const asarPath of asarPaths) {
3552+
yield* verifyPackagedBundleIsSelfContained({ asarPath, verbose: input.verbose ?? false });
3553+
}
3554+
yield* Effect.log(
3555+
`[desktop-artifact] Validated macOS payload (${String(asarPaths.length)} app bundle(s)).`,
3556+
);
3557+
},
3558+
);
3559+
35243560
const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(function* (
35253561
options: ResolvedBuildOptions,
35263562
) {
@@ -4016,6 +4052,10 @@ const buildDesktopArtifactUnlocked = Effect.fn("buildDesktopArtifactUnlocked")(f
40164052
verbose: options.verbose,
40174053
});
40184054
}
4055+
// x64 builds on arm64 runners; the arm64 check covers the shared JS bundle.
4056+
if (options.platform === "mac" && options.arch === "arm64" && hostPlatform === "darwin") {
4057+
yield* validateMacPackagedPayload({ stageDistDir, verbose: options.verbose });
4058+
}
40194059

40204060
const stageEntries = yield* fs.readDirectory(stageDistDir);
40214061
yield* fs.makeDirectory(options.outputDir, { recursive: true });

0 commit comments

Comments
 (0)