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: open
  • Created 8 months ago
  • Comments: 16 (9 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 .

@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!

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.

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?