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.
This commit is contained in:
William Boman 2022-07-05 12:48:13 +02:00 committed by GitHub
parent 4983febe06
commit 06161eca0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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)