Skip to content

fix(devices): parse devices -l fields by key, not by position - #582

Merged
koral-- merged 2 commits into
masterfrom
fix/devices-with-paths
Nov 7, 2026
Merged

koral-- merged 2 commits into
masterfrom
fix/devices-with-paths

Conversation

@koral--

@koral-- koral-- commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Problem

client.listDevicesWithPaths() (host:devices-l) split each line by position, assuming id 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:

  • For emulators, network devices and every device on Windows, all fields shifted: path: 'product:…', product: 'model:…', model: 'device:…', device: 'transport_id:…', transportId: undefined.
  • Where the positions did match, the values kept their keys: product: 'product:OnePlus7'.

Real output on an API 30 emulator, also connected over TCP:

emulator-5620          device product:sdk_gphone_arm64 model:sdk_gphone_arm64 device:emulator_arm64 transport_id:8
localhost:5621         device product:sdk_gphone_arm64 model:sdk_gphone_arm64 device:emulator_arm64 transport_id:19

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, device and transport_id), in any order, and each one only if present. The public field names stay the same:

  • path keeps its usb: prefix (usb:1-1), as the README documents and as getDevicePath() returns it. It's undefined when adb prints no path.
  • product, model and device hold only the value, and transportId stays a string.
  • A state with spaces, such as no permissions (…); see [http://…], is kept whole, and its http: isn't taken for a field.
  • The README and the DeviceWithPath JSDoc list the fields.

Visible change: on Linux and macOS USB devices, product, model and device used to include the key ('product:OnePlus7') and now don't ('OnePlus7').

Question for the maintainer: path, product, model, device and transportId can be undefined (they could before too, through the shifting), but DeviceWithPath still types them as string. Making them optional would be more honest, but strict TypeScript consumers that assign them to a string would 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

  • New 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, unauthorized and offline lines with missing fields, a no permissions (…) state, and empty output. On master, 6 of its 8 tests fail. npm test: 292 passing. npm run lint is clean.
  • Emulators (API 30 and API 36, plus localhost:5621), with this branch:
    {"id":"emulator-5620","type":"device","product":"sdk_gphone_arm64","model":"sdk_gphone_arm64","device":"emulator_arm64","transportId":"8"}, with path undefined. getDevicePath() returns "unknown" for these, which is adb's own get-devpath answer when there's no path.
  • Not tested on a real USB device or on Windows: the 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 the id, as listDevices() returns it, instead of (no.
  • adb replaces spaces in model: but not in product: or device:. A token after the first field that isn't a known key: field is now appended, with a space, to the previous field's value instead of dropped. For example, product:My Product … device:my device x gives product: 'My Product' and device: 'my device x'.
  • The loop uses a flag (inFields) instead of counting Object.keys(fields) on every token.
  • Tests for both cases. npm test 294 passing, lint clean.
  • The emulator output is unchanged.

🤖 Generated with Claude Code

koral-- and others added 2 commits November 7, 2026 00:36
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]>
@koral--
koral-- merged commit 4cb04cd into master Nov 7, 2026
6 checks passed
@koral--
koral-- deleted the fix/devices-with-paths branch November 7, 2026 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad parsing of protocol in "Devices With Path" command on windows

1 participant