Skip to content

Commit cf00361

Browse files
committed
refactor(android): one adb failure table carries the host-refusal shapes
1 parent 8e130b4 commit cf00361

1 file changed

Lines changed: 36 additions & 37 deletions

File tree

‎packages/platform-android/src/adb-failure.ts‎

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,58 @@ export type AndroidAdbFailureClassification = Readonly<{
2323
type AndroidAdbFailureMatcher = readonly [
2424
pattern: RegExp,
2525
failure: AndroidAdbFailureClassification,
26-
matchStdout?: true,
26+
options?: Readonly<{
27+
matchStdout?: true;
28+
/** Anchored shape of the host adb's first stderr line when it issues this refusal before the command leaves the host. */
29+
hostRefusal?: RegExp;
30+
}>,
2731
];
2832

33+
const HOST_REFUSAL_PREFIX = String.raw`(?:adb|error): `;
34+
35+
function hostRefusalHead(body: string): RegExp {
36+
return new RegExp(`^${HOST_REFUSAL_PREFIX}${body}$`);
37+
}
38+
2939
const ANDROID_ADB_DEVICE_OFFLINE_FAILURE = {
3040
reason: 'device_offline',
3141
hint: 'The device is connected but offline — wait for it to finish booting or run adb reconnect, then retry.',
3242
retriable: true,
3343
} as const satisfies AndroidAdbFailureClassification;
3444

35-
const ANDROID_ADB_FAILURE_MATCHERS = [
45+
const ANDROID_ADB_FAILURE_MATCHERS: readonly AndroidAdbFailureMatcher[] = [
3646
[
3747
/device unauthorized|device still authorizing/,
3848
{
3949
reason: 'device_unauthorized',
4050
hint: 'USB debugging is not authorized — accept the authorization prompt on the device screen (re-plug the cable if none appears), then retry.',
4151
},
52+
{ hostRefusal: hostRefusalHead(String.raw`device (?:unauthorized|still authorizing)\.?`) },
53+
],
54+
[
55+
/device offline/,
56+
ANDROID_ADB_DEVICE_OFFLINE_FAILURE,
57+
{ hostRefusal: hostRefusalHead(String.raw`device (?:'[^']*' )?offline`) },
4258
],
43-
[/device offline/, ANDROID_ADB_DEVICE_OFFLINE_FAILURE],
4459
[
4560
/more than one (?:device\/emulator|device and emulator)/,
4661
{
4762
reason: 'multiple_devices',
4863
hint: 'Multiple Android devices are connected — pass --serial <serial> (see adb devices) to select one.',
4964
},
65+
{
66+
hostRefusal: hostRefusalHead(
67+
String.raw`more than one (?:device/emulator|device and emulator)`,
68+
),
69+
},
5070
],
5171
[
5272
/no devices\/emulators found|no devices found/,
5373
{
5474
reason: 'no_devices',
5575
hint: 'No Android devices detected — boot an emulator or connect a device and verify it appears in adb devices.',
5676
},
77+
{ hostRefusal: hostRefusalHead(String.raw`no devices(?:/emulators)? found`) },
5778
],
5879
[
5980
/device (?:'[^']*' )?not found/,
@@ -62,6 +83,7 @@ const ANDROID_ADB_FAILURE_MATCHERS = [
6283
hint: 'The device disconnected or is restarting — verify it is listed in adb devices, then retry.',
6384
retriable: true,
6485
},
86+
{ hostRefusal: hostRefusalHead(String.raw`device (?:'[^']*' )?not found`) },
6587
],
6688
[
6789
/adb server version \(\d+\) doesn't match this client/,
@@ -85,33 +107,33 @@ const ANDROID_ADB_FAILURE_MATCHERS = [
85107
reason: 'install_insufficient_storage',
86108
hint: 'The device is out of storage — free up space or uninstall unused apps, then retry the install.',
87109
},
88-
true,
110+
{ matchStdout: true },
89111
],
90112
[
91113
/install_failed_update_incompatible/,
92114
{
93115
reason: 'install_update_incompatible',
94116
hint: 'The installed app has an incompatible signature — uninstall the existing app first, then retry the install.',
95117
},
96-
true,
118+
{ matchStdout: true },
97119
],
98120
[
99121
/install_failed_version_downgrade/,
100122
{
101123
reason: 'install_version_downgrade',
102124
hint: 'The APK is older than the installed app — uninstall the app first (or install with downgrade allowed), then retry.',
103125
},
104-
true,
126+
{ matchStdout: true },
105127
],
106128
[
107129
/install_failed_\w+|install_parse_failed_\w+/,
108130
{
109131
reason: 'install_failed',
110132
hint: 'The Android package installer rejected the APK — see the INSTALL_FAILED code in the error output for the exact cause.',
111133
},
112-
true,
134+
{ matchStdout: true },
113135
],
114-
] as const satisfies readonly AndroidAdbFailureMatcher[];
136+
];
115137

116138
const ANDROID_ADB_TIMEOUT_FAILURE: AndroidAdbFailureClassification = Object.freeze({
117139
reason: 'timeout',
@@ -121,34 +143,10 @@ const ANDROID_ADB_TIMEOUT_FAILURE: AndroidAdbFailureClassification = Object.free
121143
const ANDROID_HELPER_INSTALL_TIMEOUT_HINT =
122144
'The helper install timed out — some OEM builds (ColorOS is one) hold the first install of each package behind a system install-confirmation dialog and adb waits for it. Check the device screen, confirm any pending install prompt, then retry; if none is showing, run adb kill-server && adb start-server and retry.';
123145

124-
type HostRefusalShape = readonly [head: RegExp, reason: AndroidAdbFailureReason];
125-
126-
const HOST_REFUSAL_PREFIX = String.raw`(?:adb|error): `;
127-
128-
/** First stderr line, lowercased, of each refusal the host adb issues before the command leaves the host. */
129-
const HOST_REFUSAL_SHAPES: readonly HostRefusalShape[] = [
130-
[
131-
new RegExp(`^${HOST_REFUSAL_PREFIX}device (?:unauthorized|still authorizing)\\.?$`),
132-
'device_unauthorized',
133-
],
134-
[new RegExp(`^${HOST_REFUSAL_PREFIX}device (?:'[^']*' )?offline$`), 'device_offline'],
135-
[
136-
new RegExp(`^${HOST_REFUSAL_PREFIX}more than one (?:device/emulator|device and emulator)$`),
137-
'multiple_devices',
138-
],
139-
[new RegExp(`^${HOST_REFUSAL_PREFIX}no devices(?:/emulators)? found$`), 'no_devices'],
140-
[new RegExp(`^${HOST_REFUSAL_PREFIX}device (?:'[^']*' )?not found$`), 'device_not_found'],
141-
];
142-
143146
/** The fixed trailer lines adb appends to a device_unauthorized refusal. */
144147
const HOST_REFUSAL_TRAILER =
145148
/^(?:this adb server's \$adb_vendor_keys is not set|try 'adb kill-server' if that seems wrong\.|(?:otherwise|please) check (?:for|the) (?:a )?confirmation dialog on your device\.)$/;
146149

147-
const ANDROID_ADB_FAILURES_BY_REASON = new Map<
148-
AndroidAdbFailureReason,
149-
AndroidAdbFailureClassification
150-
>(ANDROID_ADB_FAILURE_MATCHERS.map(([, failure]) => [failure.reason, failure]));
151-
152150
function classifyHostRefusal(
153151
stderrText: string,
154152
stdoutText: string,
@@ -161,9 +159,10 @@ function classifyHostRefusal(
161159
if (head === undefined || !trailer.every((line) => HOST_REFUSAL_TRAILER.test(line))) {
162160
return undefined;
163161
}
164-
const reason = HOST_REFUSAL_SHAPES.find(([shape]) => shape.test(head))?.[1];
165-
const failure = reason && ANDROID_ADB_FAILURES_BY_REASON.get(reason);
166-
return failure && { ...failure, hostRefusal: true };
162+
const row = ANDROID_ADB_FAILURE_MATCHERS.find(([, , options]) =>
163+
options?.hostRefusal?.test(head),
164+
);
165+
return row && { ...row[1], hostRefusal: true };
167166
}
168167

169168
export function classifyAndroidAdbFailure(
@@ -174,8 +173,8 @@ export function classifyAndroidAdbFailure(
174173
const stdoutText = stdout.toLowerCase();
175174
const refusal = classifyHostRefusal(stderrText, stdoutText);
176175
if (refusal) return refusal;
177-
for (const [pattern, classification, matchStdout] of ANDROID_ADB_FAILURE_MATCHERS) {
178-
if (pattern.test(stderrText) || (matchStdout && pattern.test(stdoutText))) {
176+
for (const [pattern, classification, options] of ANDROID_ADB_FAILURE_MATCHERS) {
177+
if (pattern.test(stderrText) || (options?.matchStdout && pattern.test(stdoutText))) {
179178
return classification;
180179
}
181180
}

0 commit comments

Comments
 (0)