Skip to content

Commit 60b65ec

Browse files
committed
feat(eslint): improve monorepo support for Eslint LSP
1 parent 525ff8b commit 60b65ec

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

lsp/eslint.lua

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,15 @@
3030
--- Messages handled in lspconfig: `eslint/openDoc`, `eslint/confirmESLintExecution`, `eslint/probeFailed`, `eslint/noLibrary`
3131
---
3232
--- Additional messages you can handle: `eslint/noConfig`
33+
---
34+
--- ### Monorepo support
35+
---
36+
--- `vscode-eslint-language-server` supports monorepos by default. It will automatically find the config file corresponding to the package you are working on. You can use different configs in different packages.
37+
--- This works without the need of spawning multiple instances of `vscode-eslint-language-server`.
38+
--- You can use a different version of ESLint in each package, but it is recommended to use the same version of ESLint in all packages. The location of the ESLint binary will be determined automatically.
39+
---
40+
--- /!\ When using flat config files, you need to use them accross all your packages in your monorepo, as it's a global setting for the server.
3341

34-
local util = require 'lspconfig.util'
3542
local lsp = vim.lsp
3643

3744
return {
@@ -62,27 +69,7 @@ return {
6269
}, nil, bufnr)
6370
end, {})
6471
end,
65-
-- https://eslint.org/docs/user-guide/configuring/configuration-files#configuration-file-formats
66-
root_dir = function(bufnr, on_dir)
67-
local root_file_patterns = {
68-
'.eslintrc',
69-
'.eslintrc.js',
70-
'.eslintrc.cjs',
71-
'.eslintrc.yaml',
72-
'.eslintrc.yml',
73-
'.eslintrc.json',
74-
'eslint.config.js',
75-
'eslint.config.mjs',
76-
'eslint.config.cjs',
77-
'eslint.config.ts',
78-
'eslint.config.mts',
79-
'eslint.config.cts',
80-
}
81-
82-
local fname = vim.api.nvim_buf_get_name(bufnr)
83-
root_file_patterns = util.insert_package_json(root_file_patterns, 'eslintConfig', fname)
84-
on_dir(vim.fs.dirname(vim.fs.find(root_file_patterns, { path = fname, upward = true })[1]))
85-
end,
72+
root_markers = { 'package-lock.json', 'yarn.lock', 'pnpm-lock.yaml', 'bun.lockb' },
8673
-- Refer to https://github.com/Microsoft/vscode-eslint#settings-options for documentation.
8774
settings = {
8875
validate = 'on',
@@ -107,7 +94,7 @@ return {
10794
-- This path is relative to the workspace folder (root dir) of the server instance.
10895
nodePath = '',
10996
-- use the workspace folder location or the file location (if no workspace folder is open) as the working directory
110-
workingDirectory = { mode = 'location' },
97+
workingDirectory = { mode = 'auto' },
11198
codeAction = {
11299
disableRuleComment = {
113100
enable = true,
@@ -142,7 +129,17 @@ return {
142129
}
143130

144131
for _, file in ipairs(flat_config_files) do
145-
if vim.fn.filereadable(root_dir .. '/' .. file) == 1 then
132+
local found_files = vim.fn.globpath(root_dir, file, true, true)
133+
134+
-- Filter out files inside node_modules
135+
local filtered_files = {}
136+
for _, found_file in ipairs(found_files) do
137+
if string.find(found_file, '/node_modules/') == nil then
138+
table.insert(filtered_files, found_file)
139+
end
140+
end
141+
142+
if #filtered_files > 0 then
146143
config.settings.experimental = config.settings.experimental or {}
147144
config.settings.experimental.useFlatConfig = true
148145
break

0 commit comments

Comments
 (0)