Skip to content

Commit 9c98b26

Browse files
authored
fix(tool): parse ADB device lists by connection state (flutter#189973)
Fixes flutter#189972 Related to flutter#189430 and flutter#189369. This supersedes the narrower parser approach from closed PR flutter#189431. ## Summary ADB formats long device-list rows as [`%-22s %s`](https://android.googlesource.com/platform/packages/modules/adb/+/1cf2f017d312f73b3dc53bda85ef2610e35a80e9/transport.cpp#1410): the serial field has a minimum width, so a long wireless serial can be separated from its connection state by a single space. mDNS conflict suffixes can also introduce whitespace inside the serial. [PR flutter#189369](flutter#189369) correctly protects serials containing whitespace by requiring two spaces or a tab before the state. [PR flutter#189431](flutter#189431) proposed a narrower one-space fallback for serials without whitespace and was closed after flutter#189369 landed. The combined case has both an mDNS conflict suffix and a valid single-space state delimiter, so neither earlier rule accepts it. This change parses the explicit known ADB connection-state field instead of inferring a boundary from whitespace width. The serial capture is greedy so state-like words and mDNS conflict suffixes remain part of the serial, and right-side padding from ADB's minimum-width field is removed. ## Tests Added regression coverage for: - ordinary single-space wireless mDNS serials; - mDNS conflict serials containing ` (2)` with `device` and `offline` states; - every state currently recognized by Flutter Tools; - serials that contain state-like words; and - existing padded and tab-separated output forms. Ran: ```text flutter test packages/flutter_tools/test/general.shard/android/android_device_discovery_test.dart flutter analyze packages/flutter_tools ``` The device-discovery test file passed with 16 tests, and analysis reported no issues. ## Real-device validation After deleting `bin/cache/flutter_tools.snapshot`, unmodified `upstream/master` at `0ed149e63db1f68d2042063baf0a99d969ccd84b` omitted the wireless Android device and reported an unexpected ADB parsing failure. With this patch, `flutter devices -v` listed the device with the complete mDNS conflict serial and successfully used that serial for `adb -s ... shell getprop`. ## Scope This PR changes only Flutter Tools parsing and its Android device-discovery tests. It does not change ADB, mDNS pairing, wireless transport behavior, or device-state handling after a row has been parsed. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
1 parent ec66198 commit 9c98b26

2 files changed

Lines changed: 87 additions & 8 deletions

File tree

‎packages/flutter_tools/lib/src/android/android_device_discovery.dart‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,17 @@ class AndroidDevices extends PollingDeviceDiscovery {
113113
// Parses the output of `adb devices -l`.
114114
//
115115
// The regex is structured as:
116-
// 1. Group 1 (Serial): Lazily matched to allow spaces in the serial (which
117-
// can happen during wireless ADB mDNS name conflicts, e.g., "device (2)").
118-
// The column separator requires at least two spaces or a tab to prevent
119-
// single spaces within a serial from being mis-matched.
116+
// 1. Group 1 (Serial): Greedily matched before the state. ADB formats long
117+
// listings as `%-22s %s`, so the width is a minimum: serials longer than
118+
// 22 characters are followed by one space. Serials can also contain
119+
// whitespace, such as a wireless mDNS conflict suffix (`device (2)`).
120120
// 2. Group 2 (State): Matches known ADB device states explicitly, including
121121
// "no permissions" (which contains a space). Explicitly listing states
122-
// prevents false positive state matching on extra device info/attributes
123-
// or serial name components.
122+
// lets the greedy serial capture retain state-like serial components.
124123
// 3. Group 3 (Extra Info): Optional trailing details (e.g. key:value pairs
125124
// like "product:mokey model:mokey device:mokey transport_id:1" or "usb:123").
126125
static final _kDeviceRegex = RegExp(
127-
r'^(.*?)(?:\s{2,}|\t+)'
126+
r'^(.*)\s+'
128127
r'(device|offline|unauthorized|no permissions|bootloader|recovery|sideload|rescue|connecting|authorizing|host|unknown)'
129128
r'(?:\s+(.*)|$)',
130129
);
@@ -163,7 +162,9 @@ class AndroidDevices extends PollingDeviceDiscovery {
163162
if (_kDeviceRegex.hasMatch(line)) {
164163
final Match match = _kDeviceRegex.firstMatch(line)!;
165164

166-
final String deviceID = match[1]!;
165+
// The greedy serial capture includes the optional padding from ADB's
166+
// minimum-width serial field.
167+
final String deviceID = match[1]!.trimRight();
167168
final String deviceState = match[2]!;
168169
String? rest = match[3];
169170

‎packages/flutter_tools/test/general.shard/android/android_device_discovery_test.dart‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,84 @@ adb-ZY22MGW35T-Z3uXXq (2)._adb-tls-connect._tcp device product:vantage_ge mod
238238
'expectedId': '127.0.0.1:5555',
239239
'expectedStatus': 'success',
240240
},
241+
// ADB long listings use a minimum-width serial column, so a serial that
242+
// is 22 characters or longer can be followed by only one space.
243+
<String, String>{
244+
'input':
245+
'adb-0123456789abcdef._adb-tls-connect._tcp device product:socrates model:22127RK46C device:socrates transport_id:1',
246+
'expectedId': 'adb-0123456789abcdef._adb-tls-connect._tcp',
247+
'expectedStatus': 'success',
248+
},
249+
// mDNS conflict suffixes introduce whitespace into the serial itself.
250+
<String, String>{
251+
'input':
252+
'adb-0123456789abcdef (2)._adb-tls-connect._tcp device product:socrates model:22127RK46C device:socrates transport_id:1',
253+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
254+
'expectedStatus': 'success',
255+
},
256+
<String, String>{
257+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp offline',
258+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
259+
'expectedStatus': 'offline',
260+
},
261+
// Serial contents may themselves include known ADB state names.
262+
<String, String>{
263+
'input': 'my device offline device',
264+
'expectedId': 'my device offline',
265+
'expectedStatus': 'success',
266+
},
267+
// Exercise every state currently recognized by Flutter with the ADB
268+
// single-space form and a serial containing whitespace.
269+
<String, String>{
270+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp unauthorized',
271+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
272+
'expectedStatus': 'unauthorized',
273+
},
274+
<String, String>{
275+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp no permissions',
276+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
277+
'expectedStatus': 'no permissions',
278+
},
279+
<String, String>{
280+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp bootloader',
281+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
282+
'expectedStatus': 'success',
283+
},
284+
<String, String>{
285+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp recovery',
286+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
287+
'expectedStatus': 'success',
288+
},
289+
<String, String>{
290+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp sideload',
291+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
292+
'expectedStatus': 'success',
293+
},
294+
<String, String>{
295+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp rescue',
296+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
297+
'expectedStatus': 'success',
298+
},
299+
<String, String>{
300+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp connecting',
301+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
302+
'expectedStatus': 'success',
303+
},
304+
<String, String>{
305+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp authorizing',
306+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
307+
'expectedStatus': 'success',
308+
},
309+
<String, String>{
310+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp host',
311+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
312+
'expectedStatus': 'success',
313+
},
314+
<String, String>{
315+
'input': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp unknown',
316+
'expectedId': 'adb-0123456789abcdef (2)._adb-tls-connect._tcp',
317+
'expectedStatus': 'success',
318+
},
241319
// States that should go to diagnostics
242320
<String, String>{
243321
'input': '015d172c98400a03 offline',

0 commit comments

Comments
 (0)