patch 9.1.0359: MS-Windows: relative import in a script sourced from a buffer 
doesn't work

Commit: 
https://github.com/vim/vim/commit/f135fa28e481b2eba73baf52b08d24add5c4fe8b
Author: Yegappan Lakshmanan <[email protected]>
Date:   Sat Apr 20 18:31:21 2024 +0200

    patch 9.1.0359: MS-Windows: relative import in a script sourced from a 
buffer doesn't work
    
    Problem:  MS-Windows: Relative import in a script sourced from a buffer
              doesn't work (Ernie Rael)
    Solution: Set a filename, so that we are not trying to use
              script-relative filename (Yegappan Lakshmanan)
    
    When a script is sourced from a buffer, the file name is set to ":source
    buffer=". In MS-Windows, the ":" is a path separator character (used
    after a drive letter). This results in the code trying to use the ":"
    prefix to import the script on MS-Windows. To fix this, when importing a
    script from a script sourced from a buffer with nofile, don't use
    a script relative path name.
    
    fixes #14588
    closes: #14603
    
    Signed-off-by: Yegappan Lakshmanan <[email protected]>
    Signed-off-by: Christian Brabandt <[email protected]>

diff --git a/src/scriptfile.c b/src/scriptfile.c
index cc7626041..54af7597e 100644
--- a/src/scriptfile.c
+++ b/src/scriptfile.c
@@ -1430,8 +1430,13 @@ do_source_buffer_init(source_cookie_T *sp, exarg_T *eap)
        return NULL;
 
     // Use ":source buffer=<num>" as the script name
-    vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", curbuf->b_fnum);
-    fname = vim_strsave(IObuff);
+    if (curbuf->b_ffname != NULL)
+       fname = vim_strsave(curbuf->b_ffname);
+    else
+    {
+       vim_snprintf((char *)IObuff, IOSIZE, ":source buffer=%d", 
curbuf->b_fnum);
+       fname = vim_strsave(IObuff);
+    }
     if (fname == NULL)
        return NULL;
 
diff --git a/src/testdir/test_vim9_import.vim b/src/testdir/test_vim9_import.vim
index b7902f10c..78383ee79 100644
--- a/src/testdir/test_vim9_import.vim
+++ b/src/testdir/test_vim9_import.vim
@@ -1140,13 +1140,9 @@ def Test_autoload_import_relative()
   v9.CheckScriptFailure(lines, 'E484:')
 enddef
 
+" autoload relative, access from compiled function.
+" Github issues: #14565, #14579
 def Test_autoload_import_relative_compiled_buffer()
-  if !has('unix')
-    # temporary, until it's discovered why the test fails on Windows.
-    CheckUnix
-    return
-  endif
-  # autoload relative, access from compiled function. #14565, #14579
   var lines =<< trim END
     vim9script
 
@@ -1168,6 +1164,40 @@ def Test_autoload_import_relative_compiled_buffer()
   new
   setline(1, lines)
   :source
+  # source one more time to detect issues with clearing the script state and
+  # variables
+  :source
+  :bw!
+enddef
+
+" Test for relative import when sourcing a buffer in another directory
+def Test_autoload_import_relative_from_buffer_in_dir()
+  mkdir('Ximportrelative/dir1/dir2', 'pR')
+  var lines =<< trim END
+    vim9script
+
+    export def F1(): string
+        return 'InFile.vim'
+    enddef
+  END
+  writefile(lines, 'Ximportrelative/dir1/dir2/Ximport.vim')
+  lines =<< trim END
+    vim9script
+
+    import autoload './Ximport.vim' as xfile
+
+    def F(): string
+      return xfile.F1()
+    enddef
+    assert_equal('InFile.vim', F())
+  END
+  writefile(lines, 'Ximportrelative/dir1/dir2/Xrelative.vim')
+
+  split Ximportrelative/dir1/dir2/Xrelative.vim
+  :source
+  # source one more time to detect issues with clearing the script state and
+  # variables
+  :source
   :bw!
 enddef
 
diff --git a/src/version.c b/src/version.c
index ee3406315..9e2acb87e 100644
--- a/src/version.c
+++ b/src/version.c
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    359,
 /**/
     358,
 /**/
diff --git a/src/vim9script.c b/src/vim9script.c
index a64ce7212..303588964 100644
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -456,15 +456,24 @@ handle_import(
        scriptitem_T    *si = SCRIPT_ITEM(current_sctx.sc_sid);
        char_u          *tail = gettail(si->sn_name);
        char_u          *from_name;
+       int             sourced_from_nofile_buf = FALSE;
 
-       // Relative to current script: "./name.vim", "../../name.vim".
-       len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
-       from_name = alloc((int)len);
-       if (from_name == NULL)
-           goto erret;
-       vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
-       add_pathsep(from_name);
-       STRCAT(from_name, tv.vval.v_string);
+       if (STRNCMP(si->sn_name, ":source buffer=", 15) == 0)
+           sourced_from_nofile_buf = TRUE;
+
+       if (!sourced_from_nofile_buf)
+       {
+           // Relative to current script: "./name.vim", "../../name.vim".
+           len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) 
+ 2;
+           from_name = alloc((int)len);
+           if (from_name == NULL)
+               goto erret;
+           vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
+           add_pathsep(from_name);
+           STRCAT(from_name, tv.vval.v_string);
+       }
+       else
+           from_name = vim_strsave(tv.vval.v_string);
        simplify_filename(from_name);
 
        res = handle_import_fname(from_name, is_autoload, &sid);

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/E1ryDpm-004Zx9-Da%40256bit.org.

Raspunde prin e-mail lui