Skip to content

Commit c9db878

Browse files
committed
fix(desktop): prepare macOS x64 bytecode under Rosetta
- Enable Rosetta on ARM64 macOS release hosts - Generate and test prepared bytecode for macOS x64 builds
1 parent 8657eec commit c9db878

4 files changed

Lines changed: 78 additions & 3 deletions

File tree

‎.github/workflows/release-desktop.yml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,14 @@ jobs:
203203
native/hyprland-snap-shot/target/${{ inputs.rust_target }}/release/t3-hyprland-snap-shot
204204
key: linux-capture-helpers-${{ inputs.rust_target }}-${{ hashFiles('native/kde-snap-shot/Cargo.lock', 'native/kde-snap-shot/Cargo.toml', 'native/kde-snap-shot/src/**', 'native/hyprland-snap-shot/Cargo.lock', 'native/hyprland-snap-shot/Cargo.toml', 'native/hyprland-snap-shot/src/**', 'native/hyprland-snap-shot/protocols/**') }}
205205

206+
- name: Ensure Rosetta for macOS x64 bytecode generation
207+
if: inputs.platform == 'mac' && inputs.arch == 'x64'
208+
shell: bash
209+
run: |
210+
if [[ "$(uname -m)" == "arm64" ]] && ! /usr/bin/arch -x86_64 /usr/bin/true; then
211+
sudo /usr/sbin/softwareupdate --install-rosetta --agree-to-license
212+
fi
213+
206214
- name: Setup Rust
207215
if: steps.resource_monitor_cache.outputs.cache-hit != 'true' || (inputs.platform == 'linux' && steps.capture_helper_cache.outputs.cache-hit != 'true')
208216
uses: dtolnay/rust-toolchain@6bed0761d98439e5a578e2877258200ad565ba87 # stable

‎docs/operations/development.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,9 @@ options.
265265

266266
Native macOS and Windows desktop artifact builds generate server bytecode with the packaged
267267
Electron executable, after ASAR creation and before signing. Linux builds skip generation until
268-
native packaging and relocated AppImage reuse have been validated. Cross-platform and
269-
cross-architecture builds also skip generation explicitly. CLI, SEA and WSL runtimes keep their
268+
native packaging and relocated AppImage reuse have been validated. macOS x64 builds on ARM64
269+
hosts run the packaged x64 Electron under Rosetta to generate the cache. Other cross-platform and
270+
cross-architecture builds skip generation explicitly. CLI, SEA and WSL runtimes keep their
270271
existing behavior; only desktop backend imports covered by generation receive prepared bytecode.
271272
Worker-only modules compile normally.
272273

‎scripts/lib/server-compile-cache.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,64 @@ it.each(["x64", "arm64"])("skips prepared bytecode generation on Linux %s", asyn
2525
expect(result.stdout).toContain("Skipping unsupported target linux");
2626
});
2727

28+
it("prepares macOS x64 cache on an ARM64 build host", async () => {
29+
const { spawnSync } = await import("node:child_process");
30+
const root = NodeFS.realpathSync(
31+
NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-rosetta-cache-")),
32+
);
33+
try {
34+
const resources = NodePath.join(root, "Fixture.app", "Contents", "Resources");
35+
const archive = NodePath.join(resources, "app.asar");
36+
const dist = NodePath.join(archive, "apps/server/dist");
37+
const helper = NodePath.join(archive, "apps/desktop/dist-electron/serverCompileCache.cjs");
38+
NodeFS.mkdirSync(dist, { recursive: true });
39+
NodeFS.mkdirSync(NodePath.dirname(helper), { recursive: true });
40+
NodeFS.writeFileSync(NodePath.join(dist, "binCli-fixture.mjs"), "export const answer = 42;");
41+
NodeFS.writeFileSync(
42+
helper,
43+
`module.exports = require(${JSON.stringify(NodeURL.fileURLToPath(new URL("./server-compile-cache.ts", import.meta.url)))});`,
44+
);
45+
const hook = NodeURL.fileURLToPath(
46+
new URL("../prepare-server-compile-cache.cjs", import.meta.url),
47+
);
48+
// The fixture uses Node for generation; the hook still sees an ARM64 macOS build host.
49+
const result = spawnSync(
50+
process.execPath,
51+
[
52+
"-e",
53+
`
54+
const childProcess = require("node:child_process");
55+
const spawn = childProcess.spawnSync;
56+
childProcess.spawnSync = (executable, args, options) => spawn(process.execPath, args, options);
57+
Object.defineProperty(process, "platform", { value: "darwin" });
58+
Object.defineProperty(process, "arch", { value: "arm64" });
59+
require(${JSON.stringify(hook)})({
60+
appOutDir: ${JSON.stringify(root)}, electronPlatformName: "darwin", arch: 1,
61+
packager: { appInfo: { productFilename: "Fixture" } }
62+
}).catch(error => { console.error(error); process.exitCode = 1; });
63+
`,
64+
],
65+
{ encoding: "utf8" },
66+
);
67+
expect(result.status, result.stderr).toBe(0);
68+
const manifest = JSON.parse(
69+
NodeFS.readFileSync(NodePath.join(resources, "server-compile-cache/manifest.json"), "utf8"),
70+
);
71+
expect(manifest.entries).toEqual([
72+
{
73+
module: "app.asar/apps/server/dist/binCli-fixture.mjs",
74+
file: expect.stringMatching(/^[a-f0-9]{8}$/),
75+
},
76+
]);
77+
expect(
78+
NodeFS.statSync(NodePath.join(resources, "server-compile-cache", manifest.entries[0].file))
79+
.size,
80+
).toBeGreaterThan(0);
81+
} finally {
82+
NodeFS.rmSync(root, { recursive: true, force: true });
83+
}
84+
});
85+
2886
it("seeds relocated cache entries without allowing writes into shipped resources", () => {
2987
const root = NodeFS.realpathSync(
3088
NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-prepared-test-")),

‎scripts/prepare-server-compile-cache.cjs‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,15 @@ module.exports = async ({ appOutDir, electronPlatformName, arch, packager }) =>
1212
return;
1313
}
1414
const targetArch = ["ia32", "x64", "armv7l", "arm64", "universal"][arch];
15-
if (electronPlatformName !== process.platform || targetArch !== process.arch) {
15+
const runsWithRosetta =
16+
electronPlatformName === "darwin" &&
17+
process.platform === "darwin" &&
18+
process.arch === "arm64" &&
19+
targetArch === "x64";
20+
if (
21+
electronPlatformName !== process.platform ||
22+
(targetArch !== process.arch && !runsWithRosetta)
23+
) {
1624
console.log(
1725
`[server-compile-cache] Skipping unexecutable target ${electronPlatformName}/${targetArch}`,
1826
);

0 commit comments

Comments
 (0)