Skip to content

Commit efea044

Browse files
committed
Add xcodeVersionFromXcodeBuild
1 parent 8f2596e commit efea044

5 files changed

Lines changed: 233 additions & 7 deletions

File tree

‎lib/entry-points.js‎

Lines changed: 59 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/languages/traced.test.ts‎

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs";
22

3+
import * as toolrunner from "@actions/exec/lib/toolrunner";
34
import * as io from "@actions/io";
45
import test from "ava";
56
import * as sinon from "sinon";
@@ -428,7 +429,7 @@ test.serial(
428429
);
429430

430431
test.serial(
431-
"checkRunnerSupportsTracedAnalysis uses Xcode SDK version as fallback",
432+
"checkRunnerSupportsTracedAnalysis attempts fallbacks if needed",
432433
async (t) =>
433434
withTmpDir(async (tmpDir) => {
434435
const logger = new RecordingLogger();
@@ -439,7 +440,16 @@ test.serial(
439440
const readlinkSync = sinon
440441
.stub(fs, "readlinkSync")
441442
.throws(new Error("Not found"));
442-
const which = sinon.stub(io, "which").throws(new Error("Tool not found"));
443+
const which = sinon.stub(io, "which").resolves("/path/to/xcrun");
444+
const exec = sinon.stub<[], Promise<number>>().callsFake(async () => {
445+
throw new Error("Error to force next fallback");
446+
});
447+
const runner = sinon.createStubInstance(toolrunner.ToolRunner, { exec });
448+
const toolrunnerStub = sinon
449+
.stub(toolrunner, "ToolRunner")
450+
.callsFake(() => {
451+
return runner;
452+
});
443453

444454
const codeql = await getCodeQLWithTracedStub(
445455
"codeql-for-testing",
@@ -468,11 +478,27 @@ test.serial(
468478

469479
t.is(readlinkSync.callCount, 1);
470480
t.deepEqual(readlinkSync.args[0], [XCODE_SELECT_LINK_PATH]);
471-
t.is(which.callCount, 1);
481+
t.is(which.callCount, 2);
472482
t.deepEqual(which.args[0], ["xcrun", true]);
483+
t.deepEqual(which.args[1], ["xcrun", true]);
484+
485+
t.is(toolrunnerStub.callCount, 2);
486+
t.is(exec.callCount, 2);
487+
488+
t.is(toolrunnerStub.args[0][0], "/path/to/xcrun");
489+
t.deepEqual(toolrunnerStub.args[0][1], ["xcodebuild", "-version"]);
490+
491+
t.is(toolrunnerStub.args[1][0], "/path/to/xcrun");
492+
t.deepEqual(toolrunnerStub.args[1][1], [
493+
"--sdk",
494+
"macosx",
495+
"--show-sdk-version",
496+
]);
473497

474498
checkExpectedLogMessages(t, logger.messages, [
475-
"Unable to determine Xcode version: Tool not found",
499+
"Unable to determine Xcode version: Not found",
500+
"Unable to determine Xcode version from xcodebuild: Error to force next fallback",
501+
"Unable to determine Xcode version: Error to force next fallback",
476502
]);
477503
checkUnexpectedLogMessages(t, logger.messages, [
478504
"Traced analysis is not supported on Xcode 27",

‎src/languages/traced.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
darwinVersion,
1212
xcodeVersionFromLink,
1313
xcodeVersionFromSdk,
14+
xcodeVersionFromXcodeBuild,
1415
} from "../platform";
1516
import { ConfigurationError } from "../util";
1617

@@ -201,10 +202,12 @@ export async function checkRunnerSupportsTracedAnalysis(
201202

202203
// Determining whether the Xcode version is supported only makes sense on macOS, so we only do it
203204
// after determining that we are running on macOS. We first try to obtain the version of Xcode itself
204-
// starting with the cheapest approach. If that doesn't work, we obtain the version from the SDK instead.
205-
// Since either Xcode or the SDK at version 27 or above is incompatible, the major version of either is fine.
205+
// starting with the cheapest approach and then call out to an external program. If neither works, we try
206+
// to obtain the version from the SDK instead. Since either Xcode or the SDK at version 27 or above is
207+
// incompatible, either is fine as we only check the major version below.
206208
const xcodeVer =
207209
xcodeVersionFromLink(action.logger) ??
210+
(await xcodeVersionFromXcodeBuild(action.logger)) ??
208211
(await xcodeVersionFromSdk(action.logger));
209212

210213
if (

‎src/platform.test.ts‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
XCODE_SELECT_LINK_PATH,
1414
xcodeVersionFromLink,
1515
xcodeVersionFromSdk,
16+
xcodeVersionFromXcodeBuild,
1617
} from "./platform";
1718
import {
1819
checkExpectedLogMessages,
@@ -228,6 +229,74 @@ test.serial(
228229
},
229230
);
230231

232+
test.serial(
233+
"xcodeVersionFromXcodeBuild returns undefined for non-zero exit code",
234+
async (t) => {
235+
const logger = new RecordingLogger();
236+
237+
const which = sinon.stub(io, "which").resolves("/path/to/xcrun");
238+
const exec = sinon.stub<[], Promise<number>>().resolves(1);
239+
const runner = sinon.createStubInstance(toolrunner.ToolRunner, { exec });
240+
const toolrunnerStub = sinon.stub(toolrunner, "ToolRunner").returns(runner);
241+
242+
await t.notThrowsAsync(async () => {
243+
const result = await xcodeVersionFromXcodeBuild(logger);
244+
245+
t.is(result, undefined);
246+
247+
t.is(which.callCount, 1);
248+
t.deepEqual(which.args[0], ["xcrun", true]);
249+
250+
t.is(toolrunnerStub.callCount, 1);
251+
t.is(exec.callCount, 1);
252+
253+
checkExpectedLogMessages(t, logger.messages, [
254+
"xcodebuild returned exit code 1",
255+
]);
256+
});
257+
},
258+
);
259+
260+
test.serial(
261+
"xcodeVersionFromXcodeBuild extracts version from xcrun stdout",
262+
async (t) => {
263+
const logger = new RecordingLogger();
264+
265+
const which = sinon.stub(io, "which").resolves("/path/to/xcrun");
266+
267+
let stdoutListener: ((data: Buffer) => void) | undefined = undefined;
268+
const exec = sinon.stub<[], Promise<number>>().callsFake(async () => {
269+
if (stdoutListener !== undefined) {
270+
stdoutListener(Buffer.from("Xcode 26.5"));
271+
stdoutListener(Buffer.from("Build version 26F9"));
272+
}
273+
return 0;
274+
});
275+
const runner = sinon.createStubInstance(toolrunner.ToolRunner, { exec });
276+
277+
const toolrunnerStub = sinon
278+
.stub(toolrunner, "ToolRunner")
279+
.callsFake((_cmd, _args, opts: ExecOptions) => {
280+
stdoutListener = opts.listeners?.stdout;
281+
return runner;
282+
});
283+
284+
await t.notThrowsAsync(async () => {
285+
const result = await xcodeVersionFromXcodeBuild(logger);
286+
287+
t.not(result, undefined);
288+
t.is(result?.major, 26);
289+
t.is(result?.minor, 5);
290+
291+
t.is(which.callCount, 1);
292+
t.deepEqual(which.args[0], ["xcrun", true]);
293+
294+
t.is(toolrunnerStub.callCount, 1);
295+
t.is(exec.callCount, 1);
296+
});
297+
},
298+
);
299+
231300
test.serial(
232301
"xcodeVersionFromSdk returns undefined for non-zero exit code",
233302
async (t) => {

‎src/platform.ts‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,76 @@ export function xcodeVersionFromLink(
115115
}
116116
}
117117

118+
/**
119+
* Tries to determine the version of Xcode that is installed by running `xcrun xcodebuild -version`.
120+
*
121+
* @param logger The logger to use.
122+
* @returns The Xcode version or `undefined` if it couldn't be determined.
123+
*/
124+
export async function xcodeVersionFromXcodeBuild(
125+
logger: Logger,
126+
): Promise<semver.SemVer | undefined> {
127+
try {
128+
const xcrun = await io.which("xcrun", true);
129+
let stdout = "";
130+
let stderr = "";
131+
132+
const exitCode = await new toolrunner.ToolRunner(
133+
xcrun,
134+
["xcodebuild", "-version"],
135+
{
136+
silent: true,
137+
ignoreReturnCode: true,
138+
listeners: {
139+
stdout: (data: Buffer) => {
140+
stdout += data.toString("utf-8");
141+
},
142+
stderr: (data: Buffer) => {
143+
stderr += data.toString("utf-8");
144+
},
145+
},
146+
},
147+
).exec();
148+
149+
if (exitCode !== 0) {
150+
logger.debug(`xcodebuild returned exit code ${exitCode}`);
151+
logger.debug(`stdout:\n${stdout}`);
152+
logger.debug(`stderr:\n${stderr}`);
153+
return undefined;
154+
}
155+
156+
// The output contains a couple of lines, but we only care about the product version.
157+
const match = stdout.match(/^Xcode (\d+(\.\d+)?)/);
158+
159+
if (match === null) {
160+
logger.debug(
161+
`Output doesn't appear to contain the Xcode product version.`,
162+
);
163+
logger.debug(`stdout:\n${stdout}`);
164+
logger.debug(`stderr:\n${stderr}`);
165+
return undefined;
166+
}
167+
168+
// We expect the output to only be a version string. Try to parse it into a semver.
169+
const version = semver.coerce(match[1].trim());
170+
171+
if (version === null) {
172+
logger.debug(
173+
`xcodebuild's output couldn't be parsed as a version:\n${stdout}`,
174+
);
175+
logger.debug(`stderr:\n${stderr}`);
176+
return undefined;
177+
}
178+
179+
return version;
180+
} catch (err) {
181+
logger.debug(
182+
`Unable to determine Xcode version from xcodebuild: ${getErrorMessage(err)}`,
183+
);
184+
return undefined;
185+
}
186+
}
187+
118188
/**
119189
* Tries to determine the version of Xcode that is installed by running `xcrun --sdk macosx --show-sdk-version`.
120190
*

0 commit comments

Comments
 (0)