You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Validation mode to check if file is correctly formatted #105
This would be useful in CI systems where you would want the build to fail if the user hasn't formatted their code before the CI system runs.
Reacted by Gabriel Ittner, Christian Stein, Jonathan Bluett-Duncan, Philipp Wendler, alexkleiman, Stephan Schroevers, David Golub, Andrew January, Navaneeth Kishore and Chinna
We are using maven in our build at the moment (trying to move to gradle but transition is slow).
Anything we can do in our world?
I was thinking about doing a checksum diff as a simple fix unless there is a better solution out there.
We have a format.sh script that wraps the Maven plugin, and a Dangerfile containing the following (this is executed by Travis CI, only for PR builds):
# Run the formatter and check whether this introduces any changes. if !system('./format.sh && git diff --exit-code --name-status')fail'There are formatting errors, please run `./format.sh`.'end
@JLLeitschuh Interesting you mention Maven, as we (@nedtwigg and I) are in the middle of writing version 3.0 of Spotless, and one of the things @nedtwigg has been working on recently is splitting out the "core" of Spotless into its own lib so that it can eventually be used in non-Gradle contexts.
However, I don't know if he has any definite plans to support a Maven plugin, so if you're interested, I'd suggest you ask him on the issue he's made to track the spotless-lib efforts. (diffplug/spotless#56)
I honestly don't plan on supporting our maven build for that much longer either.
I'm in the process of rewriting our build using gradle groovy and gradle script kotlin however being a startup with many other things going on simeltaneously I've got a lot of moving targets.
You've been a lot of help.
Do you mind sharing the contents of your ./format.sh@Stephan202 ? Is it just the call out to the google-java-format jar??
Where the referenced plugin is a Jitpack'ed version of this branch, forked off talios/googleformatter-maven-plugin. (I recommend using the latest version instead. Reason I haven't upgraded our script yet is that I need to make sure we have precisely matching versions of the IDEA and Eclipse plugins. For that I am waiting on #80.)
The run-google-java-format project has a check-google-java-format.py script that checks whether a file is properly formatted. The project's README file tells how to integrate with make, ant, gradle, git pre-commit hooks, and gives some advice about running google-java-format for the first time.
I agree that this would be very useful as a core feature. I imagine that nearly everybody using google-java-format within a large organization maintains some sort of CI system to enforce that code is formatted properly. In any organization with more than a handful of developers, it is essential to ensure that every developer always submits code formatted to google-java-format's standards. Otherwise, there is no way to maintain a codebase's formatting without constantly causing merge conflicts for developers whenever a reformatting event takes place.
For these reasons, I view a validation mode as a hard requirement for use of google-java-format in a large organization. Because this is a requirement for use of google-java-format at scale, it makes sense as a core feature.
Are there advantages to adding this to the CLI over using something like the google-java-format <file> | diff <file> - to check if a file is already formatted?
For batch processing multiple files a single invocation of google-java-format would be faster, but we recommend validating only changed lines instead of the entire fire, which is only possible one file at a time anyways. The formatting algorithm will continue to change, so trying to be 100% compliant with the latest version is going to result in churn from unrelated formatting changes when editing files.
@cushon I would like to add that, for those of us who use Windows or AppVeyor, diff programs are not installed by default, and thus for me at least, it's far from obvious (1) what open-source command-line diff programs are available, and (2) how to chain the results of google-java-format with such a diff program to get the equivalent of a CI-friendly validation mode.
Thus, if precedence from gofmt and dartfmt dictates that google-java-format should not have a built-in validation mode, then I'd strongly advocate for there to be some sort of instructions in the README on how to use google-java-format as a validation mechanism in popular CI systems like Travis CI, AppVeyor and Jenkins.
if precedence from gofmt and dartfmt dictates that google-java-format should not have a built-in validation mode
Sorry, I was trying to say the opposite of that. gofmt -l and dartfmt -n both print the paths of the files whose contents would change if the formatter were run normally, which I think is what's being requested here. (clang-format is the exception, but that's ok.)
That's why I like "validate" / "validation mode" phrase. When a validation run returns zero, everything's fine. Indicating zero errors occured. A non-zero status means format validation failed.
Is printing the paths of files that need to be formatted also sufficient for that? Neither dartfmt or gofmt set the exit status in dry run mode by default, but dartfmt has an option (--set-exit-if-changed) to enable that behaviour.
Reacted by Jonathan Bluett-Duncan and Christian Stein
tl;dr - If I had to choose, I'd easily go with the CLI, but an API addition would be a nice-to-have.
are you interested in an addition to the API here, or just the CLI?
Ah, I'm sorry for not specifying that earlier. I'm more interested in the CLI command, but would be interested in both, for different reasons.
The CLI command would be great for all the reasons listed above. With just a line or two of code, it would be possible to write the desired automation which uses google-java-format.
An API addition could be useful as an optimization. An API method would:
Eliminate the need to read the formatted and unformatted versions of a file into memory
Eliminate the need to perform string comparisons on these two versions
Potentially eliminate the need to format an entire file, as a file with a formatted error on line 1 doesn't need to be examined any further.
Now, this would only be a benefit if the API call didn't do all of this under the hood. I wouldn't expect the API call to be more efficient on it's first implementation, but one can always dream for the future, right?
One additional consideration: it's easier for Gradle plugins to lead the google-java-format library on the runtime classpath and call its API than it is to make a CLI call. Using the API also enables users to easily specify the version of google-java-format they want the Gradle plugin to use, as is done with the gradle-errorprone-plugin.
Thanks for your thoughtful consideration of this feature request!
That's why I like "validate" / "validation mode" phrase. When a validation run returns zero, everything's fine. Indicating zero errors occured. A non-zero status means format validation failed.
Is printing the paths of files that need to be formatted also sufficient for that? Neither dartfmt or gofmt set the exit status in dry run mode by default, but dartfmt has an option (--set-exit-if-changed) to enable that behaviour.
Only printing the paths of files that need to be formatted would yield a "output analyzing" post-processing pass to check if s/t was printed or not.
The original request here reads:
This would be useful in CI systems where you would want the build to fail if the user hasn't formatted their code before the CI system runs.
Having a non-zero exit code in "validation mode" could reach that goal w/o any extra effort.
I just pointed out that other formatters don't do that by default. Making the interfaces similar makes it easier to integrate them into other tools (e.g. for presubmit checks...), and makes it easier to learn them. It sounds like the way dartfmt handles this would work for your use-case?
Redirect the output, if any, to a file and test for its existence using OS dependent utilities. Feels not so good, but manageable.
Following dartfmt -n-style, I'll guard the allOk = falseline with an configurable switch. Plus an update to the command line description and the test case?
... but dartfmt has an option (--set-exit-if-changed) to enable that behaviour.
Is this switch limited to the dry run mode? Or if set to true does dartfmt always report an error code if it changed (printed the file paths in dry run) some files?
Activity
jbduncan commentedon Dec 12, 2016
FWIW, Spotless (which supports google-java-format) already allows the user to do this. :)
JLLeitschuh commentedon Dec 12, 2016
We are using maven in our build at the moment (trying to move to gradle but transition is slow).
Anything we can do in our world?
I was thinking about doing a checksum diff as a simple fix unless there is a better solution out there.
Stephan202 commentedon Dec 12, 2016
We have a
format.shscript that wraps the Maven plugin, and aDangerfilecontaining the following (this is executed by Travis CI, only for PR builds):jbduncan commentedon Dec 12, 2016
@JLLeitschuh Interesting you mention Maven, as we (@nedtwigg and I) are in the middle of writing version 3.0 of Spotless, and one of the things @nedtwigg has been working on recently is splitting out the "core" of Spotless into its own lib so that it can eventually be used in non-Gradle contexts.
However, I don't know if he has any definite plans to support a Maven plugin, so if you're interested, I'd suggest you ask him on the issue he's made to track the spotless-lib efforts. (diffplug/spotless#56)
JLLeitschuh commentedon Dec 12, 2016
I honestly don't plan on supporting our maven build for that much longer either.
I'm in the process of rewriting our build using gradle groovy and gradle script kotlin however being a startup with many other things going on simeltaneously I've got a lot of moving targets.
You've been a lot of help.
Do you mind sharing the contents of your
./format.sh@Stephan202 ? Is it just the call out to the google-java-format jar??Stephan202 commentedon Dec 12, 2016
@JLLeitschuh: there's some other (actually unused) stuff in there, but the no-arg invocation boils down to:
Where the referenced plugin is a Jitpack'ed version of this branch, forked off talios/googleformatter-maven-plugin. (I recommend using the latest version instead. Reason I haven't upgraded our script yet is that I need to make sure we have precisely matching versions of the IDEA and Eclipse plugins. For that I am waiting on #80.)
mernst commentedon Dec 16, 2016
The run-google-java-format project has a
check-google-java-format.pyscript that checks whether a file is properly formatted. The project's README file tells how to integrate with make, ant, gradle, git pre-commit hooks, and gives some advice about running google-java-format for the first time.sormuras commentedon Dec 17, 2016
I still think, a validation mode is a useful core addition.
alexkleiman commentedon May 4, 2017
I agree that this would be very useful as a core feature. I imagine that nearly everybody using google-java-format within a large organization maintains some sort of CI system to enforce that code is formatted properly. In any organization with more than a handful of developers, it is essential to ensure that every developer always submits code formatted to google-java-format's standards. Otherwise, there is no way to maintain a codebase's formatting without constantly causing merge conflicts for developers whenever a reformatting event takes place.
For these reasons, I view a validation mode as a hard requirement for use of google-java-format in a large organization. Because this is a requirement for use of google-java-format at scale, it makes sense as a core feature.
sormuras commentedon May 25, 2017
FWIW, you may use https://jitpack.io to get a compiled version of
google-java-formatwith integrated validation mode:https://jitpack.io/#sormuras/google-java-format/validate-SNAPSHOT
If you want to just download the all-deps JAR, you can grab it here: https://jitpack.io/com/github/sormuras/google-java-format/google-java-format/validate-SNAPSHOT/google-java-format-validate-SNAPSHOT-all-deps.jar
I'll try to keep the validate branch as up-to-date as possible.
Add --dry-run option to simplify CI system integrations
Add --dry-run option to simplify CI system integrations
Add --dry-run option to simplify CI system integrations
Add --dry-run option to simplify CI system integrations
cushon commentedon Oct 9, 2017
Are there advantages to adding this to the CLI over using something like the
google-java-format <file> | diff <file> -to check if a file is already formatted?For batch processing multiple files a single invocation of
google-java-formatwould be faster, but we recommend validating only changed lines instead of the entire fire, which is only possible one file at a time anyways. The formatting algorithm will continue to change, so trying to be 100% compliant with the latest version is going to result in churn from unrelated formatting changes when editing files.7 remaining items
jbduncan commentedon Oct 10, 2017
@cushon I would like to add that, for those of us who use Windows or AppVeyor, diff programs are not installed by default, and thus for me at least, it's far from obvious (1) what open-source command-line diff programs are available, and (2) how to chain the results of google-java-format with such a diff program to get the equivalent of a CI-friendly validation mode.
Thus, if precedence from
gofmtanddartfmtdictates that google-java-format should not have a built-in validation mode, then I'd strongly advocate for there to be some sort of instructions in the README on how to use google-java-format as a validation mechanism in popular CI systems like Travis CI, AppVeyor and Jenkins.cushon commentedon Oct 10, 2017
Sorry, I was trying to say the opposite of that.
gofmt -landdartfmt -nboth print the paths of the files whose contents would change if the formatter were run normally, which I think is what's being requested here. (clang-formatis the exception, but that's ok.)Is printing the paths of files that need to be formatted also sufficient for that? Neither
dartfmtorgofmtset the exit status in dry run mode by default, butdartfmthas an option (--set-exit-if-changed) to enable that behaviour.alexkleiman commentedon Oct 10, 2017
@cushon thanks for your response!
tl;dr - If I had to choose, I'd easily go with the CLI, but an API addition would be a nice-to-have.
Ah, I'm sorry for not specifying that earlier. I'm more interested in the CLI command, but would be interested in both, for different reasons.
The CLI command would be great for all the reasons listed above. With just a line or two of code, it would be possible to write the desired automation which uses
google-java-format.An API addition could be useful as an optimization. An API method would:
Now, this would only be a benefit if the API call didn't do all of this under the hood. I wouldn't expect the API call to be more efficient on it's first implementation, but one can always dream for the future, right?
One additional consideration: it's easier for Gradle plugins to lead the
google-java-formatlibrary on the runtime classpath and call its API than it is to make a CLI call. Using the API also enables users to easily specify the version ofgoogle-java-formatthey want the Gradle plugin to use, as is done with the gradle-errorprone-plugin.Thanks for your thoughtful consideration of this feature request!
sormuras commentedon Oct 10, 2017
Only printing the paths of files that need to be formatted would yield a "output analyzing" post-processing pass to check if s/t was printed or not.
The original request here reads:
Having a non-zero exit code in "validation mode" could reach that goal w/o any extra effort.
You don't like the non-zero exit code. Why?
This how I use the modified GJF at the moment:
I could live with a second option to enable the "--set-exit-to-non-zero-if-validation-fails" behaviour. The usage would be enhanced to:
cushon commentedon Oct 10, 2017
I just pointed out that other formatters don't do that by default. Making the interfaces similar makes it easier to integrate them into other tools (e.g. for presubmit checks...), and makes it easier to learn them. It sounds like the way
dartfmthandles this would work for your use-case?sormuras commentedon Oct 10, 2017
Redirect the output, if any, to a file and test for its existence using OS dependent utilities. Feels not so good, but manageable.Following
dartfmt -n-style, I'll guard theallOk = falseline with an configurable switch. Plus an update to the command line description and the test case?sormuras commentedon Oct 10, 2017
Is this switch limited to the dry run mode? Or if set to
truedoesdartfmtalways report an error code if it changed (printed the file paths in dry run) some files?sormuras commentedon Oct 10, 2017
See 00b6b53 in #106 for
--set-exit-if-changedoption implementation. The exit code is only set to1ininPlaceorvalidatemode.What about the stdout case?
sormuras commentedon Oct 10, 2017
If I understand https://github.com/dart-lang/dart_style/blob/master/lib/src/formatter_options.dart#L216 correctly, it always returns
1if any changes are made. That includes the stdout case.I'll update the PR according to this.
cushon commentedon Oct 10, 2017
That matches what I'm seeing, and seems reasonable.
Do you have concerns about printing the names of files that need formatting?
sormuras commentedon Oct 10, 2017
No. See https://github.com/google/google-java-format/pull/106/files#diff-e2a1fb24af1354460a3ee75611e8a298R160
A small cleanup is being forced pushed in a minute...
sormuras commentedon Oct 11, 2017
...pushed.
Add dry-run mode and --set-exit-if-changed option
sormuras commentedon Oct 12, 2017
After merging #106 this issue is finally closeable. 🌞