Skip to content

Commit 9a90179

Browse files
erraelchrisbra
authored andcommitted
patch 9.1.0338: Vim9: import through symlinks not correctly handled
Problem: Vim9: import through symlinks not correctly handled Solution: Check for script being a symlink but only once (Ernie Rael) closes: #14565 Signed-off-by: Ernie Rael <[email protected]> Signed-off-by: Yegappan Lakshmanan <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent d1068a2 commit 9a90179

File tree

6 files changed

+71
-0
lines changed

6 files changed

+71
-0
lines changed

src/proto/scriptfile.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ void ex_runtime(exarg_T *eap);
99
void set_context_in_runtime_cmd(expand_T *xp, char_u *arg);
1010
int find_script_by_name(char_u *name);
1111
int get_new_scriptitem_for_fname(int *error, char_u *fname);
12+
void check_script_symlink(int sid);
1213
int do_in_path(char_u *path, char *prefix, char_u *name, int flags, void (*callback)(char_u *fname, void *ck), void *cookie);
1314
int do_in_runtimepath(char_u *name, int flags, void (*callback)(char_u *fname, void *ck), void *cookie);
1415
int source_runtime(char_u *name, int flags);

src/scriptfile.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,43 @@ get_new_scriptitem_for_fname(int *error, char_u *fname)
405405
return sid;
406406
}
407407

408+
/*
409+
* If the script for "sid" is a symlink and "sn_source_sid" is not set
410+
* then initialize it. A new script_item is created if needed.
411+
*/
412+
void
413+
check_script_symlink(int sid)
414+
{
415+
scriptitem_T *si = SCRIPT_ITEM(sid);
416+
if (si->sn_syml_checked || si->sn_sourced_sid > 0)
417+
return;
418+
si->sn_syml_checked = TRUE;
419+
420+
// If fname is a symbolic link, create an script_item for the real file.
421+
422+
char_u *real_fname = fix_fname(si->sn_name);
423+
if (real_fname != NULL && STRCMP(real_fname, si->sn_name) != 0)
424+
{
425+
int real_sid = find_script_by_name(real_fname);
426+
int error2 = OK;
427+
int new_sid = FALSE;
428+
if (real_sid < 0)
429+
{
430+
real_sid = get_new_scriptitem_for_fname(&error2, real_fname);
431+
new_sid = TRUE;
432+
}
433+
if (error2 == OK)
434+
{
435+
si = SCRIPT_ITEM(sid);
436+
si->sn_sourced_sid = real_sid;
437+
if (new_sid)
438+
SCRIPT_ITEM(real_sid)->sn_import_autoload
439+
= si->sn_import_autoload;
440+
}
441+
}
442+
vim_free(real_fname);
443+
}
444+
408445
static void
409446
find_script_callback(char_u *fname, void *cookie)
410447
{

src/structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,6 +2131,7 @@ typedef struct
21312131
int sn_state; // SN_STATE_ values
21322132
char_u *sn_save_cpo; // 'cpo' value when :vim9script found
21332133
char sn_is_vimrc; // .vimrc file, do not restore 'cpo'
2134+
char sn_syml_checked;// flag: this has been checked for sym link
21342135

21352136
// for a Vim9 script under "rtp/autoload/" this is "dir#scriptname#"
21362137
char_u *sn_autoload_prefix;

src/testdir/test_vim9_import.vim

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2930,6 +2930,33 @@ def Test_vim9_import_symlink()
29302930
unlet g:resultValue
29312931
&rtp = save_rtp
29322932
delete('Xfrom', 'rf')
2933+
2934+
# Access item from :def imported through symbolic linked directory. #14536
2935+
mkdir('Xto/real_dir', 'pR')
2936+
lines =<< trim END
2937+
vim9script
2938+
export const val = 17
2939+
export def F(): number
2940+
return 23
2941+
enddef
2942+
END
2943+
writefile(lines, 'Xto/real_dir/real_file.vim')
2944+
system('ln -s real_dir Xto/syml_dir')
2945+
defer delete('Xto/syml_dir')
2946+
lines =<< trim END
2947+
vim9script
2948+
import autoload './Xto/syml_dir/real_file.vim'
2949+
2950+
def Fmain()
2951+
assert_equal(17, real_file.val)
2952+
enddef
2953+
def F2()
2954+
assert_equal(23, real_file.F())
2955+
enddef
2956+
Fmain()
2957+
F2()
2958+
END
2959+
v9.CheckScriptSuccess(lines)
29332960
endif
29342961
enddef
29352962

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
338,
707709
/**/
708710
337,
709711
/**/

src/vim9expr.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,9 @@ compile_load_scriptvar(
546546
int done = FALSE;
547547
int res = OK;
548548

549+
check_script_symlink(import->imp_sid);
550+
import_check_sourced_sid(&import->imp_sid);
551+
549552
// Need to lookup the member.
550553
if (*p != '.')
551554
{

0 commit comments

Comments
 (0)