Skip to content

Commit 499e3aa

Browse files
fix(apple): pick the Mac runner and simulator build arch from the host CPU (callstack#3047)
1 parent 19dc639 commit 499e3aa

16 files changed

Lines changed: 157 additions & 38 deletions

File tree

‎packages/host-kit/src/internal/host-cpu-arch.test.ts‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import assert from 'node:assert/strict';
22
import { test } from 'vitest';
33
import { type CommandExecutorOverride, withCommandExecutorOverride } from './exec.ts';
4-
import { resolveHostCpuArch } from './host-cpu-arch.ts';
4+
import { readHostCpuArch, readHostCpuArchSync, resolveHostCpuArch } from './host-cpu-arch.ts';
55

66
function sysctlAnswering(result: { stdout?: string; exitCode?: number } | Error) {
77
const calls: string[][] = [];
@@ -45,3 +45,20 @@ test('other platforms report the Node arch without running sysctl', async () =>
4545
});
4646
assert.deepEqual(sysctl.calls, []);
4747
});
48+
49+
test('the per-process value a Rosetta-translated process resolves is the one sync callers read', async () => {
50+
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
51+
const arch = Object.getOwnPropertyDescriptor(process, 'arch')!;
52+
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
53+
Object.defineProperty(process, 'arch', { ...arch, value: 'x64' });
54+
try {
55+
const sysctl = sysctlAnswering({ stdout: '1\n' });
56+
const resolved = await withCommandExecutorOverride(sysctl.override, () => readHostCpuArch());
57+
assert.equal(resolved, 'arm64');
58+
assert.equal(readHostCpuArchSync(), 'arm64');
59+
assert.equal(sysctl.calls.length, 1);
60+
} finally {
61+
Object.defineProperty(process, 'platform', platform);
62+
Object.defineProperty(process, 'arch', arch);
63+
}
64+
});

‎packages/host-kit/src/internal/host-cpu-arch.ts‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,67 @@
1-
import { runCmd } from './exec.ts';
1+
import { runCmd, runCmdSync } from './exec.ts';
22

33
const SYSCTL_TIMEOUT_MS = 1_000;
4+
const APPLE_SILICON_SYSCTL = ['-n', 'hw.optional.arm64'] as const;
45

56
let hostCpuArch: Promise<string> | undefined;
7+
let settledHostCpuArch: string | undefined;
68

79
/**
810
* The machine's native CPU architecture in Apple naming (`arm64`, `x86_64`), which is what
911
* simulators on a Mac run by default. Other CPUs keep Node's `process.arch` name. Resolved once
1012
* per process.
1113
*/
1214
export function readHostCpuArch(): Promise<string> {
13-
hostCpuArch ??= resolveHostCpuArch(process.platform, process.arch);
15+
hostCpuArch ??=
16+
settledHostCpuArch !== undefined
17+
? Promise.resolve(settledHostCpuArch)
18+
: resolveHostCpuArch(process.platform, process.arch).then(
19+
(arch) => (settledHostCpuArch ??= arch),
20+
);
1421
return hostCpuArch;
1522
}
1623

24+
/**
25+
* {@link readHostCpuArch} for a caller that cannot await. It shares the same per-process value,
26+
* resolving it with a blocking `sysctl` only when nothing has resolved it yet.
27+
*/
28+
export function readHostCpuArchSync(): string {
29+
settledHostCpuArch ??= hostCpuArchName(
30+
process.platform === 'darwin' && isAppleSiliconMacSync(),
31+
process.arch,
32+
);
33+
return settledHostCpuArch;
34+
}
35+
1736
export async function resolveHostCpuArch(
1837
platform: NodeJS.Platform,
1938
nodeArch: string,
2039
): Promise<string> {
21-
if (platform === 'darwin' && (await isAppleSiliconMac())) return 'arm64';
40+
return hostCpuArchName(platform === 'darwin' && (await isAppleSiliconMac()), nodeArch);
41+
}
42+
43+
function hostCpuArchName(appleSilicon: boolean, nodeArch: string): string {
44+
if (appleSilicon) return 'arm64';
2245
return nodeArch === 'x64' ? 'x86_64' : nodeArch;
2346
}
2447

