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

@@ -108,7 +108,7 @@ pub(crate) fn compile_call(
working_set,
builder,
expr,
RedirectModes::capture_out(arg.span()),
RedirectModes::value(arg.span()),
None,
arg_reg,
)?;

View File

@@ -90,7 +90,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
part_expr,
RedirectModes::capture_out(part_expr.span),
RedirectModes::value(part_expr.span),
None,
reg,
)?;
@@ -148,7 +148,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
subexpr,
RedirectModes::capture_out(subexpr.span),
RedirectModes::value(subexpr.span),
None,
out_reg,
)?;
@@ -217,7 +217,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
expr,
RedirectModes::capture_out(expr.span),
RedirectModes::value(expr.span),
None,
reg,
)?;
@@ -265,7 +265,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
column,
RedirectModes::capture_out(column.span),
RedirectModes::value(column.span),
None,
reg,
)?;
@@ -290,7 +290,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
item,
RedirectModes::capture_out(item.span),
RedirectModes::value(item.span),
None,
item_reg,
)?;
@@ -337,7 +337,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
key,
RedirectModes::capture_out(key.span),
RedirectModes::value(key.span),
None,
key_reg,
)?;
@@ -345,7 +345,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
val,
RedirectModes::capture_out(val.span),
RedirectModes::value(val.span),
None,
val_reg,
)?;
@@ -365,7 +365,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
expr,
RedirectModes::capture_out(expr.span),
RedirectModes::value(expr.span),
None,
reg,
)?;
@@ -449,7 +449,7 @@ pub(crate) fn compile_expression(
// general, which shouldn't be captured any differently than they otherwise
// would be.
if !full_cell_path.tail.is_empty() {
RedirectModes::capture_out(expr.span)
RedirectModes::value(expr.span)
} else {
redirect_modes
},
@@ -491,7 +491,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
exprs_iter.next().expect("peek() was Some"),
RedirectModes::capture_out(expr.span),
RedirectModes::value(expr.span),
None,
out_reg,
)?;
@@ -507,7 +507,7 @@ pub(crate) fn compile_expression(
working_set,
builder,
expr,
RedirectModes::capture_out(expr.span),
RedirectModes::value(expr.span),
None,
scratch_reg,
)?;

View File

@@ -47,7 +47,7 @@ pub(crate) fn compile_if(
working_set,
builder,
condition,
RedirectModes::capture_out(condition.span),
RedirectModes::value(condition.span),
None,
condition_reg,
)?;
@@ -181,7 +181,7 @@ pub(crate) fn compile_match(
working_set,
builder,
match_expr,
RedirectModes::capture_out(match_expr.span),
RedirectModes::value(match_expr.span),
None,
match_reg,
)?;
@@ -233,7 +233,7 @@ pub(crate) fn compile_match(
working_set,
builder,
guard,
RedirectModes::capture_out(guard.span),
RedirectModes::value(guard.span),
None,
guard_reg,
)?;
@@ -319,7 +319,7 @@ pub(crate) fn compile_let(
working_set,
builder,
block,
RedirectModes::capture_out(call.head),
RedirectModes::value(call.head),
Some(io_reg),
io_reg,
)?;
@@ -427,7 +427,7 @@ pub(crate) fn compile_try(
working_set,
builder,
catch_expr,
RedirectModes::capture_out(catch_expr.span),
RedirectModes::value(catch_expr.span),
None,
closure_reg,
)?;
@@ -655,7 +655,7 @@ pub(crate) fn compile_while(
working_set,
builder,
cond_arg,
RedirectModes::capture_out(call.head),
RedirectModes::value(call.head),
None,
io_reg,
)?;
@@ -739,7 +739,7 @@ pub(crate) fn compile_for(
working_set,
builder,
in_expr,
RedirectModes::capture_out(in_expr.span),
RedirectModes::value(in_expr.span),
None,
stream_reg,
)?;
@@ -867,7 +867,7 @@ pub(crate) fn compile_return(
working_set,
builder,
arg_expr,
RedirectModes::capture_out(arg_expr.span),
RedirectModes::value(arg_expr.span),
None,
io_reg,
)?;

View File

@@ -35,7 +35,7 @@ pub(crate) fn compile_binary_op(
working_set,
builder,
rhs,
RedirectModes::capture_out(rhs.span),
RedirectModes::value(rhs.span),
None,
out_reg,
)?;
@@ -53,7 +53,7 @@ pub(crate) fn compile_binary_op(
working_set,
builder,
lhs,
RedirectModes::capture_out(lhs.span),
RedirectModes::value(lhs.span),
None,
lhs_reg,
)?;
@@ -73,7 +73,7 @@ pub(crate) fn compile_binary_op(
working_set,
builder,
rhs,
RedirectModes::capture_out(rhs.span),
RedirectModes::value(rhs.span),
None,
lhs_reg,
)?;
@@ -96,7 +96,7 @@ pub(crate) fn compile_binary_op(
working_set,
builder,
rhs,
RedirectModes::capture_out(rhs.span),
RedirectModes::value(rhs.span),
None,
lhs_reg,
)?;
@@ -118,7 +118,7 @@ pub(crate) fn compile_binary_op(
working_set,
builder,
rhs,
RedirectModes::capture_out(rhs.span),
RedirectModes::value(rhs.span),
None,
rhs_reg,
)?;
@@ -277,7 +277,7 @@ pub(crate) fn compile_assignment(
working_set,
builder,
&path.head,
RedirectModes::capture_out(path.head.span),
RedirectModes::value(path.head.span),
None,
head_reg,
)?;

