]> granicus.if.org Git - vim/commitdiff
patch 8.2.4754: using cached values after unsetting some environment variables v8.2.4754
authorLemonBoy <thatlemon@gmail.com>
Fri, 15 Apr 2022 19:50:46 +0000 (20:50 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 15 Apr 2022 19:50:46 +0000 (20:50 +0100)
Problem:    Still using cached values after unsetting some known environment
            variables.
Solution:   Take care of the side effects. (closes #10194)

src/evalfunc.c
src/evalvars.c
src/misc1.c
src/optionstr.c
src/proto/misc1.pro
src/testdir/test_environ.vim
src/version.c
src/vim9execute.c

index da8f8457c59c577a10e3178efc502f311de0c113..690b921082a27f8d15dc82e04953811dae69562e 100644 (file)
@@ -9223,9 +9223,9 @@ f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
     name = tv_get_string_buf(&argvars[0], namebuf);
     if (argvars[1].v_type == VAR_SPECIAL
                                      && argvars[1].vval.v_number == VVAL_NULL)
-       vim_unsetenv(name);
+       vim_unsetenv_ext(name);
     else
-       vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
+       vim_setenv_ext(name, tv_get_string_buf(&argvars[1], valbuf));
 }
 
 /*
index 4690d1ac475258af8566456b77808974657938d2..8f8df0cf9d09621ba6d17de64c6b988c9b820278 100644 (file)
@@ -1795,7 +1795,7 @@ do_unlet_var(
 
        // Environment variable, normal name or expanded name.
        if (*lp->ll_name == '$')
-           vim_unsetenv(lp->ll_name + 1);
+           vim_unsetenv_ext(lp->ll_name + 1);
        else if (do_unlet(lp->ll_name, forceit) == FAIL)
            ret = FAIL;
        *name_end = cc;
index af83c108b75a2f4bf2c882789bc5ef1aaf7b40c5..9aa8ecfba807da1a277ec25439d5636a6d458669 100644 (file)
@@ -1910,6 +1910,20 @@ vim_unsetenv(char_u *var)
 #endif
 }
 
+/*
+ * Removes environment variable "name" and take care of side effects.
+ */
+    void
+vim_unsetenv_ext(char_u *var)
+{
+    vim_unsetenv(var);
+
+    // "homedir" is not cleared, keep using the old value until $HOME is set.
+    if (STRICMP(var, "VIM") == 0)
+       didset_vim = FALSE;
+    else if (STRICMP(var, "VIMRUNTIME") == 0)
+       didset_vimruntime = FALSE;
+}
 
 /*
  * Set environment variable "name" and take care of side effects.
@@ -1922,8 +1936,7 @@ vim_setenv_ext(char_u *name, char_u *val)
        init_homedir();
     else if (didset_vim && STRICMP(name, "VIM") == 0)
        didset_vim = FALSE;
-    else if (didset_vimruntime
-           && STRICMP(name, "VIMRUNTIME") == 0)
+    else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
        didset_vimruntime = FALSE;
 }
 #endif
index 698a3f3f9b290aee3b46ebc60ebb5ccbe230c631..26dd55120b45e25b2a0b718b9287b8c66150609e 100644 (file)
@@ -644,7 +644,7 @@ check_stl_option(char_u *s)
 
 /*
  * Handle string options that need some action to perform when changed.
- * Returns NULL for success, or an error message for an error.
+ * Returns NULL for success, or an unstranslated error message for an error.
  */
     char *
 did_set_string_option(
@@ -787,15 +787,9 @@ did_set_string_option(
     {
        // May compute new values for $VIM and $VIMRUNTIME
        if (didset_vim)
-       {
-           vim_setenv((char_u *)"VIM", (char_u *)"");
-           didset_vim = FALSE;
-       }
+           vim_unsetenv_ext((char_u *)"VIM");
        if (didset_vimruntime)
-       {
-           vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
-           didset_vimruntime = FALSE;
-       }
+           vim_unsetenv_ext((char_u *)"VIMRUNTIME");
     }
 
 #ifdef FEAT_SYN_HL
index cc7e07a20c42585b4b33be44caf8812a099c370d..167805bd52947a80664a1c85507eaee0738c5bb7 100644 (file)
@@ -32,6 +32,7 @@ void expand_env(char_u *src, char_u *dst, int dstlen);
 void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, int esc, int one, char_u *startstr);
 char_u *vim_getenv(char_u *name, int *mustfree);
 void vim_unsetenv(char_u *var);
+void vim_unsetenv_ext(char_u *var);
 void vim_setenv_ext(char_u *name, char_u *val);
 void vim_setenv(char_u *name, char_u *val);
 char_u *get_env_name(expand_T *xp, int idx);
index dd34983ee5a8a7fa37e4aa4b1eb9ee41dd215f37..d8344817f5accf9e3a35c17e9ac316db1eeb29bd 100644 (file)
@@ -28,6 +28,26 @@ func Test_setenv()
   call assert_equal(v:null, getenv('TEST ENV'))
 endfunc
 
+func Test_special_env()
+  " The value for $HOME is cached internally by Vim, ensure the value is up to
+  " date.
+  let orig_ENV = $HOME
+
+  let $HOME = 'foo'
+  call assert_equal('foo', expand('~'))
+  " old $HOME value is kept until a new one is set
+  unlet $HOME
+  call assert_equal('foo', expand('~'))
+
+  call setenv('HOME', 'bar')
+  call assert_equal('bar', expand('~'))
+  " old $HOME value is kept until a new one is set
+  call setenv('HOME', v:null)
+  call assert_equal('bar', expand('~'))
+
+  let $HOME = orig_ENV
+endfunc
+
 func Test_external_env()
   call setenv('FOO', 'HelloWorld')
   if has('win32')
index 97683b459acb2cb835b1d7b829ec6038a6586519..e833b78be716876af1f50e1354ebc5707dfa8f25 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4754,
 /**/
     4753,
 /**/
index da2822c1b5de47a7cdd2da667992729ca71e9a31..afa0dcf198263f7090a1e28630e4d22f9c4dd7aa 100644 (file)
@@ -2656,6 +2656,7 @@ exec_instructions(ectx_T *ectx)
            case ISN_SOURCE:
                {
                    int notused;
+
                    SOURCING_LNUM = iptr->isn_lnum;
                    if (may_load_script((int)iptr->isn_arg.number, &notused)
                                                                       == FAIL)
@@ -3490,7 +3491,7 @@ exec_instructions(ectx_T *ectx)
                    goto on_error;
                break;
            case ISN_UNLETENV:
-               vim_unsetenv(iptr->isn_arg.unlet.ul_name);
+               vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
                break;
 
            case ISN_LOCKUNLOCK: