Skip to content

Commit f308f20

Browse files
authored
adb: Print stderr on negative status when capturing output (#108)
When capturing process output both `stdout` and `stderr` are collected by Rust instead of forwarded to the console. When such a command has a non-zero exit code, at the very least emit `stderr` within the `anyhow` error message so that users know what's up: for me it's typically: Error: adb getprop exited with code Some(1) that now results in a more sensible: Error: adb getprop exited with code Some(1): adb: device '<id>' not found
1 parent ecd6840 commit f308f20

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

xbuild/src/devices/adb.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,9 @@ impl Adb {
4646
let output = Command::new(&self.0).arg("devices").output()?;
4747
anyhow::ensure!(
4848
output.status.success(),
49-
"adb devices exited with code {:?}",
50-
output.status.code()
49+
"adb devices exited with code {:?}: {}",
50+
output.status.code(),
51+
std::str::from_utf8(&output.stderr)?.trim()
5152
);
5253
let mut lines = std::str::from_utf8(&output.stdout)?.lines();
5354
lines.next();
@@ -66,8 +67,9 @@ impl Adb {
6667
let output = self.shell(device, None).arg("getprop").arg(prop).output()?;
6768
anyhow::ensure!(
6869
output.status.success(),
69-
"adb getprop exited with code {:?}",
70-
output.status.code()
70+
"adb getprop exited with code {:?}: {}",
71+
output.status.code(),
72+
std::str::from_utf8(&output.stderr)?.trim()
7173
);
7274
Ok(std::str::from_utf8(&output.stdout)?.trim().to_string())
7375
}
@@ -164,8 +166,9 @@ impl Adb {
164166
.output()?;
165167
anyhow::ensure!(
166168
output.status.success(),
167-
"adb logcat exited with code {:?}",
168-
output.status.code()
169+
"adb logcat exited with code {:?}: {}",
170+
output.status.code(),
171+
std::str::from_utf8(&output.stderr)?.trim()
169172
);
170173
let line = std::str::from_utf8(&output.stdout)?.lines().nth(1).unwrap();
171174
Ok(line[..18].to_string())
@@ -174,7 +177,11 @@ impl Adb {
174177
fn pidof(&self, device: &str, id: &str) -> Result<u32> {
175178
loop {
176179
let output = self.shell(device, None).arg("pidof").arg(id).output()?;
177-
anyhow::ensure!(output.status.success(), "failed to get pid");
180+
anyhow::ensure!(
181+
output.status.success(),
182+
"failed to get pid: {}",
183+
std::str::from_utf8(&output.stderr)?.trim()
184+
);
178185
let pid = std::str::from_utf8(&output.stdout)?.trim();
179186
// may return multiple space separated pids if the old process hasn't exited yet.
180187
if pid.is_empty() || pid.split_once(' ').is_some() {
@@ -208,8 +215,9 @@ impl Adb {
208215
.output()?;
209216
anyhow::ensure!(
210217
output.status.success(),
211-
"adb forward exited with code {:?}",
212-
output.status.code()
218+
"adb forward exited with code {:?}: {}",
219+
output.status.code(),
220+
std::str::from_utf8(&output.stderr)?.trim()
213221
);
214222
Ok(std::str::from_utf8(&output.stdout)?.trim().parse()?)
215223
}
@@ -221,7 +229,11 @@ impl Adb {
221229
.arg("-c")
222230
.arg("pwd")
223231
.output()?;
224-
anyhow::ensure!(output.status.success(), "failed to get app dir");
232+
anyhow::ensure!(
233+
output.status.success(),
234+
"failed to get app dir: {}",
235+
std::str::from_utf8(&output.stderr)?.trim()
236+
);
225237
Ok(Path::new(std::str::from_utf8(&output.stdout)?.trim()).to_path_buf())
226238
}*/
227239

0 commit comments

Comments
 (0)