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

Most upvoted comments

the use case for us would be to have a single format makefile target that can be used both for:

  • developers formatting their local repo
  • CI jobs checking the formatting

something like

.PHONY: format
format:
	poetry run ruff format . --exit-error-on-fix

without this we need separate targets for dev and CI

.PHONY: format-check
format-check:
	poetry run ruff format . --diff

.PHONY: format
format:
        poetry run ruff format .

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:

if ! ruff format --check .; then ruff format .; false; fi

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-fix in 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 --check exits with a non-zero exit code if it would reformat any files but it would also write the changes back to disk so that running ruff format isn’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 --check or --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-commit tool). As it stands, I have to run ruff format twice: with and without --check.

If I understand you correctly, you’re asking that ruff format --check exits with a non-zero exit code if it would reformat any files but it would also write the changes back to disk so that running ruff format isn’t necessary locally.

Exactly, but as an additional flag rather than changing --check so as to avoid breaking everyone’s current CI workflows. 😏

Did you use black before? If so, how did your setup look like?

I used black, just not with this behaviour. It was actually ruff’s own --exit-non-zero-on-fix that gave me this idea for the formatter.

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 --check or --diff.

This seems like a reasonable approach if we do implement it. It isn’t incompatible with --check though, it just has no effect. With --diff I don’t know our exit code behavior.