Skip to content

Commit fc5f3c0

Browse files
committed
fix(ios): select the Mac by name or index, retry failed hardware lookups
- Mac discovery accepts the display name and a numeric --device, so indexes match the `ns devices` list. - A failed system_profiler lookup is no longer cached as unsupported hardware. - Release runs launch through DevicesService.execute, so a --device run launches only the device that received the app.
1 parent d808c94 commit fc5f3c0

5 files changed

Lines changed: 98 additions & 41 deletions

File tree

‎lib/common/mobile/ios/mac/ios-mac-constants.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as semver from "semver";
55
*/
66
export const IOS_MAC_DEVICE_ALIAS = "mac";
77

8+
export const IOS_MAC_DEVICE_DISPLAY_NAME = "My Mac (Designed for iPad)";
9+
810
/**
911
* Mac builds get their own folder under DerivedData's Build/Products. They use
1012
* the iphoneos SDK like phone builds, and .nsbuildinfo only records prepare

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { IProjectDataService } from "../../../../definitions/project";
1717
import {
1818
IOS_MAC_BUILD_DIR_NAME,
1919
IOS_MAC_DEVICE_ALIAS,
20+
IOS_MAC_DEVICE_DISPLAY_NAME,
2021
} from "./ios-mac-constants";
2122

2223
export interface IIOSMacHardware {
@@ -54,15 +55,14 @@ export class IOSMacDevice extends IOSDeviceBase {
5455
protected $logger: ILogger,
5556
) {
5657
super();
57-
this.applicationManager = this.$injector.resolve(
58-
IOSMacApplicationManager,
59-
{ device: this },
60-
);
58+
this.applicationManager = this.$injector.resolve(IOSMacApplicationManager, {
59+
device: this,
60+
});
6161
this.fileSystem = this.$injector.resolve(IOSMacFileSystem);
6262
this.deviceInfo = {
6363
imageIdentifier: IOS_MAC_DEVICE_ALIAS,
6464
identifier: hardware.provisioningUDID,
65-
displayName: "My Mac (Designed for iPad)",
65+
displayName: IOS_MAC_DEVICE_DISPLAY_NAME,
6666
model: hardware.model,
6767
version: hardware.osVersion,
6868
vendor: "Apple",
@@ -135,13 +135,15 @@ export class IOSMacDevice extends IOSDeviceBase {
135135
// trailing newline as a partial line, so the newline is put back.
136136
for (const stream of [this.logProcess.stdout, this.logProcess.stderr]) {
137137
if (stream) {
138-
readline.createInterface({ input: stream }).on("line", (line) =>
139-
this.$deviceLogProvider.logData(
140-
`${line}\n`,
141-
this.$devicePlatformsConstants.iOS,
142-
this.deviceInfo.identifier,
143-
),
144-
);
138+
readline
139+
.createInterface({ input: stream })
140+
.on("line", (line) =>
141+
this.$deviceLogProvider.logData(
142+
`${line}\n`,
143+
this.$devicePlatformsConstants.iOS,
144+
this.deviceInfo.identifier,
145+
),
146+
);
145147
}
146148
}
147149
this.logProcess.once("error", (err) =>

‎lib/common/mobile/mobile-core/ios-mac-device-discovery.ts‎

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { DeviceDiscovery } from "./device-discovery";
22
import { IIOSMacHardware, IOSMacDevice } from "../ios/mac/ios-mac-device";
3-
import { IOS_MAC_DEVICE_ALIAS } from "../ios/mac/ios-mac-constants";
3+
import {
4+
IOS_MAC_DEVICE_ALIAS,
5+
IOS_MAC_DEVICE_DISPLAY_NAME,
6+
} from "../ios/mac/ios-mac-constants";
7+
import { isNumberWithoutExponent } from "../../helpers";
48
import { IChildProcess, IHostInfo } from "../../declarations";
59
import { IInjector } from "../../definitions/yok";
610
import { IOptions } from "../../../declarations";
@@ -49,10 +53,14 @@ export class IOSMacDeviceDiscovery extends DeviceDiscovery {
4953
return;
5054
}
5155

56+
// A numeric --device is an index into the `ns devices` list, which
57+
// includes the Mac, so the Mac must be present for indexes to line up.
5258
const requested = options?.deviceId ?? this.$options.device;
5359
const isRequested =
5460
requested === IOS_MAC_DEVICE_ALIAS ||
55-
requested === hardware.provisioningUDID;
61+
requested === IOS_MAC_DEVICE_DISPLAY_NAME ||
62+
requested === hardware.provisioningUDID ||
63+
isNumberWithoutExponent(requested);
5664
if (!options?.fullDiscovery && !isRequested) {
5765
return;
5866
}
@@ -66,11 +74,13 @@ export class IOSMacDeviceDiscovery extends DeviceDiscovery {
6674
return this.hardware;
6775
}
6876

69-
this.hardware = null;
7077
try {
7178
// Intel Macs can't run iOS apps; Rosetta hides arm64 from process.arch.
72-
const arm64 = await this.$childProcess.exec("sysctl -n hw.optional.arm64");
79+
const arm64 = await this.$childProcess.exec(
80+
"sysctl -n hw.optional.arm64",
81+
);
7382
if (arm64.toString().trim() !== "1") {
83+
this.hardware = null;
7484
return this.hardware;
7585
}
7686

@@ -87,18 +97,19 @@ export class IOSMacDeviceDiscovery extends DeviceDiscovery {
8797
)
8898
.toString()
8999
.trim();
90-
if (info?.provisioning_UDID) {
91-
this.hardware = {
92-
provisioningUDID: info.provisioning_UDID,
93-
model: info.machine_model || "Mac",
94-
osVersion,
95-
};
96-
}
100+
this.hardware = info?.provisioning_UDID
101+
? {
102+
provisioningUDID: info.provisioning_UDID,
103+
model: info.machine_model || "Mac",
104+
osVersion,
105+
}
106+
: null;
107+
return this.hardware;
97108
} catch (err) {
109+
// Not cached, so a later lookup retries.
98110
this.$logger.trace(`Unable to read this Mac's hardware info: ${err}`);
111+
return null;
99112
}
100-
101-
return this.hardware;
102113
}
103114
}
104115

‎lib/common/test/unit-tests/mobile/ios-mac-device-discovery.ts‎

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ function createTestInjector(opts: {
2222
arm64?: boolean;
2323
isDarwin?: boolean;
2424
device?: string;
25+
profilerFailures?: number;
2526
}): IInjector {
27+
let profilerFailures = opts.profilerFailures ?? 0;
2628
const injector = new Yok();
2729
injector.register("injector", injector);
2830
injector.register("fs", FileSystemStub);
@@ -49,6 +51,10 @@ function createTestInjector(opts: {
4951
return opts.arm64 === false ? "0\n" : "1\n";
5052
}
5153
if (command.startsWith("system_profiler")) {
54+
if (profilerFailures > 0) {
55+
profilerFailures--;
56+
throw new Error("system_profiler failed");
57+
}
5258
return JSON.stringify({
5359
SPHardwareDataType: [
5460
{ provisioning_UDID: provisioningUDID, machine_model: "Mac15,8" },
@@ -90,8 +96,13 @@ describe("iOSMacDeviceDiscovery", () => {
9096
assert.equal(device.deviceInfo.platform, "iOS");
9197
});
9298

93-
it("adds the Mac when --device is mac or its UDID", async () => {
94-
for (const deviceOption of ["mac", provisioningUDID]) {
99+
it("adds the Mac when --device is mac, its UDID, its name or an index", async () => {
100+
for (const deviceOption of [
101+
"mac",
102+
provisioningUDID,
103+
"My Mac (Designed for iPad)",
104+
"1",
105+
]) {
95106
const found = await discover(
96107
createTestInjector({ device: deviceOption }),
97108
{ platform: "ios" },
@@ -108,6 +119,27 @@ describe("iOSMacDeviceDiscovery", () => {
108119
assert.lengthOf(found, 1);
109120
});
110121

122+
it("retries a hardware lookup that failed", async () => {
123+
const injector = createTestInjector({ profilerFailures: 1 });
124+
const discovery = injector.resolve<IOSMacDeviceDiscovery>(
125+
"iOSMacDeviceDiscovery",
126+
);
127+
const found: Mobile.IDevice[] = [];
128+
discovery.on(DeviceDiscoveryEventNames.DEVICE_FOUND, (d: Mobile.IDevice) =>
129+
found.push(d),
130+
);
131+
const options = <Mobile.IDeviceLookingOptions>{
132+
platform: "ios",
133+
deviceId: "mac",
134+
};
135+
136+
await discovery.startLookingForDevices(options);
137+
assert.lengthOf(found, 0);
138+
139+
await discovery.startLookingForDevices(options);
140+
assert.lengthOf(found, 1);
141+
});
142+
111143
it("leaves the Mac out of a run that doesn't name it", async () => {
112144
const found = await discover(createTestInjector({}), { platform: "ios" });
113145
assert.lengthOf(found, 0);
@@ -125,7 +157,11 @@ describe("iOSMacDeviceDiscovery", () => {
125157
createTestInjector(injectorOptions),
126158
lookingOptions,
127159
);
128-
assert.lengthOf(found, 0, JSON.stringify([injectorOptions, lookingOptions]));
160+
assert.lengthOf(
161+
found,
162+
0,
163+
JSON.stringify([injectorOptions, lookingOptions]),
164+
);
129165
}
130166
});
131167
});

‎lib/helpers/livesync-command-helper.ts‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -304,19 +304,25 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
304304

305305
await this.$deployController.deploy({ deviceDescriptors });
306306

307-
for (const deviceDescriptor of deviceDescriptors) {
308-
const device = this.$devicesService.getDeviceByIdentifier(
309-
deviceDescriptor.identifier,
310-
);
311-
await device.applicationManager.startApplication({
312-
appId:
313-
this.$projectData.projectIdentifiers[
314-
device.deviceInfo.platform.toLowerCase()
315-
],
316-
projectName: this.$projectData.projectName,
317-
projectDir: this.$projectData.projectDir,
318-
});
319-
}
307+
// Same device selection as deploy, so a --device run launches only the
308+
// device that received the app.
309+
await this.$devicesService.execute(
310+
(device: Mobile.IDevice) =>
311+
device.applicationManager.startApplication({
312+
appId:
313+
this.$projectData.projectIdentifiers[
314+
device.deviceInfo.platform.toLowerCase()
315+
],
316+
projectName: this.$projectData.projectName,
317+
projectDir: this.$projectData.projectDir,
318+
}),
319+
(device: Mobile.IDevice) =>
320+
_.some(
321+
deviceDescriptors,
322+
(deviceDescriptor) =>
323+
deviceDescriptor.identifier === device.deviceInfo.identifier,
324+
),
325+
);
320326
}
321327

322328
public async stop() {

0 commit comments

Comments
 (0)