Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions lib/services/cocoapods-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,32 +57,27 @@ export class CocoaPodsService implements ICocoaPodsService {

public async executePodInstall(
projectRoot: string,
xcodeProjPath: string
xcodeProjPath: string,
): Promise<ISpawnResult> {
this.$logger.info("Installing pods...");
let podTool = this.$config.USE_POD_SANDBOX ? "sandbox-pod" : "pod";
const args = ["install"];

if (process.platform === "darwin" && process.arch === "arm64") {
// check if pod is installed as an x86_64 binary or a native arm64 one
// we run the following:
// arch -x86_64 pod --version
// if it's an arm64 binary, we'll get something like this as a result:
// arch: posix_spawnp: pod: Bad CPU type in executable
// in which case, we should run it natively.
const res: string = await this.$childProcess
.exec("arch -x86_64 pod --version", null, {
try {
await this.$childProcess.exec(`${podTool} --version`, null, {
showStderr: true,
})
.then((res) => res.stdout + " " + res.stderr)
.catch((err) => err.message);

if (!res.includes("Bad CPU type in executable")) {
});
} catch (err) {
if (
!/Bad CPU type in executable|Exec format error/i.test(err.message)
) {
throw err;
}
this.$logger.trace(
"Running on arm64 but pod is installed under rosetta2 - running pod through rosetta2"
"Native pod execution is unavailable - running pod through rosetta2",
);
args.unshift(podTool);
args.unshift("-x86_64");
args.unshift("-x86_64", podTool);
podTool = "arch";
}
}
Expand All @@ -92,7 +87,7 @@ export class CocoaPodsService implements ICocoaPodsService {
args,
"close",
{ cwd: projectRoot, stdio: ["pipe", process.stdout, process.stdout] },
{ throwError: false }
{ throwError: false },
);

if (podInstallResult.exitCode !== 0) {
Expand Down
69 changes: 66 additions & 3 deletions test/cocoapods-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -820,10 +820,10 @@ end`,
options?: any,
execOptions?: IExecOptions
): Promise<any> => {
if (command === "arch -x86_64 pod --version") {
// This is the command that is used to check if cocoapods is installed under Rosetta 2
if (command.endsWith("pod --version")) {
// Native CocoaPods is available.
return {
stdout: "Bad CPU type in executable",
stdout: "1.17.0",
stderr: "",
exitCode: 0,
};
Expand Down Expand Up @@ -872,6 +872,69 @@ end`,
});
});

describe("on Apple Silicon", () => {
let platform: PropertyDescriptor;
let arch: PropertyDescriptor;
beforeEach(() => {
platform = Object.getOwnPropertyDescriptor(process, "platform");
arch = Object.getOwnPropertyDescriptor(process, "arch");
Object.defineProperty(process, "platform", { value: "darwin" });
Object.defineProperty(process, "arch", { value: "arm64" });
});
afterEach(() => {
Object.defineProperty(process, "platform", platform);
Object.defineProperty(process, "arch", arch);
});
["pod", "sandbox-pod"].forEach((podTool) => {
[false, true].forEach((needsRosetta) => {
it(`uses ${podTool} ${needsRosetta ? "through Rosetta after a native CPU failure" : "natively even when Rosetta is installed"}`, async () => {
testInjector.resolve<IConfiguration>("config").USE_POD_SANDBOX =
podTool === "sandbox-pod";
const childProcess =
testInjector.resolve<IChildProcess>("childProcess");
const probes: string[] = [];
childProcess.exec = async (command: string): Promise<any> => {
probes.push(command);
if (needsRosetta) {
throw new Error("Bad CPU type in executable");
}
return { stdout: "1.17.0", stderr: "", exitCode: 0 };
};
childProcess.spawnFromEvent = async (
command: string,
args: string[],
): Promise<ISpawnResult> => {
assert.equal(command, needsRosetta ? "arch" : podTool);
assert.deepStrictEqual(
args,
needsRosetta ? ["-x86_64", podTool, "install"] : ["install"],
);
return { stdout: "", stderr: "", exitCode: 0 };
};
await cocoapodsService.executePodInstall(
projectRoot,
xcodeProjPath,
);
assert.deepStrictEqual(probes, [`${podTool} --version`]);
});
});
});
it("preserves unrelated native probe failures", async () => {
const childProcess =
testInjector.resolve<IChildProcess>("childProcess");
childProcess.exec = async (): Promise<any> => {
throw new Error("pod: command not found");
};
childProcess.spawnFromEvent = async (): Promise<ISpawnResult> => {
throw new Error("unexpected Rosetta fallback");
};
await assert.isRejected(
cocoapodsService.executePodInstall(projectRoot, xcodeProjPath),
"pod: command not found",
);
});
});

it("calls pod install spawnFromEvent with correct arguments", async () => {
const childProcess = testInjector.resolve<IChildProcess>("childProcess");
let commandCalled = "";
Expand Down