fix(gf): handle local file: URI paths #38915

Problem:
`gf` and `<cfile>` treat `file:/absolute/path` as a literal path and
open `file:/...` instead of the local file.

Solution:
Strip the local `file:` prefix before path resolution in the hyperlink
path code.

(cherry picked from commit e827c3b648)
This commit is contained in:
Barrett Ruth
2026-04-14 18:08:09 -04:00
committed by github-actions[bot]
parent 465aa2af1d
commit d86d9759e5
2 changed files with 18 additions and 0 deletions

View File

@@ -1759,6 +1759,13 @@ char *find_file_name_in_path(char *ptr, size_t len, int options, long count, cha
return NULL;
}
if ((options & FNAME_HYP) && len > 6 && strncmp(ptr, "file:/",
6) == 0 && !vim_ispathsep(ptr[6])) {
size_t off = path_has_drive_letter(ptr + 6, len - 6) ? 6 : 5;
ptr += off;
len -= off;
}
if ((options & FNAME_INCL) && *curbuf->b_p_inex != NUL) {
tofree = eval_includeexpr(ptr, len);
if (tofree != NULL) {

View File

@@ -160,6 +160,17 @@ describe('file search', function()
)
end)
it('gf/<cfile> handles local file: paths', function()
local path = fn.fnamemodify(join_path(testdir, 'gf-target.txt'), ':p'):gsub('\\', '/')
local uri = 'file:' .. (is_os('win') and '/' or '') .. path
write_file(path, '')
insert(uri)
command('norm! 0')
eq(path, eval('expand("<cfile>")'))
feed('gf')
eq(path, fn.fnamemodify(eval('expand("%:p")'), ':p'):gsub('\\', '/'))
end)
---@param funcname 'finddir' | 'findfile'
local function test_find_func(funcname, folder, item)
local d = join_path(testdir, folder)