Repository navigation
fix(devices): parse devices -l fields by key, not by position - #582
Merged
Merged
Conversation
listDevicesWithPaths() split each line of host:devices-l by position, assuming id, state, path, product, model, device and transport_id. adb prints the path (usb:...) only for USB devices on Linux and macOS, so for emulators, network devices and every device on Windows the fields shifted: path was "product:...", product "model:..." and transportId undefined. Where the positions matched, product, model and device kept their "product:"-style prefixes. The fields are now matched by their key (usb, product, model, device, transport_id) in any order. path keeps its usb: prefix, as documented and as getDevicePath() returns it. Fields adb doesn't print are undefined, and a state with spaces, such as "no permissions (...)", is kept whole. Fixes #222 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Karol Wrótniak <[email protected]>
adb prints "(no serial number)" for a device with an empty serial, and the whitespace split made its id "(no". It's now kept whole, as listDevices() returns it. adb replaces spaces in model: but not in product: or device:, so those values can contain spaces. A token after the first field that isn't a known key:value field is now appended to the previous field's value instead of dropped. The loop tracks whether a field was seen with a flag instead of counting the fields on every token. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Karol Wrótniak <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
client.listDevicesWithPaths()(host:devices-l) split each line by position, assumingid state path product model device transport_id. adb prints the path (usb:…) only for USB devices on Linux and macOS, and only the fields it knows, so:path: 'product:…',product: 'model:…',model: 'device:…',device: 'transport_id:…',transportId: undefined.product: 'product:OnePlus7'.Real output on an API 30 emulator, also connected over TCP:
master parsed it as
{"id":"emulator-5620","type":"device","path":"product:sdk_gphone_arm64","product":"model:sdk_gphone_arm64","model":"device:emulator_arm64","device":"transport_id:8"}.Fix
The fields are matched by key (
usb,product,model,deviceandtransport_id), in any order, and each one only if present. The public field names stay the same:pathkeeps itsusb:prefix (usb:1-1), as the README documents and asgetDevicePath()returns it. It'sundefinedwhen adb prints no path.product,modelanddevicehold only the value, andtransportIdstays a string.no permissions (…); see [http://…], is kept whole, and itshttp:isn't taken for a field.DeviceWithPathJSDoc list the fields.Visible change: on Linux and macOS USB devices,
product,modelanddeviceused to include the key ('product:OnePlus7') and now don't ('OnePlus7').Question for the maintainer:
path,product,model,deviceandtransportIdcan beundefined(they could before too, through the shifting), butDeviceWithPathstill types them asstring. Making them optional would be more honest, but strict TypeScript consumers that assign them to astringwould stop compiling, so I left the types alone and documented it. Happy to switch them to?:if you prefer.stf doesn't use
listDevicesWithPaths().Fixes #222
Verification
test/adb/command/host/deviceswithpaths.mts: the Linux USB line, the Windows line from the issue, the real emulator and TCP lines above, a reordered line,unauthorizedandofflinelines with missing fields, ano permissions (…)state, and empty output. On master, 6 of its 8 tests fail.npm test: 292 passing.npm run lintis clean.localhost:5621), with this branch:{"id":"emulator-5620","type":"device","product":"sdk_gphone_arm64","model":"sdk_gphone_arm64","device":"emulator_arm64","transportId":"8"}, withpathundefined.getDevicePath()returns"unknown"for these, which is adb's ownget-devpathanswer when there's no path.usb:and Windows cases are covered only by the unit tests' lines.Review follow-up (commit
1936c09)(no serial number), which adb prints for an empty serial, is kept whole as theid, aslistDevices()returns it, instead of(no.model:but not inproduct:ordevice:. A token after the first field that isn't a knownkey:field is now appended, with a space, to the previous field's value instead of dropped. For example,product:My Product … device:my device xgivesproduct: 'My Product'anddevice: 'my device x'.inFields) instead of countingObject.keys(fields)on every token.npm test294 passing, lint clean.🤖 Generated with Claude Code