Skip to content

Conversation

@johannhof
Copy link
Contributor

This changes rustfmt to return the number
of found differences as exit code when run with
write mode diff.

Useful for CI to make sure your contributors actually ran rustfmt.

@johannhof
Copy link
Contributor Author

@nrc r? I wasn't sure about a good way to test this, did I overlook something?

@nrc
Copy link
Member

nrc commented Jun 20, 2016

cc @matklad to check this doesn't break any of your uses

src/summary.rs Outdated
// Code is valid, but it is impossible to format it properly.
has_formatting_errors: bool,

pub diff_result: i32,
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a comment for this field

@matklad
Copy link
Contributor

matklad commented Jun 21, 2016

cc @matklad to check this doesn't break any of your uses

Ah, we've not yet implemented tight rustfmt integration, so nothing will break on our side :( (But our own formatter is improving by leaps and bounds).

However, I am not sure that using the number of errors as an exit code is the best possible approach. I'd rather simply return zero or one. If tools need to know what exactly is the number of differences, they'd better parse the output of the diff command.

@johannhof
Copy link
Contributor Author

However, I am not sure that using the number of errors as an exit code is the best possible approach. I'd rather simply return zero or one.

Can you elaborate why?

@matklad
Copy link
Contributor

matklad commented Jun 21, 2016

The main reason is that if rustfmt fails (panics or something), then we can't report this because if we return a non zero exit code, it will be interpreted as number of formatting errors, and not as the crash of the rustfmt itself. Here is an excerpt from diff -h: "Exit status is 0 if inputs are the same, 1 if different, 2 if trouble".

Some other concerns are:

  • People usually expect exit codes to be consistent, so they might end up writing something like if $(rustfmt $FILE) == 92, without realizing that 92 is not a stable return code and just a number of errors.
  • This is not self describing. How can the user find out what the return code actually means?
  • This is not extendable. What if we want to report a number of hard errors and a number of warnings?

@matklad
Copy link
Contributor

matklad commented Jun 21, 2016

The related PR is #902

@johannhof
Copy link
Contributor Author

Ok, that seems reasonable. I'll change it to, uh, 4. :)

@johannhof
Copy link
Contributor Author

@nrc @matklad updated

src/summary.rs Outdated
pub fn has_diff(&self) -> bool {
self.has_diff
}

Copy link
Member

Choose a reason for hiding this comment

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

we don't need this method, just make the field public

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, good point, wanted to stay consistent, but I guess it's really unnecessary

@nrc
Copy link
Member

nrc commented Jun 21, 2016

@johannhof thanks for the change. I just have one last comment, then r+.

This changes rustfmt to return exit code 4
when run with write mode diff and differences between
the formatted code and the original code are found.

Useful for CI to make sure your contributors actually ran rustfmt.
@johannhof
Copy link
Contributor Author

@nrc done

@nrc nrc merged commit 7c70254 into rust-lang:master Jun 22, 2016
@nrc
Copy link
Member

nrc commented Jun 22, 2016

Thanks!

@johannhof johannhof deleted the diff-exit branch June 23, 2016 04:41
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

Successfully merging this pull request may close these issues.

3 participants