Skip to content

ruff_fix can overwrite file correctly but my customized ruff_fix cannot. #713

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

Open
otakutyrant opened this issue May 27, 2025 · 0 comments

Comments

@otakutyrant
Copy link

From my understanding, ruff check --fix outputs either "All checks passed!" or some errors like this:

test.py:1:1: I001 [*] Import block is un-sorted or un-formatted
  |
1 | / import os
2 | | import abc
  | |__________^ I001
3 |
4 |   os.close
  |
  = help: Organize imports

Found 1 error.
[*] 1 fixable with the `--fix` option.

So I tried to customize the ruff_fix formatter like this:

local conform = {
    "stevearc/conform.nvim",
    event = "BufWritePost",
    opts = {
        formatters_by_ft = {
            -- TODO: replace them with language servers if the later is better.
            html = { "prettier" },
            css = { "prettier" },
            javascript = { "prettier" },
            typescript = { "prettier" },
            json = { "prettier" },
            yaml = { "prettier" },
            markdown = { "prettier" },
            python = {
                "ruff_fix", -- To fix auto-fixable lint errors.
                "ruff_format", -- To run the Ruff formatter.
            }, -- Although ruff is a ls, the official suggests using these formatters
            toml = {}, -- TOMP formatter is provided by taplo ls.
            lua = { "stylua" }, -- lua_ls' primitive formatter is disabled
        },
        format_on_save = {
            -- These options will be passed to conform.format()
            timeout_ms = 500,
            lsp_format = "fallback", -- use vim.lsp.buf.format() when no formatter available.
        },
        formatters = {
            -- Drop `--fix` from ruff_fix formatter in favor of pyproject.toml
            ruff_fix = {
                args = {
                    "check",
                    "--force-exclude",
                    "--exit-zero",
                    "--no-cache",
                    "--stdin-filename",
                    "$FILENAME",
                    "-",
                },
                stdin = true,
            },
        },
    },
}

And when I save any python file, the file content is overwrote as "All checks passed!" or some errors. To my surprise, the origin ruff_fix formatter works well anyway. But how?

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

1 participant