nvim-eslint is a plugin designed to integrate ESLint functionality within Neovim. This plugin leverages the vscode-eslint language server and nvim native language client API to provide linting and code analysis capabilities directly in Neovim.
I initially attempted to use the ESLint LSP provided by mason-lspconfig.nvim, which utilizes vscode-langservers-extracted as the ESLint language server and nvim-lspconfig for configuring the Neovim language server client. Unfortunately, this setup did not work well with my monorepo managed by pnpm workspaces. The various projects under the packages/ directory failed to function simultaneously.
While troubleshooting, I discovered that the available documentation was sparse, and debugging was nearly impossible. The vscode-eslint plugin documentation focuses on configuring its own client, which differs from the parameters received by the server. Additionally, the server configuration provided by nvim-lspconfig was outdated and challenging to update. Understanding the client code from nvim-lspconfig and the server code from vscode-eslint without live debugging proved to be extremely difficult. To enable live debugging, building the server from source was necessary, as opposed to using the pre-packaged version from vscode-langservers-extracted. The complexity of nvim-lspconfig, which supports multiple languages, made it more practical to develop my own solution.
Therefore, I decided to start from simple, and tried to understand all configurations of ESLint server.
- Native Nvim LSP and vscode-eslint server: Achieve optimal performance with minimal differences from the ESLint VSCode plugin, as both use the same server and LSP protocol.
- All-in-one ESLint experience: No additional tools are required. The plugin includes the language server.
- Up-to-date with the latest vscode-eslint server: The plugin uses the latest release of vscode-eslint, ensuring support for new features like ESLint 9.
- Easy debugging: Includes debug instructions and a VSCode
launch.jsonfile to help you troubleshoot and resolve issues efficiently. - Easy to extend: The Lua script is concise, with just over 100 lines of code. It uses optimized default configurations but is also straightforward to customize according to your needs.
node and ESLint needs to be installed
Plug 'esmuellert/nvim-eslint'{
'esmuellert/nvim-eslint',
config = function()
require('nvim-eslint').setup({})
end,
}Add this to your lua configuration file:
require('nvim-eslint').setup({})In many cases, your repository might function with the default configurations. However, similar to the vscode-eslint plugin, you may need to adjust some settings to ensure compatibility. This is especially true for complex repository structures like monorepos or when using the latest flat config files in ESLint 9. The following section explains how to customize these default settings.
{
-- Toggle debug mode for ESLint language server, see debugging part
debug = false,
-- Command to launch language server. You might hardly want to change this setting
cmd = M.create_cmd(),
-- root_dir is used by Neovim LSP client API to determine if to attach or launch new LSP
-- The default configuration uses the git root folder as the root_dir
-- For monorepo it can have many projects, so launching too many LSP for one workspace is not efficient
-- You can override it with passing function(bufnr)
-- It should receive active buffer number and return root_dir
root_dir = M.resolve_git_dir(args.buf),
-- A table used to determine what filetypes trigger the start of LSP
filetypes = { 'javascript', 'javascriptreact', 'javascript.jsx', 'typescript', 'typescriptreact',
'typescript.tsx', 'vue', 'svelte', 'astro'},
-- The client capabilities for LSP Protocol. See Nvim LSP docs for details
-- It uses the default Nvim LSP client capabilities. Adding the capability to dynamically change configs
capabilities = M.make_client_capabilities(),
handlers = {
-- The handlers handles language server responses. See Nvim LSP docs for details
-- The default handlers only has a rewrite of default "workspace/configuration" handler of Nvim LSP
-- Basically, when you load a new buffer, ESLint LSP requests the settings with this request
-- To make it work with monorepo, the workingDirectory setting needs to be calculated at runtime
-- This is the main reaason for rewriting, and it also works if you have a simple structure repo
-- You might add more custom handler with reference to LSP protocol spec and vscode-eslint code
},
-- The settings send to ESLint LSP. See below part for details.
settings = {
validate = 'on',
-- packageManager = 'pnpm',
useESLintClass = true,
useFlatConfig = function(bufnr)
return M.use_flat_config(bufnr)
end,
experimental = { useFlatConfig = false },
codeAction = {
disableRuleComment = {
enable = true,
location = 'separateLine',
},
showDocumentation = {
enable = true,
},
},
codeActionOnSave = { mode = 'all' },
format = false,
quiet = false,
onIgnoredFiles = 'off',
options = {},
rulesCustomizations = {},
run = 'onType',
problems = { shortenToSingleLine = false },
nodePath = function(bufnr)
return M.resolve_node_path()
end,
workingDirectory = { mode = 'location' },
workspaceFolder = function(bufnr)
local git_dir = M.resolve_git_dir(bufnr)
return {
uri = vim.uri_from_fname(git_dir),
name = vim.fn.fnamemodify(git_dir, ':t'),
}
end,
}
}ESLint LSP settings are exactly the same as the parameters that vscode-eslint langage server receives:
These are the explanations for each option above. All function(bufnr) mentioned here is a Lua function that receives the number of the active buffer and returns the corresponding setting option. It is used to dynamically calculate some options, which is required for complicated scenarios.
All vscode-eslint settings options mentioned below is located at their docs.
-
validate: same aseslint.validateoption invscode-eslintsettings options -
packageManager: already deprecated byvscode-eslint, so I comment it out -
useESLintClass: same aseslint.useESLintClassoption invscode-eslintsettings options -
useFlatConfig: it can be either a function or a boolean value that has the same meaning ofeslint.useFlatConfigoption invscode-eslintsettings optionsThis option can be a bit tricky if you are using ESLint flat config files. The original explantion in
vscode-eslint:According to the doc, there are following scenarios:
- If you don't use flat config at all, you should set both
useFlatConfigandexperimental.useFlatConfigto false, and the default setting already does this for you. - If you use flat config:
- If 8.21 <= your ESLint version < 8.57, you should leave
useFlatConfigto false andexperimental.useFlatConfigto true, so you need to change the default setting. - If your ESLint version >= 8.57.0, you should set
useFlatConfigto true andexperimental.useFlatConfigto false, and the default setting already does this for you. - If your flat config file is not at the same folder of your
package.jsonfile, the default setting doesn't work for you. You need to rewrite theM.use_flat_configfunction.
- If 8.21 <= your ESLint version < 8.57, you should leave
- If you don't use flat config at all, you should set both
-
experimental.useFlatConfig: already explained in the last config -
codeAction: same aseslint.codeActionoption invscode-eslintsettings options -
codeActionOnSave: same aseslint.codeActionOnSaveoption invscode-eslintsettings options -
format: same aseslint.formatoption invscode-eslintsettings options -
quiet: same aseslint.quietoption invscode-eslintsettings options -
onIgnoredFiles: same aseslint.onIgnoredFilesoption invscode-eslintsettings options -
options: same aseslint.optionsoption invscode-eslintsettings options -
rulesCustomizations: same aseslint.rules.customizationsoption invscode-eslintsettings options -
run: same aseslint.runoption invscode-eslintsettings options -
problems: same aseslint.problemsinvscode-eslintsettings options -
nodePath: The node path will be used to resolve the path of your ESLint package. It should work if you leave it empty by setting it tonil, but it doesn't correctly resolve the ESLint path for some of my monorepo packages, so I set it to my global node path. If this default setting doesn't work for you, you can set it tonilto let the ESLint LSP resolve the node path for you. -
workingDirectory: This setting is critical and often a source of issues. It defines the directory from which the LSP will execute the ESLint command to lint your files. If this directory is incorrect, linting may fail. Typically, the working directory should be where your ESLint configuration file is located. Consider the following scenarios:-
Simple Repository Structure: If your
package.json, ESLint configuration file, and.gitare at the same level, use the defaultmode: locationoption. The ESLint LSP will use either your workspace folder (usually the.gitfolder) or the file location as the working directory. In practice, it will use the workspace folder, which is correct for this setup. No configuration changes are needed. -
Monorepo Structure: If your repository is a monorepo with independent packages under a
/packagesfolder, each package should have its ownpackage.jsonand ESLint configuration file. You must dynamically detect the working directory for each buffer, as it can change. The built-inmodeoption of ESLint LSP doesn't support this scenario. You can write a function to handle it:function(bufnr) return { directory = vim.fs.root(bufnr, {'package.json'}) } end
This function sets the
package.jsonfolder of the current buffer as the working directory. Note that ESLint LSP accepts two formats forworkingDirectory:{ mode = 'location' }and{ directory = 'your/workingDirectory'}. -
Multi-package Single Project: If your repository has multiple
package.jsonfiles for subfolders but is built as a single project at the root level, the working directory should be the root folder. If not, determine the working directory for each subfolder and write a function to set it correctly.
If none of these scenarios fit your setup, you may need to write a custom function to determine the working directory.
Note: this parameter is very likely to be confused with
workingDirectoriesparameter. The detail of the difference is explained here: #1 (comment) -
-
workspaceFolder: In Neovim, the concept of a workspace folder is less relevant compared to VSCode, as Neovim primarily operates with buffers. Typically, in VSCode, the workspace folder is the root directory of your project, often the git root. For Neovim, this plugin uses the git root folder as the workspace folder by default. This setting is mainly useful for determining the working directory as described earlier. If the default setting does not suit your needs, you may need to customize it.
If the linter doesn't work for your project, there are several ways to troubleshoot.
First, enable debug level logs for Nvim LSP with vim.lsp.set_log_level('debug'). This will show detailed requests and responses sent to and received from the ESLint language server, helping you identify issues related to configuration, handlers, or the language server startup.
If the issue seems to originate from the ESLint language server itself, you can attach to the Node.js process for debugging:
- Run the
build-eslint-language-server.shscript in the root folder of the repo with the debug option:./build-eslint-language-server.sh --debug. This will clone thevscode-eslintproject and compile the language server with source maps enabled, allowing you to set breakpoints in TypeScript files. - Open VSCode from the root folder of the repository. Then, set
debug = truein the plugin configuration and open a buffer you want to lint. The language server will start when you clickstart debuggingin the VSCode debug tab. Thelaunch.jsonfile contains the correct debugger configuration, so it should work immediately.- This should also work with other debuggers as long as you can attach to the language server process and enable source maps. Feel free to share steps for other debuggers via a PR.
- Set breakpoints and debug. The key files to focus on are
vscode-eslint/server/src/eslint.tsandvscode-eslint/server/src/eslintServer.ts. Theeslint.tsfile contains the language server logic, whileeslintServer.tscontains handlers.- Common issues often relate to configurations, so set a breakpoint at this line to check if the language server receives the correct configuration.
- Ensure the ESLint path is correctly resolved at this line, which is a common issue for monorepos.
- You may also need to review the
vscode-eslintlanguage server client code to understand the correct client behavior if you plan to modify the Nvim LSP client. The client code is located atvscode-eslint/client/src/client.ts.
Contributions are welcome! Please fork the repository and submit a pull request for any enhancements or bug fixes.
nvim-eslint is licensed under the MIT License. See the LICENSE file for more details.
We used the original ESLint language server developed by Microsoft. Their license is: LICENSE
The ESLint LSP configs are partly taken from nvim-lspconfig. Their license is: LICENSE
As mentioned above we rewrite a Neovim LSP handler. Their license is: LICENSE
