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

"RuntimeError: Ruff <0.2.0,>=0.0.291 required, but found 0.0.287" when trying to format #305

Closed
usrme opened this issue Nov 1, 2023 · 6 comments
Labels
question Further information is requested

Comments

@usrme
Copy link

usrme commented Nov 1, 2023

I wanted to try to use Ruff for formatting, but after writing any changes I get the following error and no formatting occurs:

RuntimeError: Ruff <0.2.0,>=0.0.291 required, but found 0.0.287 at /home/usrme/.local/pipx/venvs/ruff-lsp/bin/ruff

I've installed ruff-lsp using pipx install ruff-lsp and ruff isn't installed through other means

$ ruff-lsp --version
ruff-lsp 0.0.42

My null-ls.lua is as follows:

local null_ls = require "null-ls"
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})

local formatting = null_ls.builtins.formatting
local lint = null_ls.builtins.diagnostics

local sources = {
  formatting.goimports_reviser,
  formatting.gofmt,
  formatting.ruff,
  lint.shellcheck,
}

null_ls.setup {
  debug = true,
  sources = sources,
  on_attach = function(client, bufnr)
    if client.supports_method("textDocument/formatting") then
      vim.api.nvim_clear_autocmds({
        group = augroup,
        buffer = bufnr,
      })
      vim.api.nvim_create_autocmd("BufWritePre", {
        group = augroup,
        buffer = bufnr,
        callback = function()
          vim.lsp.buf.format({ bufnr = bufnr })
        end,
      })
    end
  end,
}

And my lspconfig.lua like this:

local on_attach = require("plugins.configs.lspconfig").on_attach
local capabilities = require("plugins.configs.lspconfig").capabilities

local lspconfig = require "lspconfig"
local servers = { "gopls", "ruff_lsp", "pyright" }

for _, lsp in ipairs(servers) do
  lspconfig[lsp].setup {
    on_attach = on_attach,
    capabilities = capabilities,
  }
end

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
  -- Enable completion triggered by <c-x><c-o>
  vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc')

  -- Mappings.
  -- See `:help vim.lsp.*` for documentation on any of the below functions
  local bufopts = { noremap=true, silent=true, buffer=bufnr }
  vim.keymap.set('n', 'gD', vim.lsp.buf.declaration, bufopts)
  vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
  vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
  vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, bufopts)
  vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
  vim.keymap.set('n', '<space>wa', vim.lsp.buf.add_workspace_folder, bufopts)
  vim.keymap.set('n', '<space>wr', vim.lsp.buf.remove_workspace_folder, bufopts)
  vim.keymap.set('n', '<space>wl', function()
    print(vim.inspect(vim.lsp.buf.list_workspace_folders()))
  end, bufopts)
  vim.keymap.set('n', '<space>D', vim.lsp.buf.type_definition, bufopts)
  vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, bufopts)
  vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, bufopts)
  vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
  vim.keymap.set('n', '<space>f', function() vim.lsp.buf.format { async = true } end, bufopts)
end

lspconfig.ruff_lsp.setup({
  on_attach = on_attach,
  capabilities = capabilities,
  init_options = {
    settings = {
      args = { "--line-length=120" },
    },
  },
})
lspconfig.pyright.setup({
  on_attach = on_attach,
  capabilities = capabilities,
  settings = {
    python = {
      venvPath = os.getenv("VIRTUAL_ENV")
    }
  }
})
@charliermarsh
Copy link
Member

I'm not exactly sure how, but it looks like your ruff-lsp is installed alongside an older version of Ruff. Can you try uninstalling and reinstalling ruff-lsp? Like pipx uninstall ruff-lsp && pipx install --force-reinstall ruff-lsp?

@charliermarsh charliermarsh added the question Further information is requested label Nov 1, 2023
@usrme
Copy link
Author

usrme commented Nov 1, 2023

That seems to have done the trick, though pipx doesn't have a --force-reinstall flag for the install sub-command. I also had to remove the formatting.ruff bit from my null-ls.lua file as that was expecting an executable for ruff somewhere along the PATH.

Now, while formatting with Space+f works, I seem to have lost inline hints (e.g. "line is too long", etc). Any idea what could cause that?

LspInfo shows that ruff_lsp is attached and running.

@charliermarsh
Copy link
Member

We removed the "line is too long" rule from the default rule set to make the out-of-the-box configuration more compatible with the formatter. You can change args = { "--line-length=120" } to args = { "--line-length=120", "--extend-select=E501" } or args = { "--line-length=120", "--extend-select=E" } to re-add it.

@usrme
Copy link
Author

usrme commented Nov 1, 2023

And we're off the to the races! Now, if I want to format based off of that preferred 120, I should create a configuration file to define that and reference that in the LSP's settings as defining line length isn't possible otherwise?

@dhruvmanila
Copy link
Member

We're probably going to add it to the format command - astral-sh/ruff#8363. Until then, you'll need to define it in a config file (pyproject.toml / ruff.toml / .ruff.toml) although you don't need to pass it as Ruff will pick it up automatically. Here's the documentation for that (https://docs.astral.sh/ruff/settings/#line-length). Although, if you define it in your config file then both the linter and the formatter will pick it up.

Separately, null-ls has been archived, I would suggest to move to using ruff-lsp directly or you can use dedicated plugins conform.nvim / nvim-lint.

@usrme
Copy link
Author

usrme commented Nov 1, 2023

@dhruvmanila, got it, thank you!

While I know that null-ls is archived, it still continues to serve me well and I'm afraid I don't want to spend even more time messing around with my configs if things already work 😅

Thank you both so much for your help, let's consider this case closed!

@usrme usrme closed this as completed Nov 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants