From 06161eca0aaaafbede0234216aefaed2e5eb46d8 Mon Sep 17 00:00:00 2001 From: William Boman Date: Tue, 5 Jul 2022 12:48:13 +0200 Subject: feat(eslint): support yarn2 PnP projects #1777 https://yarnpkg.com/features/pnp Yarn's PnP feature changes the way packages are installed. Instead of building on the `node_modules` resolution, it introduces a single `.pnp.*js` file in the project. This file is responsible for orchestrating and resolving dependencies. The eslint LSP server will assume that regular `node_modules` resolution applies when locating the `eslint` package - which will not work in Yarn PnP projects. To work around this, Yarn provides the ability to run Node programs in "PnP-compat" mode via `yarn exec` and `yarn node`. My understanding is that this simply hooks into the `require()` function to resolve modules via PnP instead Node's builtin module resolution. --- lua/lspconfig/server_configurations/eslint.lua | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lua') diff --git a/lua/lspconfig/server_configurations/eslint.lua b/lua/lspconfig/server_configurations/eslint.lua index 81a796df..6cfc1e80 100644 --- a/lua/lspconfig/server_configurations/eslint.lua +++ b/lua/lspconfig/server_configurations/eslint.lua @@ -98,6 +98,13 @@ return { uri = new_root_dir, name = vim.fn.fnamemodify(new_root_dir, ':t'), } + + -- Support Yarn2 (PnP) projects + local pnp_cjs = util.path.join(new_root_dir, '.pnp.cjs') + local pnp_js = util.path.join(new_root_dir, '.pnp.js') + if util.path.exists(pnp_cjs) or util.path.exists(pnp_js) then + config.cmd = vim.list_extend({ 'yarn', 'exec' }, cmd) + end end, handlers = { ['eslint/openDoc'] = function(_, result) -- cgit v1.2.3-70-g09d2