0
0
mirror of https://github.com/vim/vim.git synced 2025-09-24 03:44:06 -04:00

patch 8.2.4754: using cached values after unsetting some environment variables

Problem:    Still using cached values after unsetting some known environment
            variables.
Solution:   Take care of the side effects. (closes #10194)
This commit is contained in:
LemonBoy
2022-04-15 20:50:46 +01:00
committed by Bram Moolenaar
parent 31e5c60a68
commit 7714231bb5
8 changed files with 46 additions and 15 deletions

View File

@@ -9223,9 +9223,9 @@ f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
name = tv_get_string_buf(&argvars[0], namebuf); name = tv_get_string_buf(&argvars[0], namebuf);
if (argvars[1].v_type == VAR_SPECIAL if (argvars[1].v_type == VAR_SPECIAL
&& argvars[1].vval.v_number == VVAL_NULL) && argvars[1].vval.v_number == VVAL_NULL)
vim_unsetenv(name); vim_unsetenv_ext(name);
else else
vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf)); vim_setenv_ext(name, tv_get_string_buf(&argvars[1], valbuf));
} }
/* /*

View File

@@ -1795,7 +1795,7 @@ do_unlet_var(
// Environment variable, normal name or expanded name. // Environment variable, normal name or expanded name.
if (*lp->ll_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) else if (do_unlet(lp->ll_name, forceit) == FAIL)
ret = FAIL; ret = FAIL;
*name_end = cc; *name_end = cc;

View File

@@ -1910,6 +1910,20 @@ vim_unsetenv(char_u *var)
#endif #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. * 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(); init_homedir();
else if (didset_vim && STRICMP(name, "VIM") == 0) else if (didset_vim && STRICMP(name, "VIM") == 0)
didset_vim = FALSE; didset_vim = FALSE;
else if (didset_vimruntime else if (didset_vimruntime && STRICMP(name, "VIMRUNTIME") == 0)
&& STRICMP(name, "VIMRUNTIME") == 0)
didset_vimruntime = FALSE; didset_vimruntime = FALSE;
} }
#endif #endif

View File

@@ -644,7 +644,7 @@ check_stl_option(char_u *s)
/* /*
* Handle string options that need some action to perform when changed. * 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 * char *
did_set_string_option( did_set_string_option(
@@ -787,15 +787,9 @@ did_set_string_option(
{ {
// May compute new values for $VIM and $VIMRUNTIME // May compute new values for $VIM and $VIMRUNTIME
if (didset_vim) if (didset_vim)
{ vim_unsetenv_ext((char_u *)"VIM");
vim_setenv((char_u *)"VIM", (char_u *)"");
didset_vim = FALSE;
}
if (didset_vimruntime) if (didset_vimruntime)
{ vim_unsetenv_ext((char_u *)"VIMRUNTIME");
vim_setenv((char_u *)"VIMRUNTIME", (char_u *)"");
didset_vimruntime = FALSE;
}
} }
#ifdef FEAT_SYN_HL #ifdef FEAT_SYN_HL

View 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); 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); char_u *vim_getenv(char_u *name, int *mustfree);
void vim_unsetenv(char_u *var); 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_ext(char_u *name, char_u *val);
void vim_setenv(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); char_u *get_env_name(expand_T *xp, int idx);

View File

@@ -28,6 +28,26 @@ func Test_setenv()
call assert_equal(v:null, getenv('TEST ENV')) call assert_equal(v:null, getenv('TEST ENV'))
endfunc 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() func Test_external_env()
call setenv('FOO', 'HelloWorld') call setenv('FOO', 'HelloWorld')
if has('win32') if has('win32')

View File

@@ -746,6 +746,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
4754,
/**/ /**/
4753, 4753,
/**/ /**/

View File

@@ -2656,6 +2656,7 @@ exec_instructions(ectx_T *ectx)
case ISN_SOURCE: case ISN_SOURCE:
{ {
int notused; int notused;
SOURCING_LNUM = iptr->isn_lnum; SOURCING_LNUM = iptr->isn_lnum;
if (may_load_script((int)iptr->isn_arg.number, &notused) if (may_load_script((int)iptr->isn_arg.number, &notused)
== FAIL) == FAIL)
@@ -3490,7 +3491,7 @@ exec_instructions(ectx_T *ectx)
goto on_error; goto on_error;
break; break;
case ISN_UNLETENV: case ISN_UNLETENV:
vim_unsetenv(iptr->isn_arg.unlet.ul_name); vim_unsetenv_ext(iptr->isn_arg.unlet.ul_name);
break; break;
case ISN_LOCKUNLOCK: case ISN_LOCKUNLOCK: