Skip to content

Commit f58cf19

Browse files
authored
adb: Explicitly match package name in pm list package output (#135)
The positional `FILTER` argument to `pm list package` works as a substring match: if you have a package named `foo.bar.baz` and `foo.bar.baz_debug` for example, and try to run `foo.bar.baz`, both packages will be returned, the `_debug`-suffixed one likely first, and the wrong UID ends up being used as `logcat` filter. To counter that we could use the very slow and extremely verbose (thousands of lines) `pm dump PACKAGE`, _or_ look for the right explicit text match in the line-based `package:foo.bar.baz uid:1234` output from `pm list package`: the latter approach is chosen here.
1 parent fffc0b4 commit f58cf19

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

xbuild/src/devices/adb.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,16 @@ impl Adb {
220220
std::str::from_utf8(&output.stderr)?.trim()
221221
);
222222
let output = std::str::from_utf8(&output.stdout)?;
223-
let uid = output
224-
.split_whitespace()
225-
.find_map(|kv| kv.strip_prefix("uid:"))
226-
.with_context(|| format!("Could not find `uid:`` in output `{output}`"))?;
223+
let (_package, uid) = output
224+
.lines()
225+
.filter_map(|line| line.split_once(' '))
226+
// `pm list package` uses the id as a substring filter; make sure
227+
// we select the right package in case it returns multiple matches:
228+
.find(|(package, _uid)| package.strip_prefix("package:") == Some(id))
229+
.with_context(|| format!("Could not find `package:{id}` in output `{output}`"))?;
230+
let uid = uid
231+
.strip_prefix("uid:")
232+
.with_context(|| format!("Could not find `uid:` in output `{output}`"))?;
227233
Ok(uid.parse()?)
228234
}
229235

0 commit comments

Comments
 (0)