ruff: Add an `--exit-non-zero-on-fix` equivalent to the formatter.
Title says it all really. --exit-non-zero-on-change? I think this would be useful when using tools like pre-commit with CI in the same way as the checker’s --exit-non-zero-on-fix.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 13
- Comments: 22 (12 by maintainers)
Commits related to this issue
- https://github.com/astral-sh/ruff/issues/8191#issuecomment-1781220297 — committed to TheReverend403/cappuccino by TheReverend403 3 years ago
- https://github.com/astral-sh/ruff/issues/8191#issuecomment-1781220297 — committed to TheReverend403/gentoogram-bot by TheReverend403 3 years ago
- Add `--exit-non-zero-on-format` (#16009) ## Summary Fixes #8191 by introducing `--exit-non-zero-on-format` to `ruff format` which pretty much does what it says on the tin. ## Test Plan Added a new... — committed to astral-sh/ruff by thejcannon 2 years ago
the use case for us would be to have a single
formatmakefile target that can be used both for:something like
without this we need separate targets for dev and CI
Also need this. Like above its fine to make the changes “ruff format” - we just need to know if it ended up making any changes…
If “ruff format” returned a 1 if it made changes then we know. We stop the pre-commit. Developer does a git status and can see we already fixed some things. They review and do a commit and all is good.
Otherwise, as mentioned before we have to run ruff twice. (Though since it is so much faster than black that isn’t as bad as it could be! 😃
@zanieb I do have a need for this, so perhaps this could be re-opened? My use case: I can’t use pre-commit, because my project is a subdirectory in a larger git repo (“monorepo”). pre-commit decided to not support this use case, so I have my own “pre-commit” script, which indeed, I want to fail if it makes changes. Currently I wrote this:
but having a --exit-non-zero-on-fix would be much nicer.
If this is approved, I will be happy to try and implement this.
Thanks!
FYI,
--exit-non-zero-on-fixin the checker isn’t required in pre-commit; pre-commit checks for changed files.Oh sorry. I misunderstood you. If I understand you correctly, you’re asking that
ruff format --checkexits with a non-zero exit code if it would reformat any files but it would also write the changes back to disk so that runningruff formatisn’t necessary locally.I’m a bit torn where to integrate this functionality best. We could either to
ruff format --exit-non-zero-on-fix(undecided on the name of the flag) without using check. However, the option would be incompatible when using with--checkor--diff.Did you use black before? If so, how did your setup look like?
I have a similar need. I’m moving to moon for monorepo task management, and it has most of what I need for a pre-commit alternative, and by eliminating pre-commit I can have a single source of truth for task execution.
As a developer running a formatter on pre-commit, I want the formatter to format my code, but I also want pre-commit to abort so that the user can review the changes that were made (same behavior as with
pre-committool). As it stands, I have to runruff formattwice: with and without--check.Exactly, but as an additional flag rather than changing
--checkso as to avoid breaking everyone’s current CI workflows. 😏I used black, just not with this behaviour. It was actually ruff’s own
--exit-non-zero-on-fixthat gave me this idea for the formatter.This seems like a reasonable approach if we do implement it. It isn’t incompatible with
--checkthough, it just has no effect. With--diffI don’t know our exit code behavior.