|
| 1 | +use crate::process::pipefail_enabled; |
1 | 2 | use crate::{info, warn};
|
2 | 3 | use crate::{process, CmdResult, FunResult};
|
3 | 4 | use os_pipe::PipeReader;
|
@@ -31,13 +32,15 @@ impl CmdChildren {
|
31 | 32 |
|
32 | 33 | /// Waits for the children processes to exit completely, returning the status that they exited with.
|
33 | 34 | 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)) |
41 | 44 | }
|
42 | 45 |
|
43 | 46 | fn wait_children(children: &mut Vec<CmdChild>) -> CmdResult {
|
@@ -149,21 +152,23 @@ impl FunChildren {
|
149 | 152 | }
|
150 | 153 |
|
151 | 154 | 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(); |
155 | 156 | 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(); |
159 | 167 | if stdout.ends_with('\n') {
|
160 | 168 | stdout.pop();
|
161 | 169 | }
|
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) |
167 | 172 | }
|
168 | 173 | }
|
169 | 174 |
|
@@ -200,7 +205,7 @@ impl CmdChild {
|
200 | 205 | StderrThread::new(&self.cmd, &self.file, self.line, self.stderr.take(), false);
|
201 | 206 | let res = self.handle.wait(&self.cmd, &self.file, self.line);
|
202 | 207 | if let Err(e) = res {
|
203 |
| - if is_last || process::pipefail_enabled() { |
| 208 | + if is_last || pipefail_enabled() { |
204 | 209 | return Err(e);
|
205 | 210 | }
|
206 | 211 | }
|
|
0 commit comments