Skip to content

Commit 9383556

Browse files
authored
fix(eslint): find closest ESLint directory to avoid version mismatch #3686
1 parent 40f120c commit 9383556

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

lua/lspconfig/configs/eslint.lua

+24
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,28 @@ local root_file = {
4747
'eslint.config.cts',
4848
}
4949

50+
--- Find the closest directory containing the ESLint library.
51+
--- Using the repository root as the ESLint library location is a common practice
52+
--- but handling different versions in monorepos can cause the LSP not to point to
53+
--- the correct instance.
54+
---
55+
--- @return string|nil (path) The path to the closest directory containing the ESLint library, or nil if not found.
56+
local function get_eslint_closest_dir()
57+
local cwd = vim.fn.getcwd()
58+
local eslint_node_modules = vim.fn.finddir('node_modules/eslint', cwd .. ';')
59+
60+
if eslint_node_modules == '' then
61+
return nil
62+
end
63+
64+
if eslint_node_modules == 'node_modules/eslint' then
65+
return cwd
66+
end
67+
68+
-- Strip the node_modules/eslint part
69+
return eslint_node_modules:match '(.*)/node_modules/eslint'
70+
end
71+
5072
return {
5173
default_config = {
5274
cmd = { 'vscode-eslint-language-server', '--stdio' },
@@ -102,6 +124,8 @@ return {
102124
},
103125
},
104126
on_new_config = function(config, new_root_dir)
127+
new_root_dir = get_eslint_closest_dir() or new_root_dir
128+
105129
-- The "workspaceFolder" is a VSCode concept. It limits how far the
106130
-- server will traverse the file system when locating the ESLint config
107131
-- file (e.g., .eslintrc).

0 commit comments

Comments
 (0)