mason.nvim icon indicating copy to clipboard operation
mason.nvim copied to clipboard

[New package]: Eslint

Open chrisgrieser opened this issue 1 year ago • 9 comments

I've searched open & closed issues for similar requests

Yes

Package name

eslint

Package homepage

https://eslint.org/

Languages

typescript, javascript

How is this package distributed?

npm


Although there is eslint_d available via Mason, eslint itself is not. For null-ls, this does not make a difference, since eslint_d can fully replace eslint here.

But the eslint-lsp does require eslint on the system and does not work with eslint_d. So right now, you can install the eslint-lsp via mason, but still have to install eslint itself outside of mason.

chrisgrieser avatar Nov 25 '22 15:11 chrisgrieser

Hello! Pull requests are always very welcomed to add new packages. If the distribution of the package is simple, the installation will most likely be so as well. See CONTRIBUTING.md and the API reference for more details! You may also use existing packages as reference.

 🤖 This is an automated comment.    📖 Source code 

williambotman avatar Nov 25 '22 15:11 williambotman

~~I know eslint is not available, but does this work fulfill your needs: https://github.com/williamboman/mason.nvim/blob/main/lua/mason-registry/eslint_d/init.lua~~

jay-babu avatar Nov 29 '22 01:11 jay-babu

As I said, eslint_d works for null-ls, but not for the eslint-lsp

chrisgrieser avatar Nov 29 '22 01:11 chrisgrieser

Hey! So the ESLint language server is more or less hard-coded to look for eslint in the project local node_modules/ and if it can't find it there it'll try to find it "globally" (can't remember exactly what it considers the global location but it's something along the lines of npm root -g). Mason won't be able to install eslint in any of these locations. To use the ESLint language server I'd recommend you make sure eslint is installed in the project.

williamboman avatar Nov 29 '22 14:11 williamboman

Hmm, that's really a bummer, being able to manage all linters and formatters via Mason, expect one 😕

Maybe it would work via symlinking the eslint binary to whatever location the eslint-lsp requires? If it does, eslint via Mason would work?

chrisgrieser avatar Nov 29 '22 14:11 chrisgrieser

okay, I just tried installing eslint_d via Mason, and symlinking to the location where npm -g eslint would install eslint to normally:

ln -sf $HOME/.local/share/nvim/mason/packages/eslint_d/node_modules/eslint_d /opt/homebrew/lib/node_modules/eslint

Unfortunately, this does not seem to work for the eslint-LSP; not sure whether it's due to symlinking not being enough, or due to eslint_d being too different from eslint though

chrisgrieser avatar Nov 29 '22 14:11 chrisgrieser

It's been a while since I last studied the ESLint language server source code but I believe it uses Node.js APIs (i.e. const eslint = require("eslint/lib") to execute ESLint, it doesn't spawn child processes via the eslint executable. Symlinking the executable without also symlinking the underlying library code is probably also problematic due to relative paths often being used to load things (the eslint executable will try to load something like ../eslint/lib/cli.js).

But don't you have eslint installed in the projects where you want to use ESLint? Generally it's a good idea to make sure to run the appropriate version of a tool to ensure consistent, reproducible, results.

I just found the nodePath setting. It seems like it's available as an escape hatch to control where it should look for ESLint. Maybe you can try playing around with that setting? image

williamboman avatar Nov 29 '22 15:11 williamboman

hah, that was the needed stepstone.

So I installed eslint in the location it would be installed if available via Mason. In my case on mac:

cd "$HOME/.local/share/nvim/mason/packages"
mkdir eslint
cd eslint
npm i eslint

and then I added this to the eslint-lsp-settings:

require("lspconfig")["eslint"].setup {
	on_attach = on_attach,
	capabilities = capabilities,
	settings = {
		nodePath = os.getenv("HOME").."/.local/share/nvim/mason/packages/eslint/node_modules",
	},
}

and it does indeed work!

I checked, it does not work with using Mason's eslint_d and .../mason/packages/eslint_d/node_modules as nodePath, it really seems to require eslint specifically.

So if eslint were to be added to the mason packages, and changing the eslint-lsp nodePath, you could indeed get everything up and running purely with Mason. 🙏


But don't you have eslint installed in the projects where you want to use ESLint? Generally it's a good idea to make sure to run the appropriate version of a tool to ensure consistent, reproducible, results.

yes, but I often have small js scripts for various things (e.g., JXA for mac automation) which do not belong to any proper project.

chrisgrieser avatar Nov 29 '22 15:11 chrisgrieser

I think this should be added as a package. I was about to open an issue about it but I checked and there's one right here.

Android789515 avatar Jan 23 '24 01:01 Android789515