Skip to content

Commit 08cab96

Browse files
committed
patch 8.0.0405: v:progpath may become invalid after :cd
Problem: v:progpath may become invalid after ":cd". Solution: Turn v:progpath into a full path if needed.
1 parent 391b1dd commit 08cab96

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed

runtime/doc/eval.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,8 +1789,11 @@ v:progpath Contains the command with which Vim was invoked, including the
17891789
|--remote-expr|.
17901790
To get the full path use: >
17911791
echo exepath(v:progpath)
1792-
< NOTE: This does not work when the command is a relative path
1793-
and the current directory has changed.
1792+
< If the path is relative it will be expanded to the full path,
1793+
so that it still works after `:cd`. Thus starting "./vim"
1794+
results in "/home/user/path/to/vim/src/vim".
1795+
On MS-Windows the executable may be called "vim.exe", but the
1796+
".exe" is not added to v:progpath.
17941797
Read-only.
17951798

17961799
*v:register* *register-variable*

src/main.c

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ static void main_start_gui(void);
5757
# if defined(HAS_SWAP_EXISTS_ACTION)
5858
static void check_swap_exists_action(void);
5959
# endif
60+
# ifdef FEAT_EVAL
61+
static void set_progpath(char_u *argv0);
62+
# endif
6063
# if defined(FEAT_CLIENTSERVER) || defined(PROTO)
6164
static void exec_on_server(mparm_T *parmp);
6265
static void prepare_server(mparm_T *parmp);
@@ -1694,7 +1697,7 @@ parse_command_name(mparm_T *parmp)
16941697

16951698
#ifdef FEAT_EVAL
16961699
set_vim_var_string(VV_PROGNAME, initstr, -1);
1697-
set_vim_var_string(VV_PROGPATH, (char_u *)parmp->argv[0], -1);
1700+
set_progpath((char_u *)parmp->argv[0]);
16981701
#endif
16991702

17001703
if (TOLOWER_ASC(initstr[0]) == 'r')
@@ -3417,7 +3420,7 @@ check_swap_exists_action(void)
34173420
}
34183421
#endif
34193422

3420-
#endif
3423+
#endif /* NO_VIM_MAIN */
34213424

34223425
#if defined(STARTUPTIME) || defined(PROTO)
34233426
static void time_diff(struct timeval *then, struct timeval *now);
@@ -3525,6 +3528,30 @@ time_msg(
35253528

35263529
#endif
35273530

3531+
#ifndef NO_VIM_MAIN
3532+
static void
3533+
set_progpath(char_u *argv0)
3534+
{
3535+
char_u *val = argv0;
3536+
char_u buf[MAXPATHL];
3537+
3538+
/* A relative path containing a "/" will become invalid when using ":cd",
3539+
* turn it into a full path.
3540+
* On MS-Windows "vim.exe" is found in the current directory, thus also do
3541+
* it when there is no path and the file exists. */
3542+
if ( !mch_isFullName(argv0)
3543+
# ifdef WIN32
3544+
&& mch_can_exe(argv0, NULL, TRUE)
3545+
# else
3546+
&& gettail(argv0) != argv0
3547+
# endif
3548+
&& vim_FullName(argv0, buf, MAXPATHL, TRUE) != FAIL)
3549+
val = buf;
3550+
set_vim_var_string(VV_PROGPATH, val, -1);
3551+
}
3552+
3553+
#endif /* NO_VIM_MAIN */
3554+
35283555
#if (defined(FEAT_CLIENTSERVER) && !defined(NO_VIM_MAIN)) || defined(PROTO)
35293556

35303557
/*

src/testdir/test_startup.vim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,17 @@ func Test_read_stdin()
183183
endif
184184
call delete('Xtestout')
185185
endfunc
186+
187+
func Test_progpath()
188+
" Tests normally run with "./vim" or "../vim", these must have been expanded
189+
" to a full path.
190+
if has('unix')
191+
call assert_equal('/', v:progpath[0])
192+
elseif has('win32')
193+
call assert_equal(':', v:progpath[1])
194+
call assert_match('[/\\]', v:progpath[2])
195+
endif
196+
197+
" Only expect "vim" to appear in v:progname.
198+
call assert_match('vim\c', v:progname)
199+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
405,
767769
/**/
768770
404,
769771
/**/

0 commit comments

Comments
 (0)