fix(loader): cache path ambiguity #24491

Problem: cache paths are derived by replacing each reserved/filesystem-
path-sensitive char with a `%` char in the original path. With this
method, two different files at two different paths (each containing `%`
chars) can erroneously resolve to the very same cache path in certain
edge-cases.

Solution: derive cache paths by url-encoding the original (path) instead
using `vim.uri_encode()` with `"rfc2396"`. Increment `Loader.VERSION` to
denote this change.
This commit is contained in:
Tyler Miller
2023-08-01 08:28:28 -07:00
committed by GitHub
parent dfe19d6e00
commit 0804034c07
4 changed files with 99 additions and 56 deletions

View File

@@ -1,4 +1,5 @@
local uv = vim.uv
local uri_encode = vim.uri_encode
--- @type (fun(modename: string): fun()|string)[]
local loaders = package.loaders
@@ -33,7 +34,7 @@ M.enabled = false
---@field _rtp_key string
---@field _hashes? table<string, CacheHash>
local Loader = {
VERSION = 3,
VERSION = 4,
---@type table<string, table<string,ModuleInfo>>
_indexed = {},
---@type table<string, string[]>
@@ -99,7 +100,7 @@ end
---@return string file_name
---@private
function Loader.cache_file(name)
local ret = M.path .. '/' .. name:gsub('[/\\:]', '%%')
local ret = ('%s/%s'):format(M.path, uri_encode(name, 'rfc2396'))
return ret:sub(-4) == '.lua' and (ret .. 'c') or (ret .. '.luac')
end