Skip to content

Add --exit-non-zero-on-format to formatter exit codes section#22761

Merged
MichaReiser merged 3 commits intoastral-sh:mainfrom
alejsdev:docs/ruff-format-flag
Jan 21, 2026
Merged

Add --exit-non-zero-on-format to formatter exit codes section#22761
MichaReiser merged 3 commits intoastral-sh:mainfrom
alejsdev:docs/ruff-format-flag

Conversation

@alejsdev
Copy link
Contributor

Summary

The formatter's exit codes documentation at https://docs.astral.sh/ruff/formatter/#exit-codes doesn't mention the --exit-non-zero-on-format flag, even though it exists (it was added in #16009) and appears in ruff format --help, and the linter flag --exit-non-zero-on-fix is properly documented in the https://docs.astral.sh/ruff/linter/#exit-codes (which was used as a reference to add the proper section in the formatter, trying to keep the consistency)

@AlexWaygood AlexWaygood added the documentation Improvements or additions to documentation label Jan 20, 2026
- `2` if Ruff terminates abnormally due to invalid configuration, invalid CLI options, or an
internal error.

`ruff format` supports the `--exit-non-zero-on-format` command-line flag that alters its exit code behavior:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a slight preference to extend the list above over adding this section. It does make it clearer which exit code it changes.

`0` if Ruff terminates successfully, and no files would be formatted if `--check` and `--exit-non-zero-on-format` were not specified.
- `1` if Ruff terminates successfully, and one or more files would be formatted if `--check` or `--exit-non-zero-on-format` were specified.

It also seems that the if --check was not specified for 1, might be incorrect, or do I misunderstand the flags @ntBre ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand the question entirely, but I did some manual testing, and it seems like the flags work together as I would expect:

# no flags, reformatted, exits 0echo 'def foo(x,y,z): return x+y+z' > try.py && ruff format; echo $?
1 file reformatted
0
# --check, would reformat, exits 1echo 'def foo(x,y,z): return x+y+z' > try.py && ruff format --check; echo $?
Would reformat: try.py
1 file would be reformatted
1
# --exit-non-zero-on-format, reformatted, exits 1echo 'def foo(x,y,z): return x+y+z' > try.py && ruff format --exit-non-zero-on-format; echo $?
1 file reformatted
1
# --check AND --exit-non-zero-on-format, would reformat, still exits 1echo 'def foo(x,y,z): return x+y+z' > try.py && ruff format --check --exit-non-zero-on-format; echo $?
Would reformat: try.py
1 file would be reformatted
1

I think --exit-non-zero-on-format might fit nicely in the first list:

ruff format exits with the following status codes:

  • 0 if Ruff terminates successfully, regardless of whether any files were formatted.
  • 2 if Ruff terminates abnormally due to invalid configuration, invalid CLI options, or an
    internal error.

as:

  • 1 if Ruff terminates successfully, one or more files were formatted, and --exit-non-zero-on-format was specified

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, thank you!

@MichaReiser MichaReiser changed the title 📝 Add --exit-non-zero-on-format to formatter exit codes section Add --exit-non-zero-on-format to formatter exit codes section Jan 21, 2026
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you

@MichaReiser MichaReiser enabled auto-merge (squash) January 21, 2026 08:35
@MichaReiser MichaReiser merged commit 4e9de95 into astral-sh:main Jan 21, 2026
39 checks passed
@alejsdev alejsdev deleted the docs/ruff-format-flag branch January 27, 2026 09:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants