From d577074da97a836af9f09d032180ab28d5d8fb2a Mon Sep 17 00:00:00 2001 From: Bahex Date: Wed, 12 Feb 2025 03:51:43 +0300 Subject: [PATCH] feat(std/dirs): retain state in subshells or with exec-restarts (#15080) Persist `std/dirs`'s state in subshells and also when restarting the shell with `exec $nu.current-exe` --- crates/nu-std/std/dirs/mod.nu | 14 +++++++++++--- crates/nu-std/tests/test_dirs.nu | 8 ++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/crates/nu-std/std/dirs/mod.nu b/crates/nu-std/std/dirs/mod.nu index ab3403f36c..8585838c99 100644 --- a/crates/nu-std/std/dirs/mod.nu +++ b/crates/nu-std/std/dirs/mod.nu @@ -17,8 +17,16 @@ # This situation could arise if we started with [/a, /b, /c], then # we changed directories from /b to /var/tmp. export-env { - $env.DIRS_POSITION = 0 - $env.DIRS_LIST = [($env.PWD | path expand)] + $env.ENV_CONVERSIONS.DIRS_LIST = { + from_string: {|| split row (char esep)} + to_string: {|| str join (char esep)} + } + $env.ENV_CONVERSIONS.DIRS_POSITION = { + from_string: {|| into int} + to_string: {|| into string} + } + $env.DIRS_POSITION = $env.DIRS_POSITION? | default (0) + $env.DIRS_LIST = $env.DIRS_LIST? | default [($env.PWD | path expand)] } # Add one or more directories to the list. @@ -145,4 +153,4 @@ export module shells-aliases { export alias p = prev export alias n = next export alias g = goto -} \ No newline at end of file +} diff --git a/crates/nu-std/tests/test_dirs.nu b/crates/nu-std/tests/test_dirs.nu index 4c5b891829..eaf56ec777 100644 --- a/crates/nu-std/tests/test_dirs.nu +++ b/crates/nu-std/tests/test_dirs.nu @@ -46,6 +46,8 @@ def dirs_command [] { # must set PWD *before* doing `use` that will run the def --env block in dirs module. cd $c.base_path + # hide existing variables to prevent the state from outside affecting the tests + hide-env -i DIRS_LIST DIRS_POSITION # must execute these uses for the UOT commands *after* the test and *not* just put them at top of test module. # the def --env gets messed up use std/dirs @@ -96,6 +98,7 @@ def dirs_next [] { cd $c.base_path assert equal $env.PWD $c.base_path "test setup" + hide-env -i DIRS_LIST DIRS_POSITION use std/dirs cur_dir_check $c.base_path "use module test setup" @@ -117,6 +120,7 @@ def dirs_cd [] { # must set PWD *before* doing `use` that will run the def --env block in dirs module. cd $c.base_path + hide-env -i DIRS_LIST DIRS_POSITION use std/dirs cur_dir_check $c.base_path "use module test setup" @@ -139,6 +143,8 @@ def dirs_cd [] { def dirs_goto_bug10696 [] { let $c = $in cd $c.base_path + + hide-env -i DIRS_LIST DIRS_POSITION use std/dirs dirs add $c.path_a @@ -153,6 +159,8 @@ def dirs_goto_bug10696 [] { def dirs_goto [] { let $c = $in cd $c.base_path + + hide-env -i DIRS_LIST DIRS_POSITION use std/dirs # check that goto can move *from* any position in the ring *to* any other position (correctly)