Skip to content

Commit 733e594

Browse files
rigor789edusperoniNathanWalker
authored
feat(ios): share DerivedData with Xcode and speed up incremental builds (#6178)
* feat(ios): share DerivedData with Xcode and speed up incremental builds DerivedData moves into platforms/ios/build through the per-user WorkspaceSettings, so CLI and Xcode builds reuse each other's output. Settings that made the two builds differ come off the xcodebuild command line: -sdk and forced explicit modules are gone, and the deployment target moves into the Podfile post_install. Simulator builds target the specific destination, debug device builds use a plain build instead of an archive, and the compilation cache lives in platforms/.cache/ios. Device discovery stops waiting as soon as the requested device is found. * fix(ios): don't end device discovery on the wrong device With 100 ms polling, an unrelated device ended the wait almost immediately. A requested device now only ends it by being found, and otherwise the 2 seconds the old polling allowed still apply, so device indexes stay stable. * fix(ios): keep pod build settings literal and out of pod-less apps The build-settings post_install section was written for every app, so apps without pods got a Podfile and ran `pod install` on every prepare. It's now only added when the project already has a Podfile, and removed first so it never keeps one alive on its own. Removing sections no longer leaves a Podfile that only differs from the empty default by blank lines. Values were written as Ruby double-quoted strings, which interpolate `#{}`, `#$` and `#@`. A build.xcconfig value or project path containing them ran as Ruby during `pod install`. They're now single-quoted literals. * fix(ios): keep macro packages, provisioned debug builds and old output paths working - Pass SWIFT_ENABLE_EXPLICIT_MODULES=NO when Package.resolved pins swift-syntax (i.e. a package ships macros), so Xcode 26 builds of those apps keep working. Other apps still leave it to Xcode and share DerivedData with it; build.xcconfig still wins either way. - Archive and export debug device builds when --provision, mobileProvisionIdentifier or --i-cloud-container-environment is set, so the export method, extension profiles and iCloud environment still apply. - Copy the built .app/.ipa to platforms/ios/build/<Config>-<sdk>/ as well (an APFS clone), so CI scripts and Detox binaryPath settings that use the old location keep working. A failed copy only warns. - Podfile build settings hook: skip non-numeric deployment targets instead of letting Gem::Version raise, and walk installer.generated_projects so generate_multiple_pod_projects setups get the settings too. - Move the DerivedData/compilation-cache path helpers into lib/services/ios/ios-build-paths.ts so importing them doesn't load ios-project-service, and put findDevice above getDeviceByIdentifier's JSDoc instead of between them. --------- Co-authored-by: Eduardo Speroni <edusperoni@gmail.com> Co-authored-by: Nathan Walker <walkerrunpdx@gmail.com>
1 parent 1072ba3 commit 733e594

17 files changed

Lines changed: 1477 additions & 301 deletions

‎lib/common/definitions/mobile.d.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,11 @@ declare global {
11741174
shouldReturnImmediateResult: boolean;
11751175
platform: string;
11761176
fullDiscovery?: boolean;
1177+
/**
1178+
* Ends the wait for physical devices once the device requested on the
1179+
* command line has been found by any discovery.
1180+
*/
1181+
isRequestedDeviceFound?: () => boolean;
11771182
}
11781183

11791184
interface IAndroidDeviceHashService {

‎lib/common/mobile/ios/device/ios-device-operations.ts‎

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ import {
1111
} from "../../../declarations";
1212
import { injector } from "../../../yok";
1313

14+
// Other devices can still be arriving by then, and device indexes follow the
15+
// order they were discovered in.
16+
const MIN_DISCOVERY_WAIT_MS = 2000;
17+
18+
export function isDeviceDiscoveryDone(
19+
foundDevice: boolean,
20+
waitedMs: number,
21+
options?: Mobile.IDeviceLookingOptions,
22+
): boolean {
23+
if (options?.isRequestedDeviceFound) {
24+
return options.isRequestedDeviceFound();
25+
}
26+
27+
return foundDevice && !options?.fullDiscovery && waitedMs >= MIN_DISCOVERY_WAIT_MS;
28+
}
29+
1430
export class IOSDeviceOperations
1531
extends EventEmitter
1632
implements IIOSDeviceOperations, IDisposable, IShouldDispose
@@ -86,21 +102,20 @@ export class IOSDeviceOperations
86102

87103
// We need this because we need to make sure that we have devices.
88104
await new Promise<void>((resolve, reject) => {
89-
let iterationsCount = 0;
90-
const maxIterationsCount = 3;
105+
const pollInterval = 100;
106+
const maxWait = 6000;
107+
let waited = 0;
91108

92109
const intervalHandle: NodeJS.Timer = setInterval(() => {
93-
if (foundDevice && !options.fullDiscovery) {
94-
resolve();
95-
return clearInterval(intervalHandle as unknown as number);
96-
}
97-
98-
iterationsCount++;
99-
if (iterationsCount >= maxIterationsCount) {
110+
waited += pollInterval;
111+
if (
112+
waited >= maxWait ||
113+
isDeviceDiscoveryDone(foundDevice, waited, options)
114+
) {
100115
clearInterval(intervalHandle as unknown as number);
101116
return resolve();
102117
}
103-
}, 2000);
118+
}, pollInterval);
104119
});
105120
}
106121
}

