From 60ca889443df27eec5bfc90822d2f5401366635d Mon Sep 17 00:00:00 2001 From: Bahex Date: Sun, 13 Jul 2025 02:31:16 +0300 Subject: [PATCH] fix(overlay): `overlay use` and `overlay hide` now update config state (#16154) - fixes #5986 - fixes #7760 - fixes #8856 - fixes #10592 - fixes #11082 # Description Unconditionally update the config state after each `overlay use` and `overlay hide`. The fix looks simple, but only because of the constant improvements and refactors to the codebase that have taken place over time made it possible. Fixing these issue when they were initially filed would have been much harder. # User-Facing Changes Overlays can add hooks, change color_config, update the config in general. # Tests + Formatting No tests added as I still haven't figured out how to simulate the repl in tests. # After Submitting N/A --------- Co-authored-by: Bahex <17417311+Bahex@users.noreply.github.com> --- .../src/core_commands/overlay/hide.rs | 1 + .../src/core_commands/overlay/use_.rs | 1 + tests/overlays/mod.rs | 68 +++++++++++++++++++ 3 files changed, 70 insertions(+) diff --git a/crates/nu-cmd-lang/src/core_commands/overlay/hide.rs b/crates/nu-cmd-lang/src/core_commands/overlay/hide.rs index 666fedead8..b747ccd504 100644 --- a/crates/nu-cmd-lang/src/core_commands/overlay/hide.rs +++ b/crates/nu-cmd-lang/src/core_commands/overlay/hide.rs @@ -96,6 +96,7 @@ impl Command for OverlayHide { for (name, val) in env_vars_to_keep { stack.add_env_var(name, val); } + stack.update_config(engine_state)?; Ok(PipelineData::empty()) } diff --git a/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs b/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs index bf29087a61..5ddf4c5f81 100644 --- a/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs +++ b/crates/nu-cmd-lang/src/core_commands/overlay/use_.rs @@ -178,6 +178,7 @@ impl Command for OverlayUse { } } else { caller_stack.add_overlay(overlay_name); + caller_stack.update_config(engine_state)?; } Ok(PipelineData::empty()) diff --git a/tests/overlays/mod.rs b/tests/overlays/mod.rs index cd101bda6d..55d9f5a347 100644 --- a/tests/overlays/mod.rs +++ b/tests/overlays/mod.rs @@ -918,6 +918,74 @@ fn overlay_use_export_env() { assert_eq!(actual_repl.out, "foo"); } +#[test] +fn overlay_use_export_env_config_affected() { + let inp = &[ + "mut out = []", + "$env.config.filesize.unit = 'metric'", + "$out ++= [(20MB | into string)]", + "module spam { export-env { $env.config.filesize.unit = 'binary' } }", + "overlay use spam", + "$out ++= [(20MiB | into string)]", + r#"$out | to json --raw"#, + ]; + + let actual = nu!(&inp.join("; ")); + let actual_repl = nu!(nu_repl_code(inp)); + + assert_eq!(actual.out, r#"["20.0 MB","20.0 MiB"]"#); + assert_eq!(actual_repl.out, r#"["20.0 MB","20.0 MiB"]"#); +} + +#[test] +fn overlay_hide_config_affected() { + let inp = &[ + "mut out = []", + "$env.config.filesize.unit = 'metric'", + "$out ++= [(20MB | into string)]", + "module spam { export-env { $env.config.filesize.unit = 'binary' } }", + "overlay use spam", + "$out ++= [(20MiB | into string)]", + "overlay hide", + "$out ++= [(20MB | into string)]", + r#"$out | to json --raw"#, + ]; + + // Can't hide overlay within the same source file + // let actual = nu!(&inp.join("; ")); + let actual_repl = nu!(nu_repl_code(inp)); + + // assert_eq!(actual.out, r#"["20.0 MB","20.0 MiB","20.0 MB"]"#); + assert_eq!(actual_repl.out, r#"["20.0 MB","20.0 MiB","20.0 MB"]"#); +} + +#[test] +fn overlay_use_after_hide_config_affected() { + let inp = &[ + "mut out = []", + "$env.config.filesize.unit = 'metric'", + "$out ++= [(20MB | into string)]", + "module spam { export-env { $env.config.filesize.unit = 'binary' } }", + "overlay use spam", + "$out ++= [(20MiB | into string)]", + "overlay hide", + "$out ++= [(20MB | into string)]", + "overlay use spam", + "$out ++= [(20MiB | into string)]", + r#"$out | to json --raw"#, + ]; + + // Can't hide overlay within the same source file + // let actual = nu!(&inp.join("; ")); + let actual_repl = nu!(nu_repl_code(inp)); + + // assert_eq!(actual.out, r#"["20.0 MB","20.0 MiB","20.0 MB"]"#); + assert_eq!( + actual_repl.out, + r#"["20.0 MB","20.0 MiB","20.0 MB","20.0 MiB"]"# + ); +} + #[test] fn overlay_use_export_env_hide() { let inp = &[