Skip to content

Commit ab19351

Browse files
fix(ios): prefer native CocoaPods before Rosetta (#6170)
Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
1 parent bf59f02 commit ab19351

2 files changed

Lines changed: 79 additions & 19 deletions

File tree

‎lib/services/cocoapods-service.ts‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -72,25 +72,22 @@ export class CocoaPodsService implements ICocoaPodsService {
7272
const args = ["install"];
7373

7474
if (process.platform === "darwin" && process.arch === "arm64") {
75-
// check if pod is installed as an x86_64 binary or a native arm64 one
76-
// we run the following:
77-
// arch -x86_64 pod --version
78-
// if it's an arm64 binary, we'll get something like this as a result:
79-
// arch: posix_spawnp: pod: Bad CPU type in executable
80-
// in which case, we should run it natively.
81-
const res: string = await this.$childProcess
82-
.exec("arch -x86_64 pod --version", null, {
75+
try {
76+
await this.$childProcess.exec(`${podTool} --version`, null, {
8377
showStderr: true,
84-
})
85-
.then((res) => res.stdout + " " + res.stderr)
86-
.catch((err) => err.message);
87-
88-
if (!res.includes("Bad CPU type in executable")) {
78+
});
79+
} catch (err) {
80+
if (
81+
!/Bad CPU type in executable|Exec format error/i.test(
82+
String(err?.message ?? err),
83+
)
84+
) {
85+
throw err;
86+
}
8987
this.$logger.trace(
90-
"Running on arm64 but pod is installed under rosetta2 - running pod through rosetta2",
88+
"Native pod execution is unavailable - running pod through rosetta2",
9189
);
92-
args.unshift(podTool);
93-
args.unshift("-x86_64");
90+
args.unshift("-x86_64", podTool);
9491
podTool = "arch";
9592
}
9693
}

‎test/cocoapods-service.ts‎

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,10 +883,10 @@ end`;
883883
options?: any,
884884
execOptions?: IExecOptions,
885885
): Promise<any> => {
886-
if (command === "arch -x86_64 pod --version") {
887-
// This is the command that is used to check if cocoapods is installed under Rosetta 2
886+
if (command.endsWith("pod --version")) {
887+
// Native CocoaPods is available.
888888
return {
889-
stdout: "Bad CPU type in executable",
889+
stdout: "1.17.0",
890890
stderr: "",
891891
exitCode: 0,
892892
};
@@ -935,6 +935,69 @@ end`;
935935
});
936936
});
937937

938+
describe("on Apple Silicon", () => {
939+
let platform: PropertyDescriptor;
940+
let arch: PropertyDescriptor;
941+
beforeEach(() => {
942+
platform = Object.getOwnPropertyDescriptor(process, "platform");
943+
arch = Object.getOwnPropertyDescriptor(process, "arch");
944+
Object.defineProperty(process, "platform", { value: "darwin" });
945+
Object.defineProperty(process, "arch", { value: "arm64" });
946+
});
947+
afterEach(() => {
948+
Object.defineProperty(process, "platform", platform);
949+
Object.defineProperty(process, "arch", arch);
950+
});
951+
["pod", "sandbox-pod"].forEach((podTool) => {
952+
[false, true].forEach((needsRosetta) => {
953+
it(`uses ${podTool} ${needsRosetta ? "through Rosetta after a native CPU failure" : "natively even when Rosetta is installed"}`, async () => {
954+
testInjector.resolve<IConfiguration>("config").USE_POD_SANDBOX =
955+
podTool === "sandbox-pod";
956+
const childProcess =
957+
testInjector.resolve<IChildProcess>("childProcess");
958+
const probes: string[] = [];
959+
childProcess.exec = async (command: string): Promise<any> => {
960+
probes.push(command);
961+
if (needsRosetta) {
962+
throw new Error("Bad CPU type in executable");
963+
}
964+
return { stdout: "1.17.0", stderr: "", exitCode: 0 };
965+
};
966+
childProcess.spawnFromEvent = async (
967+
command: string,
968+
args: string[],
969+
): Promise<ISpawnResult> => {
970+
assert.equal(command, needsRosetta ? "arch" : podTool);
971+
assert.deepStrictEqual(
972+
args,
973+
needsRosetta ? ["-x86_64", podTool, "install"] : ["install"],
974+
);
975+
return { stdout: "", stderr: "", exitCode: 0 };
976+
};
977+
await cocoapodsService.executePodInstall(
978+
projectRoot,
979+
xcodeProjPath,
980+
);
981+
assert.deepStrictEqual(probes, [`${podTool} --version`]);
982+
});
983+
});
984+
});
985+
it("preserves unrelated native probe failures", async () => {
986+
const childProcess =
987+
testInjector.resolve<IChildProcess>("childProcess");
988+
childProcess.exec = async (): Promise<any> => {
989+
throw new Error("pod: command not found");
990+
};
991+
childProcess.spawnFromEvent = async (): Promise<ISpawnResult> => {
992+
throw new Error("unexpected Rosetta fallback");
993+
};
994+
await assert.isRejected(
995+
cocoapodsService.executePodInstall(projectRoot, xcodeProjPath),
996+
"pod: command not found",
997+
);
998+
});
999+
});
1000+
9381001
it("calls pod install spawnFromEvent with correct arguments", async () => {
9391002
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
9401003
let commandCalled = "";

0 commit comments

Comments
 (0)