Skip to content

Commit

Permalink
Fix do -p not waiting for external commands (#13881)
Browse files Browse the repository at this point in the history
# Description
Similar to #13870 (thanks @WindSoilder), this PR adds a boolean which
determines whether to ignore any errors from an external command. This
is in order to fix #13876. I.e., `do -p` does not wait for externals to
complete before continuing.

# User-Facing Changes
Bug fix.

# Tests + Formatting
Added a test.
  • Loading branch information
IanManske authored Sep 22, 2024
1 parent cf5fec6 commit cd0d036
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion crates/nu-cmd-lang/src/core_commands/do_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl Command for Do {
&& !matches!(caller_stack.stdout(), OutDest::Pipe | OutDest::Capture) =>
{
if let ByteStreamSource::Child(child) = stream.source_mut() {
child.set_exit_code(0)
child.ignore_error();
}
Ok(PipelineData::ByteStream(stream, metadata))
}
Expand Down
7 changes: 7 additions & 0 deletions crates/nu-command/tests/commands/do_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,10 @@ fn run_closure_with_it_using() {
assert!(actual.err.is_empty());
assert_eq!(actual.out, "3");
}

#[test]
fn waits_for_external() {
let actual = nu!(r#"do -p { nu -c 'sleep 1sec; print before; exit 1'}; print after"#);
assert!(actual.err.is_empty());
assert_eq!(actual.out, "beforeafter");
}
17 changes: 12 additions & 5 deletions crates/nu-protocol/src/process/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl ExitStatusFuture {
core_dumped: true, ..
},
)) => {
status.check_ok(span)?;
status.check_ok(false, span)?;
Ok(status)
}
Ok(Ok(status)) => Ok(status),
Expand Down Expand Up @@ -109,6 +109,7 @@ pub struct ChildProcess {
pub stdout: Option<ChildPipe>,
pub stderr: Option<ChildPipe>,
exit_status: ExitStatusFuture,
ignore_error: bool,
span: Span,
}

Expand Down Expand Up @@ -155,12 +156,14 @@ impl ChildProcess {
exit_status: exit_status
.map(ExitStatusFuture::Running)
.unwrap_or(ExitStatusFuture::Finished(Ok(ExitStatus::Exited(0)))),
ignore_error: false,
span,
}
}

pub fn set_exit_code(&mut self, exit_code: i32) {
self.exit_status = ExitStatusFuture::Finished(Ok(ExitStatus::Exited(exit_code)));
pub fn ignore_error(&mut self) -> &mut Self {
self.ignore_error = true;
self
}

pub fn span(&self) -> Span {
Expand All @@ -182,7 +185,9 @@ impl ChildProcess {
Vec::new()
};

self.exit_status.wait(self.span)?.check_ok(self.span)?;
self.exit_status
.wait(self.span)?
.check_ok(self.ignore_error, self.span)?;

Ok(bytes)
}
Expand Down Expand Up @@ -223,7 +228,9 @@ impl ChildProcess {
consume_pipe(stderr).err_span(self.span)?;
}

self.exit_status.wait(self.span)?.check_ok(self.span)
self.exit_status
.wait(self.span)?
.check_ok(self.ignore_error, self.span)
}

pub fn try_wait(&mut self) -> Result<Option<ExitStatus>, ShellError> {
Expand Down
8 changes: 5 additions & 3 deletions crates/nu-protocol/src/process/exit_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ impl ExitStatus {
}
}

pub fn check_ok(self, span: Span) -> Result<(), ShellError> {
pub fn check_ok(self, ignore_error: bool, span: Span) -> Result<(), ShellError> {
match self {
ExitStatus::Exited(exit_code) => {
if let Ok(exit_code) = exit_code.try_into() {
if ignore_error {
Ok(())
} else if let Ok(exit_code) = exit_code.try_into() {
Err(ShellError::NonZeroExitCode { exit_code, span })
} else {
Ok(())
Expand All @@ -38,7 +40,7 @@ impl ExitStatus {

let sig = Signal::try_from(signal);

if sig == Ok(Signal::SIGPIPE) {
if sig == Ok(Signal::SIGPIPE) || (ignore_error && !core_dumped) {
// Processes often exit with SIGPIPE, but this is not an error condition.
Ok(())
} else {
Expand Down

0 comments on commit cd0d036

Please sign in to comment.