Skip to content

Commit

Permalink
fix: avoid calling vim.fn in fast event (#1878)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamboman authored Feb 15, 2025
1 parent f9f3b46 commit 3a444cb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lua/mason-core/package/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ local a = require "mason-core.async"
local fs = require "mason-core.fs"
local log = require "mason-core.log"
local path = require "mason-core.path"
local platform = require "mason-core.platform"
local registry = require "mason-registry"

local is_not_nil = _.complement(_.is_nil)
Expand Down Expand Up @@ -87,7 +88,7 @@ local PackageMt = { __index = Package }

---@param spec PackageSpec | RegistryPackageSpec
local function validate_spec(spec)
if vim.fn.has "nvim-0.11" ~= 1 then
if platform.cached_features["nvim-0.11"] ~= 1 then
return
end
if is_registry_spec(spec) then
Expand Down
5 changes: 3 additions & 2 deletions lua/mason-core/platform.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ end)

-- Most of the code that calls into these functions executes outside of the main event loop, where API/fn functions are
-- disabled. We evaluate these immediately here to avoid issues with main loop synchronization.
local cached_features = {
M.cached_features = {
["win"] = vim.fn.has "win32",
["win32"] = vim.fn.has "win32",
["win64"] = vim.fn.has "win64",
["mac"] = vim.fn.has "mac",
["darwin"] = vim.fn.has "mac",
["unix"] = vim.fn.has "unix",
["linux"] = vim.fn.has "linux",
["nvim-0.11"] = vim.fn.has "nvim-0.11",
}

---@type fun(env: string): boolean
Expand Down Expand Up @@ -104,7 +105,7 @@ local check_env = _.memoize(_.cond {
M.is = setmetatable({}, {
__index = function(__, key)
local os, arch, env = unpack(vim.split(key, "_", { plain = true }))
if not cached_features[os] or cached_features[os] ~= 1 then
if not M.cached_features[os] or M.cached_features[os] ~= 1 then
return false
end
if arch and arch ~= M.arch then
Expand Down
24 changes: 24 additions & 0 deletions tests/mason-core/package/package_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,28 @@ describe("package", function()
end)
end)
)

it(
"should be able to instantiate package outside of main loop",
async_test(function()
local dummy = registry.get_package "registry"

-- Move outside the main loop
a.wait(function(resolve)
local timer = vim.loop.new_timer()
timer:start(0, 0, function()
timer:close()
resolve()
end)
end)

assert.is_true(vim.in_fast_event())

local pkg = assert.is_not.has_error(function()
return Pkg.new(dummy.spec)
end)

assert.same(dummy.spec, pkg.spec)
end)
)
end)

0 comments on commit 3a444cb

Please sign in to comment.