diff --git a/crates/nu-cli/src/commands/classified/block.rs b/crates/nu-cli/src/commands/classified/block.rs index fc0bf19a92..6959ffe301 100644 --- a/crates/nu-cli/src/commands/classified/block.rs +++ b/crates/nu-cli/src/commands/classified/block.rs @@ -19,6 +19,11 @@ pub async fn run_block( mut input: InputStream, ) -> Result { let mut output: Result = Ok(InputStream::empty()); + ctx.scope.enter_scope(); + for (_, definition) in block.definitions.iter() { + ctx.scope.add_definition(definition.clone()); + } + for group in &block.block { match output { Ok(inp) if inp.is_empty() => {} @@ -26,7 +31,7 @@ pub async fn run_block( // Run autoview on the values we've seen so far // We may want to make this configurable for other kinds of hosting if let Some(autoview) = ctx.get_command("autoview") { - let mut output_stream = ctx + let mut output_stream = match ctx .run_command( autoview, Tag::unknown(), @@ -39,32 +44,49 @@ pub async fn run_block( ), inp, ) - .await?; + .await + { + Ok(x) => x, + Err(e) => { + ctx.scope.exit_scope(); + return Err(e); + } + }; match output_stream.try_next().await { Ok(Some(ReturnSuccess::Value(Value { value: UntaggedValue::Error(e), .. - }))) => return Err(e), + }))) => { + ctx.scope.exit_scope(); + return Err(e); + } Ok(Some(_item)) => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); + ctx.scope.exit_scope(); return Err(err.clone()); } if ctx.ctrl_c.load(Ordering::SeqCst) { + ctx.scope.exit_scope(); return Ok(InputStream::empty()); } } Ok(None) => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); + ctx.scope.exit_scope(); return Err(err.clone()); } } - Err(e) => return Err(e), + Err(e) => { + ctx.scope.exit_scope(); + return Err(e); + } } } } Err(e) => { + ctx.scope.exit_scope(); return Err(e); } } @@ -79,10 +101,14 @@ pub async fn run_block( Ok(Some(ReturnSuccess::Value(Value { value: UntaggedValue::Error(e), .. - }))) => return Err(e), + }))) => { + ctx.scope.exit_scope(); + return Err(e); + } Ok(Some(_item)) => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); + ctx.scope.exit_scope(); return Err(err.clone()); } if ctx.ctrl_c.load(Ordering::SeqCst) { @@ -91,19 +117,25 @@ pub async fn run_block( // causes lifetime issues. A future contribution // could attempt to return the current output. // https://github.com/nushell/nushell/pull/2830#discussion_r550319687 + ctx.scope.exit_scope(); return Ok(InputStream::empty()); } } Ok(None) => { if let Some(err) = ctx.get_errors().get(0) { ctx.clear_errors(); + ctx.scope.exit_scope(); return Err(err.clone()); } } - Err(e) => return Err(e), + Err(e) => { + ctx.scope.exit_scope(); + return Err(e); + } } } Err(e) => { + ctx.scope.exit_scope(); return Err(e); } } @@ -112,6 +144,7 @@ pub async fn run_block( input = InputStream::empty(); } } + ctx.scope.exit_scope(); output } diff --git a/crates/nu-parser/src/parse.rs b/crates/nu-parser/src/parse.rs index 6e18edccfc..8dc6f9ea05 100644 --- a/crates/nu-parser/src/parse.rs +++ b/crates/nu-parser/src/parse.rs @@ -2142,6 +2142,8 @@ fn parse_definition(call: &LiteCommand, scope: &dyn ParserScope) -> Option Option