2548
// macOS: `hw.optional.arm64` is 1 on Apple silicon even inside a Rosetta-translated process,
2649
// where `process.arch` reports x64; Intel Macs do not define the key.
2750
async function isAppleSiliconMac(): Promise<boolean> {
2851
try {
29-
const result = await runCmd('/usr/sbin/sysctl', ['-n', 'hw.optional.arm64'], {
52+
const result = await runCmd('/usr/sbin/sysctl', APPLE_SILICON_SYSCTL, {
53+
allowFailure: true,
54+
timeoutMs: SYSCTL_TIMEOUT_MS,
55+
});
56+
return result.exitCode === 0 && result.stdout.trim() === '1';
57+
} catch {
58+
return false;
59+
}
60+
}
61+
62+
function isAppleSiliconMacSync(): boolean {
63+
try {
64+
const result = runCmdSync('/usr/sbin/sysctl', APPLE_SILICON_SYSCTL, {
3065
allowFailure: true,
3166
timeoutMs: SYSCTL_TIMEOUT_MS,
3267
});

‎packages/host-kit/src/process.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export {
2525
writeHostStderr,
2626
} from './internal/host-process.ts';
2727
export { signalProcessGroupBestEffort } from './internal/exec.ts';
28-
export { readHostCpuArch } from './internal/host-cpu-arch.ts';
28+
export { readHostCpuArch, readHostCpuArchSync } from './internal/host-cpu-arch.ts';
2929
export { reapOwnedProcessRecordsAtStartup } from './internal/owned-process-reaper.ts';
3030
export {
3131
createOwnedProcessRecordStore,
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import assert from 'node:assert/strict';
2+
import { test } from 'vitest';
3+
import { withCommandExecutorOverride } from '@agent-device/host-kit/command';
4+
import { readHostCpuArch } from '@agent-device/host-kit/process';
5+
import '../runner-client.ts';
6+
import {
7+
resolveRunnerBuildDestination,
8+
resolveRunnerBuildDestinationFamily,
9+
resolveRunnerDestination,
10+
} from '../../runner/apple-runner-platform.ts';
11+
import { MACOS_DEVICE } from '../../runner/__tests__/device-fixtures.ts';
12+
13+
test('the macOS runner destinations name arm64 when Node runs as x64 under Rosetta', async () => {
14+
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
15+
const arch = Object.getOwnPropertyDescriptor(process, 'arch')!;
16+
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
17+
Object.defineProperty(process, 'arch', { ...arch, value: 'x64' });
18+
try {
19+
await withCommandExecutorOverride(
20+
async (command, args) => {
21+
assert.deepEqual([command, ...args], ['/usr/sbin/sysctl', '-n', 'hw.optional.arm64']);
22+
return { stdout: '1\n', stderr: '', exitCode: 0 };
23+
},
24+
() => readHostCpuArch(),
25+
);
26+
for (const destination of [
27+
resolveRunnerDestination(MACOS_DEVICE),
28+
resolveRunnerBuildDestination(MACOS_DEVICE),
29+
resolveRunnerBuildDestinationFamily(MACOS_DEVICE),
30+
]) {
31+
assert.equal(destination, 'platform=macOS,arch=arm64');
32+
}
33+
} finally {
34+
Object.defineProperty(process, 'platform', platform);
35+
Object.defineProperty(process, 'arch', arch);
36+
}
37+
});

‎packages/platform-apple/src/core/runner-host.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
signalPidsBestEffort,
1919
signalProcessGroupBestEffort,
2020
classifyOwnerLiveness,
21+
readHostCpuArchSync,
2122
} from '@agent-device/host-kit/process';
2223
import { Deadline, isEnvTruthy, retryWithPolicy } from '@agent-device/host-kit/retry';
2324

@@ -82,6 +83,7 @@ export const appleRunnerHost: AppleRunnerHost = {
8283
withKeyedLock,
8384
publishFileSync,
8485
classifyOwnerLiveness,
86+
readHostCpuArchSync,
8587
createTtlMemo,
8688
parseBooleanLiteral,
8789
isRecord,

‎packages/platform-apple/src/foldable/fold-helper-cache.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ function fakeFoldHelperHost(
2121
const real = createSnapshotSourceHost();
2222
return {
2323
...real,
24+
cpuArch: async () => 'arm64',
2425
run: async (command, args) => {
2526
if (command === 'xcrun' && args.includes('clang')) {
2627
const outputPath = args.at(-1)!;
@@ -34,9 +35,7 @@ function fakeFoldHelperHost(
3435
? args.includes('-buildVersion')
3536
? '24G90'
3637
: '15.6'
37-
: command === 'uname'
38-
? 'arm64'
39-
: '';
38+
: '';
4039
return { stdout, stderr: '', exitCode: 0 };
4140
},
4241
};

‎packages/platform-apple/src/native-build/host.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ import {
1010
renameHostPath,
1111
writeHostTextFile,
1212
} from '@agent-device/host-kit/host-file';
13-
import { hostProcessId, readProcessStartTime } from '@agent-device/host-kit/process';
13+
import {
14+
hostProcessId,
15+
readHostCpuArch,
16+
readProcessStartTime,
17+
} from '@agent-device/host-kit/process';
1418
import { NativeBuildError, nativeBuildError } from './errors.ts';
1519
import { remainingNativeBuildMs, type NativeBuildDeadline } from './deadline.ts';
1620

@@ -35,6 +39,7 @@ export type NativeBuildHost = Readonly<{
3539
options: { deadline: NativeBuildDeadline; description: string },
3640
): Promise<() => Promise<void>>;
3741
processId(): number;
42+
cpuArch(): Promise<string>;
3843
}>;
3944

4045
export function createNativeBuildHost(run: NativeBuildHost['run']): NativeBuildHost {
@@ -50,6 +55,7 @@ export function createNativeBuildHost(run: NativeBuildHost['run']): NativeBuildH
5055
remove: removeHostPath,
5156
acquireLock: acquireNativeBuildLock,
5257
processId: hostProcessId,
58+
cpuArch: readHostCpuArch,
5359
};
5460
}
5561

‎packages/platform-apple/src/native-build/toolchain-identity.test.ts‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import assert from 'node:assert/strict';
22
import { test } from 'vitest';
33
import {
44
isCommandTimeoutError,
5+
withCommandExecutorOverride,
56
type ExecOptions,
67
type ExecResult,
78
} from '@agent-device/host-kit/command';
@@ -19,6 +20,27 @@ import { execKillTimeoutError } from './__tests__/exec-timeout-fixture.ts';
1920
// actually blocks for the timeout it was handed, so a case that claims the budget was spent had to
2021
// spend it.
2122

23+
test('a Rosetta-translated process builds for the arm64 simulator the Mac runs', async () => {
24+
const platform = Object.getOwnPropertyDescriptor(process, 'platform')!;
25+
const arch = Object.getOwnPropertyDescriptor(process, 'arch')!;
26+
Object.defineProperty(process, 'platform', { ...platform, value: 'darwin' });
27+
Object.defineProperty(process, 'arch', { ...arch, value: 'x64' });
28+
try {
29+
const host = createNativeBuildHost(async (command, args) => toolchainAnswer(command, args));
30+
const identity = await withCommandExecutorOverride(
31+
async (command, args) => {
32+
assert.deepEqual([command, ...args], ['/usr/sbin/sysctl', '-n', 'hw.optional.arm64']);
33+
return { stdout: '1\n', stderr: '', exitCode: 0 };
34+
},
35+
() => readHostToolchainIdentity(host, fakeClockDeadline(40_000, { nowMs: 0 })),
36+
);
37+
assert.equal(identity.architecture, 'arm64');
38+
} finally {
39+
Object.defineProperty(process, 'platform', platform);
40+
Object.defineProperty(process, 'arch', arch);
41+
}
42+
});
43+
2244
test('a cold-start toolchain probe recovers on retry, and the retry gets only what the stall left', async () => {
2345
const clock = { nowMs: 0 };
2446
const timeouts: number[] = [];
@@ -39,9 +61,9 @@ test('a cold-start toolchain probe recovers on retry, and the retry gets only wh
3961
// retry runs on the 10 s the shared deadline has left, not a second 30 s.
4062
assert.deepEqual(timeouts.slice(0, 2), [30_000, 10_000]);
4163
assert.equal(clock.nowMs, 30_000);
42-
// 4 baseline probes (xcodebuild, sw_vers x2, uname) plus the one
64+
// 3 baseline probes (xcodebuild, sw_vers x2) plus the one
4365
// retry that recovered the first, timed-out call.
44-
assert.equal(calls, 5);
66+
assert.equal(calls, 4);
4567
});
4668

4769
// The identity read is allowed to exec one Xcode-owned binary. The Simulator SDK a second
@@ -58,7 +80,7 @@ test('the toolchain identity execs one Xcode-owned binary, and no xcrun', async
5880

5981
await readHostToolchainIdentity(host, fakeClockDeadline(120_000, clock));
6082

61-
assert.deepEqual(probed, ['xcodebuild', 'sw_vers', 'sw_vers', 'uname']);
83+
assert.deepEqual(probed, ['xcodebuild', 'sw_vers', 'sw_vers']);
6284
});
6385

6486
test('a toolchain host that never returns reports the stalled probe after one retry', async () => {
@@ -301,12 +323,14 @@ function toolchainAnswer(command: string, args: string[]): ExecResult {
301323
if (command === 'sw_vers') {
302324
return { stdout: args.includes('-buildVersion') ? '24G90' : '15.6', stderr: '', exitCode: 0 };
303325
}
304-
if (command === 'uname') return { stdout: 'arm64', stderr: '', exitCode: 0 };
305326
throw new Error(`the identity read execed ${command} ${args.join(' ')}`);
306327
}
307328

308329
function fakeToolchainHost(
309330
run: (command: string, args: string[], options: ExecOptions) => ExecResult | Promise<ExecResult>,
310331
): NativeBuildHost {
311-
return createNativeBuildHost(async (command, args, options) => run(command, args, options ?? {}));
332+
return {
333+
...createNativeBuildHost(async (command, args, options) => run(command, args, options ?? {})),
334+
cpuArch: async () => 'arm64',
335+
};
312336
}

‎packages/platform-apple/src/native-build/toolchain-identity.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function readHostToolchainIdentity(
2929
const xcode = await toolOutput(host, 'xcodebuild', ['-version'], deadline);
3030
const macosProductVersion = await toolOutput(host, 'sw_vers', ['-productVersion'], deadline);
3131
const macosBuild = await toolOutput(host, 'sw_vers', ['-buildVersion'], deadline);
32-
const architecture = await toolOutput(host, 'uname', ['-m'], deadline);
32+
const architecture = await host.cpuArch();
3333
if (architecture !== 'arm64' && architecture !== 'x86_64') {
3434
throw nativeBuildError('unsupported', 'simulator-architecture-unsupported', { architecture });
3535
}

‎packages/platform-apple/src/runner/apple-runner-platform.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
resolveDeviceAppleOs,
77
type DeviceInfo,
88
} from '@agent-device/kernel/device';
9+
import { readHostCpuArchSync } from './host.ts';
910

1011
/**
1112
* Ceiling on one Apple toolchain identity probe attempt (`xcodebuild -version`, `xcrun
@@ -342,5 +343,5 @@ function runnerPlatformDeviceKind(deviceKind: DeviceInfo['kind']): RunnerPlatfor
342343
}
343344

344345
function resolveMacRunnerArch(): 'arm64' | 'x86_64' {
345-
return process.arch === 'arm64' ? 'arm64' : 'x86_64';
346+
return readHostCpuArchSync() === 'arm64' ? 'arm64' : 'x86_64';
346347
}

0 commit comments

Comments
 (0)