nvim-lint icon indicating copy to clipboard operation
nvim-lint copied to clipboard

eslint_d unable to recognize the 9.x version of the configuration file

Open imddc opened this issue 1 year ago • 6 comments

Could not parse linter output due to: Expected value but found invalid token at character 1 output: Error: No ESLint configuration found in /Users/dccd/Desktop/item/front/vauch/src.

it's ok to use .eslintrc.json, but i do not want to use the legacy cofiguration file. 😭

imddc avatar Jun 27 '24 17:06 imddc

I was having the same issue ! I realised when reading the README of the eslint_d package, that they don't completely support the new config format yet.

Experimental support for eslint flat config is available since v13.1.0 if the ESLINT_USE_FLAT_CONFIG environment variable is defined.

I guess a fix would be to add the option to add this environment variable when running the linter. I'm just arriving here to this repo, I'll try to understand how it works and make a PR if it could help !

Mouarius avatar Jun 28 '24 22:06 Mouarius

Ok some discoveries about the issue and how to solve it !

As described on the repo of eslint_d, the feature for using the new eslint config is experimental, and should be activated by spawning the eslint_d server with the ESLINT_USE_FLAT_CONFIG=true variable. What I understood is that the eslint_d works by having a process (named eslint_d) that orchestrates multiple instances of eslint that are run each time it is being called. But killing and calling back a process for a file (what is done internally by nvim-lint) doesn't kill and restart the parent eslint_d server (that's one of the reasons why it is faster than eslint).

For that you need to run ESLINT_USE_FLAT_CONFIG=true ./path/to/eslint_d restart to restart the server with the flag on. (if you use Mason to manage your packages, you could find the eslint_d binary in ~/.local/share/nvim/mason/bin/eslint_d )

Note : I've tried to add this environment variable to the env property of the nvim-lint linter config, but for some reason, when I try to add it there, everything stops working... I couldn't find why.

Then after restarting Neovim, you should probably run into some errors about not finding the config file for your project. You can solve it by configuring the --config flag to your linter configuration.

Here is my configuration that works :

return {
  'mfussenegger/nvim-lint',
  event = { 'BufReadPre', 'BufNewFile' },
  config = function()
    local lint = require 'lint'
    lint.linters_by_ft = {
      javascript = { 'eslint_d' },
      javascriptreact = { 'eslint_d' },
      typescript = { 'eslint_d' },
      typescriptreact = { 'eslint_d' },
    }

    local eslint_d = require 'lint.linters.eslint_d'
    eslint_d.args = vim.tbl_extend('force', {
      '--config',
      function()
        return vim.fn.getcwd() .. '/eslint.config.js'
      end,
    }, eslint_d.args)

    local lint_augroup = vim.api.nvim_create_augroup('lint', { clear = true })

    vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWritePost', 'InsertLeave' }, {
      group = lint_augroup,
      callback = function()
        lint.try_lint()
      end,
    })
  end,
}

Let me know if it works for you, and if it doesn't I'll be happy to help !

If someone reads this and has a solution to avoid restarting the process by hand I would really like to hear about it !

Mouarius avatar Jun 29 '24 22:06 Mouarius

Thank you for your contribution, but it doesn't seem to work for my project. The error message still exists.

imddc avatar Jul 01 '24 01:07 imddc

Thank you for your contribution, but it doesn't seem to work for my project. The error message still exists.

When you work on your project, do you open neovim in the directory that is the root of your project, and that contains your eslint.config.js file ?

Mouarius avatar Jul 01 '24 19:07 Mouarius

worked for me! But of course if you are switching projects then it wont work for < 9. Thanks!!!

boydkelly avatar Jul 02 '24 00:07 boydkelly

Hello! Well I have same problem, and I have project mono repo type, so my eslint.config.js is in vim.fn.getcwd() .. '/app-frontend/eslint.config.js'

zayihu avatar Aug 03 '24 15:08 zayihu

For anyone still fighting with this issue, it may not be an nvim-lint problem. I just contributed https://github.com/mantoni/eslint_d.js/pull/331 to the eslint_d repo which allows you to specify the root directory to look for the eslint node module when running eslint_d. This was released in [email protected]

This is useful in monorepos where the file structure of node_modules might be atypical. Here's a snippet of config that works for me with this change:

vim.env.ESLINT_D_PPID = vim.fn.getpid()
vim.env.ESLINT_D_ROOT = "/path/to/my/monorepo/root"

require("lint").linters_by_ft = {
	typescript = { "eslint_d" },
	javascript = { "eslint_d" },
	typescriptreact = { "eslint_d" },
	javascriptreact = { "eslint_d" },
}

vim.api.nvim_create_autocmd({ "BufWritePost" }, {
	callback = function()
		require("lint").try_lint()
	end,
})

mgramigna avatar Dec 19 '24 19:12 mgramigna

Uh, somebody could've @-mentioned me here 😉. With the rewrite of eslint_d in v14 the architecture changed completely and using old and new configs in different projects is no longer an issue.

There is also a new --debug flag to see what's going on behind the scenes. Hope this helps with debugging.

Please upgrade and let me know if this is still an issue. 🙏

mantoni avatar Dec 20 '24 19:12 mantoni

I can't get it to work in my monorepo setup :( @mantoni . Here is my config:

return {
  "mfussenegger/nvim-lint",
  event = "BufReadPre",
  init = function()
    vim.env.ESLINT_D_PPID = vim.fn.getpid()
    require("lint").linters_by_ft = {
      javascript = { "eslint_d" },
      typescript = { "eslint_d" },
      typescriptreact = { "eslint_d" },
    }

    vim.api.nvim_create_autocmd({ "BufEnter", "BufWritePost" }, {
      callback = function()
        require("lint").try_lint()
      end,
    })
  end,
}

When I run eslint_d . manually in the correct directory it works.

Edit:

Ok this was due to nvim-lint not detecting my eslint config file in my mono repo setup. This has already been discussed in this issue. The solution is the following:

        local client = vim.lsp.get_clients({ bufnr = 0 })[1] or {}
        require("lint").try_lint(nil, { cwd = client.root_dir })

Credit: https://github.com/mfussenegger/nvim-lint/issues/482#issuecomment-1999185606

kedom1337 avatar Jan 13 '25 18:01 kedom1337

@kedom1337 In a monorepo you might want to set ESLINT_D_ROOT to the root of the current project. Otherwise eslint is executed in your monorepo root.

mantoni avatar Jan 13 '25 20:01 mantoni