View File

@@ -14,9 +14,9 @@ pub(crate) struct RedirectModes {
}
impl RedirectModes {
pub(crate) fn capture_out(span: Span) -> Self {
pub(crate) fn value(span: Span) -> Self {
RedirectModes {
out: Some(RedirectMode::Capture.into_spanned(span)),
out: Some(RedirectMode::Value.into_spanned(span)),
err: None,
}
}
@@ -46,7 +46,7 @@ pub(crate) fn redirection_target_to_mode(
working_set,
builder,
expr,
RedirectModes::capture_out(*redir_span),
RedirectModes::value(*redir_span),
None,
path_reg,
)?;
@@ -148,7 +148,8 @@ pub(crate) fn out_dest_to_redirect_mode(
out_dest
.map(|out_dest| match out_dest {
OutDest::Pipe => Ok(RedirectMode::Pipe),
OutDest::Capture => Ok(RedirectMode::Capture),
OutDest::PipeSeparate => Ok(RedirectMode::PipeSeparate),
OutDest::Value => Ok(RedirectMode::Value),
OutDest::Null => Ok(RedirectMode::Null),
OutDest::Inherit => Ok(RedirectMode::Inherit),
OutDest::File(_) => Err(CompileError::InvalidRedirectMode { span }),

View File

@@ -23,7 +23,7 @@ pub fn get_full_help(
// internally call several commands (`table`, `ansi`, `nu-highlight`) and get their
// `PipelineData` using this `Stack`, any other output should not be redirected like the main
// execution.
let stack = &mut stack.start_capture();
let stack = &mut stack.start_collect_value();
let signature = engine_state
.get_signature(command)
@@ -202,7 +202,7 @@ fn get_documentation(
));
}
let caller_stack = &mut Stack::new().capture();
let caller_stack = &mut Stack::new().collect_value();
if let Ok(result) = eval_call::<WithoutDebug>(
engine_state,
caller_stack,
@@ -329,7 +329,7 @@ fn update_ansi_from_config(
theme_component: &str,
) {
if let Some(color) = &nu_config.color_config.get(theme_component) {
let caller_stack = &mut Stack::new().capture();
let caller_stack = &mut Stack::new().collect_value();
let span = Span::unknown();
let span_id = UNKNOWN_SPAN_ID;

View File

@@ -235,7 +235,7 @@ pub fn eval_expression<D: DebugContext>(
stack: &mut Stack,
expr: &Expression,
) -> Result<Value, ShellError> {
let stack = &mut stack.start_capture();
let stack = &mut stack.start_collect_value();
<EvalRuntime as Eval>::eval::<D>(engine_state, stack, expr)
}
@@ -278,7 +278,7 @@ pub fn eval_expression_with_input<D: DebugContext>(
let block = engine_state.get_block(*block_id);
if !full_cell_path.tail.is_empty() {
let stack = &mut stack.start_capture();
let stack = &mut stack.start_collect_value();
// FIXME: protect this collect with ctrl-c
input = eval_subexpression::<D>(engine_state, stack, block, input)?
.into_value(*span)?
@@ -325,7 +325,7 @@ fn eval_redirection<D: DebugContext>(
}
RedirectionTarget::Pipe { .. } => {
let dest = match next_out {
None | Some(OutDest::Capture) => OutDest::Pipe,
None | Some(OutDest::PipeSeparate) => OutDest::Pipe,
Some(next) => next,
};
Ok(Redirection::Pipe(dest))
@@ -357,7 +357,7 @@ fn eval_element_redirection<D: DebugContext>(
let stderr = eval_redirection::<D>(engine_state, stack, target, None)?;
if matches!(stderr, Redirection::Pipe(OutDest::Pipe)) {
let dest = match next_out {
None | Some(OutDest::Capture) => OutDest::Pipe,
None | Some(OutDest::PipeSeparate) => OutDest::Pipe,
Some(next) => next,
};
// e>| redirection, don't override current stack `stdout`

View File

@@ -1414,7 +1414,8 @@ fn eval_redirection(
) -> Result<Option<Redirection>, ShellError> {
match mode {
RedirectMode::Pipe => Ok(Some(Redirection::Pipe(OutDest::Pipe))),
RedirectMode::Capture => Ok(Some(Redirection::Pipe(OutDest::Capture))),
RedirectMode::PipeSeparate => Ok(Some(Redirection::Pipe(OutDest::PipeSeparate))),
RedirectMode::Value => Ok(Some(Redirection::Pipe(OutDest::Value))),
RedirectMode::Null => Ok(Some(Redirection::Pipe(OutDest::Null))),
RedirectMode::Inherit => Ok(Some(Redirection::Pipe(OutDest::Inherit))),
RedirectMode::File { file_num } => {