Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solved switching between projects with and without flat config #288

Open
kommander opened this issue Apr 30, 2024 · 2 comments
Open

Solved switching between projects with and without flat config #288

kommander opened this issue Apr 30, 2024 · 2 comments

Comments

@kommander
Copy link

kommander commented Apr 30, 2024

I use nvim btw. Just though I'd share this here, as this was really annoying me and I spent quite some time getting it to work. I have to switch between projects with different setups and some are using the flat config already, some are not. Here's how I solved that:

In my lint.lua config I manually download and setup my fork branch with these two lines changes in /bin/eslint_d.js:

-process.env.CORE_D_TITLE = 'eslint_d';
-process.env.CORE_D_DOTFILE = '.eslint_d';
+process.env.CORE_D_TITLE = process.env.CORE_D_TITLE || 'eslint_d';
+process.env.CORE_D_DOTFILE = process.env.CORE_D_DOTFILE || '.eslint_d';

> Branch is here <

To setup nvim-lint, at startup I then check for existence of eslint.config.js in cwd and override the env that eslint_d is called in, to get a separate daemon starting up with ESLINT_USE_FLAT_CONFIG enabled.

        local env = nil
        local cwd = vim.loop.cwd()
        local maybe_config_path = vim.fn.fnamemodify(cwd .. '/eslint.config.js', ':p')
        local has_flat_config = vim.fn.filereadable(maybe_config_path) == 1

        if has_flat_config then
          env = {
            ['ESLINT_USE_FLAT_CONFIG'] = 'true',
            ['CORE_D_TITLE'] = 'eslint_d_flat',
            ['CORE_D_DOTFILE'] = '.eslint_d_flat',
            ['PWD'] = cwd,
            ['SHELL'] = os.getenv 'SHELL',
            ['TMPDIR'] = os.getenv 'TMPDIR',
            ['HOME'] = os.getenv 'HOME',
          }
        end

nvim-lint overrides the whole env and the additional variables are needed, otherwise it just does nothing and returns nothing.
Not the prettiest solution, but easiest and works wonders for my setup and blood pressure.

Anyway this option could go into a release so I can use the toolchain without manual installs again? @mantoni

Disclaimer: I didn't look into core_d yet and what the effect of using different daemons is here, but it works well for me and I only have the eslint_d and eslint_d_flat daemons running at all times and so far nothing got stuck on quite large projects.

@dantxal
Copy link

dantxal commented May 23, 2024

wish I knew how to do a manual install on nvim. using mason for now, still a lot to learn, gonna upvote your PR, hopefully I can get it to work soon

@kommander
Copy link
Author

I am just manually downloading my repo with the fix if it doesn't exist when none-ls is loaded in the config:

    local eslint_d_repo_path = vim.fn.stdpath 'data' .. '/eslint_d_patched'
    if not vim.loop.fs_stat(eslint_d_repo_path) then
      local eslint_d_repo = 'https://github.com/kommander/eslint_d.js'
      vim.fn.system { 'git', 'clone', '--filter=blob:none', '--branch=override-core-d-options', eslint_d_repo, eslint_d_repo_path }
      vim.fn.system { 'npm', 'install', '--prefix', eslint_d_repo_path }
    end

(Note: This is synchronous, so it blocks everything the first time, didn't bother making it async for now)

Then I just override the command and env in the setup (same thing can be done for the builtins in nvim-lint):

    local cmd_name = vim.fn.fnamemodify(eslint_d_repo_path .. '/bin/eslint_d.js', ':p')

    null_ls.setup {
      sources = {
        require('none-ls.code_actions.eslint_d').with {
          command = cmd_name,
          env = env,
        },
        require('none-ls.diagnostics.eslint_d').with {
          command = cmd_name,
          env = env,
        },
        ...
      },
    }

The repo does not have to be downloaded automatically in the config like this, you can also just manually clone the repo somewhere locally and point cmd_name to it and then do the override. Even if my PR gets merged, one would still have to do the command and env override for whatever plugin is being used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants