diff --git a/crates/nu-engine/src/eval.rs b/crates/nu-engine/src/eval.rs index 550eee6e1a..adfc2a795a 100644 --- a/crates/nu-engine/src/eval.rs +++ b/crates/nu-engine/src/eval.rs @@ -1102,7 +1102,18 @@ impl Eval for EvalRuntime { .get_block(block_id) .captures .iter() - .map(|&id| stack.get_var(id, span).map(|var| (id, var))) + .map(|&id| { + stack + .get_var(id, span) + .or_else(|_| { + engine_state + .get_var(id) + .const_val + .clone() + .ok_or(ShellError::VariableNotFoundAtRuntime { span }) + }) + .map(|var| (id, var)) + }) .collect::>()?; Ok(Value::closure(Closure { block_id, captures }, span)) diff --git a/tests/const_/mod.rs b/tests/const_/mod.rs index 9ef7667bc0..a76c19efdc 100644 --- a/tests/const_/mod.rs +++ b/tests/const_/mod.rs @@ -304,6 +304,19 @@ fn const_captures_work() { assert_eq!(actual.out, "xy"); } +#[test] +fn const_captures_in_closures_work() { + let module = "module foo { + const a = 'world' + export def bar [] { + 'hello ' + $a + } + }"; + let inp = &[module, "use foo", "do { foo bar }"]; + let actual = nu!(&inp.join("; ")); + assert_eq!(actual.out, "hello world"); +} + #[ignore = "TODO: Need to fix `overlay hide` to hide the constants brough by `overlay use`"] #[test] fn complex_const_overlay_use_hide() {