fix(startup): init.lua: set $MYVIMRC to absolute path #15748

- main.c: remove os_setenv("MYVIMRC",…), it is already done by
  do_source().
  - This also sets $MYVIMRC to a full (absolute) path.
- code cleanup.
This commit is contained in:
Justin M. Keyes 2021-09-21 08:47:46 -07:00 committed by GitHub
parent c208993026
commit c76cddf3e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 13 deletions

View File

@ -2112,7 +2112,7 @@ int do_source(char_u *fname, int check_other, int is_vimrc)
verbose_leave(); verbose_leave();
} }
if (is_vimrc == DOSO_VIMRC) { if (is_vimrc == DOSO_VIMRC) {
vimrc_found(fname_exp, (char_u *)"MYVIMRC"); vimrc_found((char *)fname_exp, "MYVIMRC");
} }
#ifdef USE_CRNL #ifdef USE_CRNL

View File

@ -1781,23 +1781,23 @@ static bool do_user_initialization(void)
} }
char_u *init_lua_path = (char_u *)stdpaths_user_conf_subpath("init.lua"); char_u *init_lua_path = (char_u *)stdpaths_user_conf_subpath("init.lua");
char_u *user_vimrc = (char_u *)stdpaths_user_conf_subpath("init.vim");
// init.lua
if (os_path_exists(init_lua_path) if (os_path_exists(init_lua_path)
&& do_source(init_lua_path, true, DOSO_VIMRC)) { && do_source(init_lua_path, true, DOSO_VIMRC)) {
os_setenv("MYVIMRC", (const char *)init_lua_path, 1); if (os_path_exists(user_vimrc)) {
char_u *vimrc_path = (char_u *)stdpaths_user_conf_subpath("init.vim");
if (os_path_exists(vimrc_path)) {
EMSG3(_("E5422: Conflicting configs: \"%s\" \"%s\""), init_lua_path, EMSG3(_("E5422: Conflicting configs: \"%s\" \"%s\""), init_lua_path,
vimrc_path); user_vimrc);
} }
xfree(vimrc_path); xfree(user_vimrc);
xfree(init_lua_path); xfree(init_lua_path);
return false; return false;
} }
xfree(init_lua_path); xfree(init_lua_path);
char_u *user_vimrc = (char_u *)stdpaths_user_conf_subpath("init.vim"); // init.vim
if (do_source(user_vimrc, true, DOSO_VIMRC) != FAIL) { if (do_source(user_vimrc, true, DOSO_VIMRC) != FAIL) {
do_exrc = p_exrc; do_exrc = p_exrc;
if (do_exrc) { if (do_exrc) {
@ -1809,6 +1809,7 @@ static bool do_user_initialization(void)
return do_exrc; return do_exrc;
} }
xfree(user_vimrc); xfree(user_vimrc);
char *const config_dirs = stdpaths_get_xdg_var(kXDGConfigDirs); char *const config_dirs = stdpaths_get_xdg_var(kXDGConfigDirs);
if (config_dirs != NULL) { if (config_dirs != NULL) {
const void *iter = NULL; const void *iter = NULL;
@ -1839,6 +1840,7 @@ static bool do_user_initialization(void)
} while (iter != NULL); } while (iter != NULL);
xfree(config_dirs); xfree(config_dirs);
} }
if (execute_env("EXINIT") == OK) { if (execute_env("EXINIT") == OK) {
do_exrc = p_exrc; do_exrc = p_exrc;
return do_exrc; return do_exrc;

View File

@ -6888,15 +6888,15 @@ static void paste_option_changed(void)
/// ///
/// Set the values for options that didn't get set yet to the defaults. /// Set the values for options that didn't get set yet to the defaults.
/// When "fname" is not NULL, use it to set $"envname" when it wasn't set yet. /// When "fname" is not NULL, use it to set $"envname" when it wasn't set yet.
void vimrc_found(char_u *fname, char_u *envname) void vimrc_found(char *fname, char *envname)
{ {
if (fname != NULL && envname != NULL) { if (fname != NULL && envname != NULL) {
char *p = vim_getenv((char *)envname); char *p = vim_getenv(envname);
if (p == NULL) { if (p == NULL) {
// Set $MYVIMRC to the first vimrc file found. // Set $MYVIMRC to the first vimrc file found.
p = FullName_save((char *)fname, false); p = FullName_save(fname, false);
if (p != NULL) { if (p != NULL) {
os_setenv((char *)envname, p, 1); os_setenv(envname, p, 1);
xfree(p); xfree(p);
} }
} else { } else {

View File

@ -472,7 +472,7 @@ describe('user config init', function()
clear{ args_rm={'-u' }, env=xenv } clear{ args_rm={'-u' }, env=xenv }
eq(1, eval('g:lua_rc')) eq(1, eval('g:lua_rc'))
eq(init_lua_path, eval('$MYVIMRC')) eq(funcs.fnamemodify(init_lua_path, ':p'), eval('$MYVIMRC'))
end) end)
describe 'with explicitly provided config'(function() describe 'with explicitly provided config'(function()