Skip to content

Commit 4b403d2

Browse files
author
Jose Alvarez
authored
fix!: update command resolver signature to fix caching (#1072)
1 parent f9b5ec8 commit 4b403d2

17 files changed

+50
-48
lines changed

lua/null-ls/builtins/code_actions/eslint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ return h.make_builtin({
166166
params.messages = output[1].messages
167167
return code_action_handler(params)
168168
end,
169-
dynamic_command = cmd_resolver.from_node_modules,
169+
dynamic_command = cmd_resolver.from_node_modules(),
170170
cwd = h.cache.by_bufnr(function(params)
171171
return u.root_pattern(
172172
-- https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-file-formats

lua/null-ls/builtins/diagnostics/eslint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ return h.make_builtin({
4242
end,
4343
use_cache = true,
4444
on_output = handle_eslint_output,
45-
dynamic_command = cmd_resolver.from_node_modules,
45+
dynamic_command = cmd_resolver.from_node_modules(),
4646
cwd = h.cache.by_bufnr(function(params)
4747
return u.root_pattern(
4848
-- https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-file-formats

lua/null-ls/builtins/diagnostics/puglint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ return h.make_builtin({
3434
groups = { "filename", "row", "col", "message" },
3535
},
3636
}),
37-
dynamic_command = cmd_resolver.from_node_modules,
37+
dynamic_command = cmd_resolver.from_node_modules(),
3838
},
3939
factory = h.generator_factory,
4040
})

lua/null-ls/builtins/diagnostics/standardjs.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ return h.make_builtin({
4141
},
4242
},
4343
}),
44-
dynamic_command = cmd_resolver.from_node_modules,
44+
dynamic_command = cmd_resolver.from_node_modules(),
4545
},
4646
factory = h.generator_factory,
4747
})

lua/null-ls/builtins/diagnostics/stylelint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ return h.make_builtin({
1818
to_stdin = true,
1919
format = "json_raw",
2020
from_stderr = true,
21-
dynamic_command = cmd_resolver.from_node_modules,
21+
dynamic_command = cmd_resolver.from_node_modules(),
2222
on_output = function(params)
2323
local output = params.output and params.output[1] and params.output[1].warnings or {}
2424

lua/null-ls/builtins/diagnostics/stylint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ return h.make_builtin({
2828
groups = { "row", "col", "severity", "message" },
2929
},
3030
}),
31-
dynamic_command = cmd_resolver.from_node_modules,
31+
dynamic_command = cmd_resolver.from_node_modules(),
3232
},
3333
factory = h.generator_factory,
3434
})