‎lib/common/mobile/mobile-core/devices-service.ts‎

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -415,13 +415,23 @@ export class DevicesService
415415
deviceInitOpts?: Mobile.IDeviceLookingOptions,
416416
): Promise<void> {
417417
const options = this.getDeviceLookingOptions(deviceInitOpts);
418-
419-
for (const deviceDiscovery of this._allDeviceDiscoveries) {
418+
const startLooking = async (deviceDiscovery: Mobile.IDeviceDiscovery) => {
420419
try {
421420
await deviceDiscovery.startLookingForDevices(options);
422421
} catch (err) {
423422
this.$logger.trace("Error while checking for devices.", err);
424423
}
424+
};
425+
426+
// Concurrently, so a requested simulator ends the wait for physical
427+
// devices. Otherwise in order, as device indexes follow discovery order.
428+
if (options.isRequestedDeviceFound) {
429+
await Promise.all(this._allDeviceDiscoveries.map(startLooking));
430+
return;
431+
}
432+
433+
for (const deviceDiscovery of this._allDeviceDiscoveries) {
434+
await startLooking(deviceDiscovery);
425435
}
426436
}
427437

@@ -511,6 +521,21 @@ export class DevicesService
511521
}
512522
}
513523

524+
/**
525+
* Finds a device by identifier, display name or emulator image identifier.
526+
*/
527+
private findDevice(deviceOption: string): Mobile.IDevice {
528+
return _.find(
529+
this.getDeviceInstances(),
530+
(d) =>
531+
(d.deviceInfo.identifier && d.deviceInfo.identifier === deviceOption) ||
532+
(d.deviceInfo.displayName &&
533+
d.deviceInfo.displayName === deviceOption) ||
534+
(d.deviceInfo.imageIdentifier &&
535+
d.deviceInfo.imageIdentifier === deviceOption),
536+
);
537+
}
538+
514539
/**
515540
* Returns device that matches an identifier.
516541
* The identifier is expected to be the same as the running device declares it (emulator-5554 for android or GUID for ios).
@@ -581,20 +606,7 @@ export class DevicesService
581606
* @param deviceOption parameter passed by the user to --device flag. Can be name, identifier or imageIdentifier.
582607
*/
583608
public async getDevice(deviceOption: string): Promise<Mobile.IDevice> {
584-
let device: Mobile.IDevice = null;
585-
586-
if (!device) {
587-
device = _.find(
588-
this.getDeviceInstances(),
589-
(d) =>
590-
(d.deviceInfo.identifier &&
591-
d.deviceInfo.identifier === deviceOption) ||
592-
(d.deviceInfo.displayName &&
593-
d.deviceInfo.displayName === deviceOption) ||
594-
(d.deviceInfo.imageIdentifier &&
595-
d.deviceInfo.imageIdentifier === deviceOption),
596-
);
597-
}
609+
let device = this.findDevice(deviceOption);
598610

599611
if (!device && helpers.isNumberWithoutExponent(deviceOption)) {
600612
device = this.getDeviceByIndex(parseInt(deviceOption, 10));
@@ -907,6 +919,13 @@ export class DevicesService
907919
}
908920

909921
deviceInitOpts = deviceInitOpts || {};
922+
const deviceOption = deviceInitOpts.deviceId;
923+
if (deviceOption && !helpers.isNumberWithoutExponent(deviceOption)) {
924+
deviceInitOpts = {
925+
...deviceInitOpts,
926+
isRequestedDeviceFound: () => !!this.findDevice(deviceOption),
927+
};
928+
}
910929
this._data = deviceInitOpts;
911930

912931
if (!deviceInitOpts.skipEmulatorStart) {
@@ -915,13 +934,13 @@ export class DevicesService
915934
}
916935

917936
const platform = deviceInitOpts.platform;
918-
const deviceOption = deviceInitOpts.deviceId;
919937
const deviceLookingOptions: Mobile.IDeviceLookingOptions = {
920938
emulator: deviceInitOpts.emulator,
921939
platform: deviceInitOpts.platform,
922940
shouldReturnImmediateResult: deviceInitOpts.shouldReturnImmediateResult,
923941
detectionInterval: deviceInitOpts.detectionInterval,
924942
fullDiscovery: deviceInitOpts.fullDiscovery,
943+
isRequestedDeviceFound: deviceInitOpts.isRequestedDeviceFound,
925944
};
926945

927946
if (platform && deviceOption) {
@@ -1198,6 +1217,7 @@ export class DevicesService
11981217
shouldReturnImmediateResult: !!shouldReturnImmediateResult,
11991218
emulator: !!emulator,
12001219
fullDiscovery: deviceInitOpts.fullDiscovery,
1220+
isRequestedDeviceFound: deviceInitOpts.isRequestedDeviceFound,
12011221
};
12021222
}
12031223

