Fix try not working with let, etc. (#13885)

# Description
Partialy addresses #13868. `try` does not catch non-zero exit code
errors from the last command in a pipeline if the result is assigned to
a variable using `let` (or `mut`).

This was fixed by adding a new `OutDest::Value` case. This is used when
the pipeline is in a "value" position. I.e., it will be collected into a
value. This ended up replacing most of the usages of `OutDest::Capture`.
So, this PR also renames `OutDest::Capture` to `OutDest::PipeSeparate`
to better fit the few remaining use cases for it.

# User-Facing Changes
Bug fix.

# Tests + Formatting
Added two tests.
This commit is contained in:
Ian Manske
2024-09-23 04:44:25 -07:00
committed by GitHub
parent 2541a712e4
commit 03ee54a4df
32 changed files with 127 additions and 98 deletions

View File

@ -270,7 +270,7 @@ impl Command for Save {
}
fn pipe_redirection(&self) -> (Option<OutDest>, Option<OutDest>) {
(Some(OutDest::Capture), Some(OutDest::Capture))
(Some(OutDest::PipeSeparate), Some(OutDest::PipeSeparate))
}
}

View File

@ -158,7 +158,7 @@ use it in your pipeline."#
let tee_thread = spawn_tee(info.clone(), eval_block)?;
let tee = IoTee::new(stderr, tee_thread);
match stack.stderr() {
OutDest::Pipe | OutDest::Capture => {
OutDest::Pipe | OutDest::PipeSeparate | OutDest::Value => {
child.stderr = Some(ChildPipe::Tee(Box::new(tee)));
Ok(None)
}
@ -176,7 +176,7 @@ use it in your pipeline."#
if let Some(stdout) = child.stdout.take() {
match stack.stdout() {
OutDest::Pipe | OutDest::Capture => {
OutDest::Pipe | OutDest::PipeSeparate | OutDest::Value => {
child.stdout = Some(stdout);
Ok(())
}
@ -191,7 +191,7 @@ use it in your pipeline."#
let stderr_thread = if let Some(stderr) = child.stderr.take() {
let info = info.clone();
match stack.stderr() {
OutDest::Pipe | OutDest::Capture => {
OutDest::Pipe | OutDest::PipeSeparate | OutDest::Value => {
child.stderr = Some(stderr);
Ok(None)
}
@ -213,7 +213,7 @@ use it in your pipeline."#
let tee_thread = spawn_tee(info.clone(), eval_block)?;
let tee = IoTee::new(stdout, tee_thread);
match stack.stdout() {
OutDest::Pipe | OutDest::Capture => {
OutDest::Pipe | OutDest::PipeSeparate | OutDest::Value => {
child.stdout = Some(ChildPipe::Tee(Box::new(tee)));
Ok(())
}
@ -280,7 +280,7 @@ use it in your pipeline."#
}
fn pipe_redirection(&self) -> (Option<OutDest>, Option<OutDest>) {
(Some(OutDest::Capture), Some(OutDest::Capture))
(Some(OutDest::PipeSeparate), Some(OutDest::PipeSeparate))
}
}

View File

@ -93,6 +93,6 @@ impl Command for Complete {
}
fn pipe_redirection(&self) -> (Option<OutDest>, Option<OutDest>) {
(Some(OutDest::Capture), Some(OutDest::Capture))
(Some(OutDest::PipeSeparate), Some(OutDest::PipeSeparate))
}
}

View File

@ -340,7 +340,7 @@ fn write_pipeline_data(
} else if let PipelineData::Value(Value::Binary { val, .. }, ..) = data {
writer.write_all(&val)?;
} else {
stack.start_capture();
stack.start_collect_value();
// Turn off color as we pass data through
Arc::make_mut(&mut engine_state.config).use_ansi_coloring = false;
@ -367,7 +367,7 @@ pub fn command_not_found(
) -> ShellError {
// Run the `command_not_found` hook if there is one.
if let Some(hook) = &stack.get_config(engine_state).hooks.command_not_found {
let mut stack = stack.start_capture();
let mut stack = stack.start_collect_value();
// Set a special environment variable to avoid infinite loops when the
// `command_not_found` hook triggers itself.
let canary = "ENTERED_COMMAND_NOT_FOUND";