lua/null-ls/builtins/formatting/eslint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ return h.make_builtin({
4242
},
4343
}
4444
end,
45-
dynamic_command = cmd_resolver.from_node_modules,
45+
dynamic_command = cmd_resolver.from_node_modules(),
4646
check_exit_code = { 0, 1 },
4747
cwd = h.cache.by_bufnr(function(params)
4848
return u.root_pattern(

lua/null-ls/builtins/formatting/prettier.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ return h.make_builtin({
4141
"$FILENAME",
4242
}, "--range-start", "--range-end", { row_offset = -1, col_offset = -1 }),
4343
to_stdin = true,
44-
dynamic_command = cmd_resolver.from_node_modules,
44+
dynamic_command = cmd_resolver.from_node_modules(),
4545
cwd = h.cache.by_bufnr(function(params)
4646
return u.root_pattern(
4747
-- https://prettier.io/docs/en/configuration.html

lua/null-ls/builtins/formatting/prettier_d_slim.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ return h.make_builtin({
4343
{ row_offset = -1, col_offset = -1 }
4444
),
4545
to_stdin = true,
46-
dynamic_command = cmd_resolver.from_node_modules,
46+
dynamic_command = cmd_resolver.from_node_modules(),
4747
cwd = h.cache.by_bufnr(function(params)
4848
return u.root_pattern(
4949
-- https://prettier.io/docs/en/configuration.html

lua/null-ls/builtins/formatting/prettier_standard.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ return h.make_builtin({
1717
command = "prettier-standard",
1818
args = { "--stdin" },
1919
to_stdin = true,
20-
dynamic_command = cmd_resolver.from_node_modules,
20+
dynamic_command = cmd_resolver.from_node_modules(),
2121
cwd = h.cache.by_bufnr(function(params)
2222
return u.root_pattern(
2323
-- https://prettier.io/docs/en/configuration.html

lua/null-ls/builtins/formatting/prettierd.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ return h.make_builtin({
3232
generator_opts = {
3333
command = "prettierd",
3434
args = { "$FILENAME" },
35-
dynamic_command = cmd_resolver.from_node_modules,
35+
dynamic_command = cmd_resolver.from_node_modules(),
3636
to_stdin = true,
3737
cwd = h.cache.by_bufnr(function(params)
3838
return u.root_pattern(

lua/null-ls/builtins/formatting/rescript.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ return h.make_builtin({
1919
args = function(params)
2020
return { "format", "-stdin", "." .. vim.fn.fnamemodify(params.bufname, ":e") }
2121
end,
22-
dynamic_command = cmd_resolver.from_node_modules,
22+
dynamic_command = cmd_resolver.from_node_modules(),
2323
to_stdin = true,
2424
},
2525
factory = h.formatter_factory,

lua/null-ls/builtins/formatting/rustywind.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ return h.make_builtin({
2424
command = "rustywind",
2525
args = { "--stdin" },
2626
to_stdin = true,
27-
dynamic_command = cmd_resolver.from_node_modules,
27+
dynamic_command = cmd_resolver.from_node_modules(),
2828
},
2929
factory = h.formatter_factory,
3030
})

lua/null-ls/builtins/formatting/standardjs.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ return h.make_builtin({
1616
command = "standard",
1717
args = { "--stdin", "--fix" },
1818
to_stdin = true,
19-
dynamic_command = cmd_resolver.from_node_modules,
19+
dynamic_command = cmd_resolver.from_node_modules(),
2020
},
2121
factory = h.formatter_factory,
2222
})

lua/null-ls/builtins/formatting/stylelint.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ return h.make_builtin({
1717
args = { "--fix", "--stdin", "--stdin-filename", "$FILENAME" },
1818
to_stdin = true,
1919
from_stderr = true,
20-
dynamic_command = cmd_resolver.from_node_modules,
20+
dynamic_command = cmd_resolver.from_node_modules(),
2121
},
2222
factory = h.formatter_factory,
2323
})

lua/null-ls/helpers/command_resolver.lua

+34-32
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ end
2727

2828
--- creates a resolver that searches for a local executable and caches results by bufnr
2929
---@param prefix string|nil
30-
M.make_generic_resolver = function(prefix)
30+
M.generic = function(prefix)
3131
---@param params NullLsParams
3232
---@return string|nil
3333
return cache.by_bufnr(function(params)
@@ -41,42 +41,44 @@ M.make_generic_resolver = function(prefix)
4141
end)
4242
end
4343

44-
local node_modules_resolver = M.make_generic_resolver(u.path.join("node_modules", ".bin"))
45-
--- resolver that searches for a local node_modules executable and falls back to a global executable
46-
M.from_node_modules = function(params)
47-
return node_modules_resolver(params) or params.command
44+
--- creates a resolver that searches for a local node_modules executable and falls back to a global executable
45+
M.from_node_modules = function()
46+
local node_modules_resolver = M.generic(u.path.join("node_modules", ".bin"))
47+
return function(params)
48+
return node_modules_resolver(params) or params.command
49+
end
4850
end
4951

50-
--- resolver that searches for a local yarn pnp executable and falls back to a global executable
51-
---@param params NullLsParams
52-
---@return string|string[]|nil
53-
M.from_yarn_pnp = cache.by_bufnr(function(params)
54-
local ok, yarn_command = pcall(function()
55-
local root = params.root
56-
if not root then
57-
error("unable to resolve root directory")
58-
end
52+
--- creates a resolver that searches for a local yarn pnp executable and falls back to a global executable
53+
M.from_yarn_pnp = function()
54+
return cache.by_bufnr(function(params)
55+
local ok, yarn_command = pcall(function()
56+
local root = params.root
57+
if not root then
58+
error("unable to resolve root directory")
59+
end
5960

60-
-- older yarn versions use `.pnp.js`, so look for both new and old names
61-
local pnp_executable, pnp_dir = search_ancestors_for_executable(params.bufname, root, ".pnp.cjs")
62-
if not (pnp_executable and pnp_dir) then
63-
pnp_executable, pnp_dir = search_ancestors_for_executable(params.bufname, root, ".pnp.js")
64-
end
65-
if not (pnp_executable and pnp_dir) then
66-
error("failed to find yarn executable")
67-
end
61+
-- older yarn versions use `.pnp.js`, so look for both new and old names
62+
local pnp_executable, pnp_dir = search_ancestors_for_executable(params.bufname, root, ".pnp.cjs")
63+
if not (pnp_executable and pnp_dir) then
64+
pnp_executable, pnp_dir = search_ancestors_for_executable(params.bufname, root, ".pnp.js")
65+
end
66+
if not (pnp_executable and pnp_dir) then
67+
error("failed to find yarn executable")
68+
end
6869

69-
local yarn_bin_cmd =
70-
string.format("cd %s && yarn bin %s", vim.fn.shellescape(pnp_dir), vim.fn.shellescape(params.command))
71-
local yarn_bin = vim.fn.system(yarn_bin_cmd):gsub("%s+", "")
72-
if vim.v.shell_error ~= 0 then
73-
error("failed to get yarn bin")
74-
end
70+
local yarn_bin_cmd =
71+
string.format("cd %s && yarn bin %s", vim.fn.shellescape(pnp_dir), vim.fn.shellescape(params.command))
72+
local yarn_bin = vim.fn.system(yarn_bin_cmd):gsub("%s+", "")
73+
if vim.v.shell_error ~= 0 then
74+
error("failed to get yarn bin")
75+
end
7576

76-
return { "node", "--require", pnp_executable, yarn_bin }
77-
end)
77+
return { "node", "--require", pnp_executable, yarn_bin }
78+
end)
7879

79-
return ok and yarn_command or params.command
80-
end)
80+
return ok and yarn_command or params.command
81+
end)
82+
end
8183

8284
return M

lua/null-ls/helpers/make_builtin.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ local function make_builtin(opts)
8181
if prefer_local or only_local then
8282
local maybe_prefix = prefer_local or only_local
8383
local prefix = type(maybe_prefix) == "string" and maybe_prefix or nil
84-
local resolver = cmd_resolver.make_generic_resolver(prefix)
84+
local resolver = cmd_resolver.generic(prefix)
8585

8686
generator_opts.dynamic_command = function(params)
8787
local resolved_command = resolver(params) or (prefer_local and params.command)

0 commit comments

Comments
 (0)