Skip to content

Commit fb4dd02

Browse files
committed
Fix handling of ignore in wait() and wait_with_all()
1 parent 2c7fc8c commit fb4dd02

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

src/child.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::process::pipefail_enabled;
12
use crate::{info, warn};
23
use crate::{process, CmdResult, FunResult};
34
use os_pipe::PipeReader;
@@ -31,13 +32,15 @@ impl CmdChildren {
3132

3233
/// Waits for the children processes to exit completely, returning the status that they exited with.
3334
pub fn wait(&mut self) -> CmdResult {
34-
// wait for the last child result
35-
let handle = self.children.pop().unwrap();
36-
if let Err(e) = handle.wait(true) {
37-
let _ = Self::wait_children(&mut self.children);
38-
return Err(e);
39-
}
40-
Self::wait_children(&mut self.children)
35+
let last_child = self.children.pop().unwrap();
36+
let last_child_res = last_child.wait(true);
37+
let other_children_res = (!pipefail_enabled())
38+
.then_some(Ok(()))
39+
.unwrap_or(Self::wait_children(&mut self.children));
40+
41+
self.ignore_error
42+
.then_some(Ok(()))
43+
.unwrap_or(last_child_res.and(other_children_res))
4144
}
4245

4346
fn wait_children(children: &mut Vec<CmdChild>) -> CmdResult {
@@ -149,21 +152,23 @@ impl FunChildren {
149152
}
150153

151154
fn inner_wait_with_all(&mut self, capture_stderr: bool) -> (CmdResult, String, String) {
152-
// wait for the last child result
153-
let last_handle = self.children.pop().unwrap();
154-
let mut stdout_buf = Vec::new();
155+
let mut stdout = Vec::new();
155156
let mut stderr = String::new();
156-
let last_res = last_handle.wait_with_all(capture_stderr, &mut stdout_buf, &mut stderr);
157-
let res = CmdChildren::wait_children(&mut self.children);
158-
let mut stdout: String = String::from_utf8_lossy(&stdout_buf).into();
157+
158+
let last_child = self.children.pop().unwrap();
159+
let last_child_res = last_child.wait_with_all(capture_stderr, &mut stdout, &mut stderr);
160+
let other_children_res = CmdChildren::wait_children(&mut self.children);
161+
let cmd_result = self
162+
.ignore_error
163+
.then_some(Ok(()))
164+
.unwrap_or(last_child_res.and(other_children_res));
165+
166+
let mut stdout: String = String::from_utf8_lossy(&stdout).into();
159167
if stdout.ends_with('\n') {
160168
stdout.pop();
161169
}
162-
if res.is_err() && !self.ignore_error && process::pipefail_enabled() {
163-
(res, stdout, stderr)
164-
} else {
165-
(last_res, stdout, stderr)
166-
}
170+
171+
(cmd_result, stdout, stderr)
167172
}
168173
}
169174

@@ -200,7 +205,7 @@ impl CmdChild {
200205
StderrThread::new(&self.cmd, &self.file, self.line, self.stderr.take(), false);
201206
let res = self.handle.wait(&self.cmd, &self.file, self.line);
202207
if let Err(e) = res {
203-
if is_last || process::pipefail_enabled() {
208+
if is_last || pipefail_enabled() {
204209
return Err(e);
205210
}
206211
}

0 commit comments

Comments
 (0)