‎lib/common/test/unit-tests/mobile/devices-service.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,39 @@ describe("devicesService", () => {
12371237
);
12381238
});
12391239

1240+
it("stops waiting for physical devices once the requested simulator is found", async () => {
1241+
let waitedForRequestedDevice = false;
1242+
(<any>iOSDeviceDiscovery).startLookingForDevices = (
1243+
options: Mobile.IDeviceLookingOptions,
1244+
) =>
1245+
new Promise<void>((resolve) => {
1246+
const poll = setInterval(() => {
1247+
if (options.isRequestedDeviceFound?.()) {
1248+
waitedForRequestedDevice = true;
1249+
clearInterval(poll);
1250+
resolve();
1251+
}
1252+
}, 5);
1253+
});
1254+
(<any>iOSSimulatorDiscovery).startLookingForDevices = async () => {
1255+
iOSSimulatorDiscovery.emit(
1256+
DeviceDiscoveryEventNames.DEVICE_FOUND,
1257+
iOSSimulator,
1258+
);
1259+
};
1260+
1261+
await devicesService.initialize({
1262+
platform: "ios",
1263+
deviceId: iOSSimulator.deviceInfo.identifier,
1264+
skipEmulatorStart: true,
1265+
});
1266+
1267+
assert.isTrue(waitedForRequestedDevice);
1268+
assert.deepStrictEqual(devicesService.getDeviceInstances(), <any>[
1269+
iOSSimulator,
1270+
]);
1271+
});
1272+
12401273
describe("when only deviceIdentifier is passed", () => {
12411274
const assertAllMethodsResults = async (deviceId: string) => {
12421275
assert.isFalse(
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { assert } from "chai";
2+
import { isDeviceDiscoveryDone } from "../../../mobile/ios/device/ios-device-operations";
3+
4+
describe("isDeviceDiscoveryDone", () => {
5+
const options = (extra: object = {}) => <any>{ ...extra };
6+
7+
it("ends as soon as the requested device is found", () => {
8+
const isRequestedDeviceFound = () => true;
9+
assert.isTrue(
10+
isDeviceDiscoveryDone(false, 100, options({ isRequestedDeviceFound })),
11+
);
12+
});
13+
14+
it("keeps waiting for the requested device when another one shows up", () => {
15+
const isRequestedDeviceFound = () => false;
16+
assert.isFalse(
17+
isDeviceDiscoveryDone(true, 5000, options({ isRequestedDeviceFound })),
18+
);
19+
});
20+
21+
it("gives other devices time to arrive when none was requested", () => {
22+
assert.isFalse(isDeviceDiscoveryDone(true, 100, options()));
23+
assert.isTrue(isDeviceDiscoveryDone(true, 2000, options()));
24+
});
25+
26+
it("keeps waiting while no device was found", () => {
27+
assert.isFalse(isDeviceDiscoveryDone(false, 5000, options()));
28+
});
29+
30+
it("doesn't end early on a full discovery", () => {
31+
assert.isFalse(
32+
isDeviceDiscoveryDone(true, 5000, options({ fullDiscovery: true })),
33+
);
34+
});
35+
36+
it("works without options", () => {
37+
assert.isTrue(isDeviceDiscoveryDone(true, 2000));
38+
});
39+
});

‎lib/definitions/ios.d.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ declare global {
114114
platformData: IPlatformData,
115115
projectData: IProjectData,
116116
): string[];
117+
shouldArchiveDeviceBuild(
118+
buildConfig: IBuildConfig & { buildForAppStore?: boolean },
119+
): boolean;
117120
}
118121

119122
interface IXcodebuildCommandService {

‎lib/definitions/project.d.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,12 @@ interface ICocoaPodsService {
563563
platformData: IPlatformData,
564564
): Promise<void>;
565565

566+
applyPodfileBuildSettings(
567+
projectData: IProjectData,
568+
platformData: IPlatformData,
569+
buildSettings: Record<string, string> | null,
570+
): Promise<void>;
571+
566572
applyPodfileFromExtensions(
567573
projectData: IProjectData,
568574
platformData: IPlatformData,

0 commit comments

Comments
 (0)