-
Notifications
You must be signed in to change notification settings - Fork 884
Validation mode to check if file is correctly formatted #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
FWIW, Spotless (which supports google-java-format) already allows the user to do this. :) |
We are using maven in our build at the moment (trying to move to gradle but transition is slow). |
We have a # 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. You've been a lot of help. |
@JLLeitschuh: there's some other (actually unused) stuff in there, but the no-arg invocation boils down to: mvn -B -T 1.0C com.github.PicnicSupermarket:googleformatter-maven-plugin:format 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 |
I still think, a validation mode is a useful core addition. |
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. |
FWIW, you may use https://jitpack.io to get a compiled version of 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. |
Fixes: google#105. Test Plan: $ git show --name-only HEAD | grep java$ | xargs -r gjf --dry-run Need Formatting: [gerrit-common/src/main/java/com/google/gerrit/common/IoUtil.java,\ gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java]
Fixes: google#105. Test Plan: $ git show --name-only HEAD | grep java$ | xargs -r gjf --dry-run Need Formatting: [gerrit-common/src/main/java/com/google/gerrit/common/IoUtil.java,\ gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java]
Fixes: google#105. Test Plan: $ git show --name-only HEAD | grep java$ | xargs -r gjf --dry-run Need Formatting: [gerrit-common/src/main/java/com/google/gerrit/common/IoUtil.java,\ gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java]
Fixes: google#105. Test Plan: $ git show --name-only HEAD | grep java$ | xargs -r gjf --dry-run Need Formatting: [gerrit-common/src/main/java/com/google/gerrit/common/IoUtil.java,\ gerrit-server/src/main/java/com/google/gerrit/server/account/Emails.java]
Are there advantages to adding this to the CLI over using something like the For batch processing multiple files a single invocation of |
The advantage for me is that I often don't have a) versioning system in place for a tree of sources and b) operate on a system where piping and diffing is not available "by default". A change in the formatting algorithm is a non-issue. I'm content to be locked-in to gjf w/o configuration support. And a project does not need to upgrade to the latest formatter version, if this is a problem. |
@cushon yes, this would be useful.
I believe a common setup is:
A validation mode would make implementing (1) significantly easier.
See (3). By reformatting the entire codebase when upgrading versions of Furthermore, the community desire for this feature is illustrated by the numerous open-source project which have added an unofficial validation mode, either by forking I would ask that you once again consider implementing this much-desired feature. Thanks! |
Not to mention that there's also https://github.com/diffplug/spotless which does the same thing as the other projects that @alexkleiman listed. |
...which BTW is still awaiting review to be mentioned in the README. 😉 |
There's some precedent: @sormuras how does this help with the lack of a versioning system? @alexkleiman are you interested in an addition to the API here, or just the CLI? The examples that use the API look fine: https://github.com/sherter/google-java-format-gradle-plugin/blob/3666fb45e5ff0b85b6fb2cd5b262c92f78dc60d8/src/main/java/com/github/sherter/googlejavaformatgradleplugin/FormatFileCallable.java#L44 |
Also, is there consensus that a dry run mode should exist with a non-zero status if unformatted files were found? That's a departure from |
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.
Mh, no versioning system, no diff support, no easy way to tell if files needs formatting. |
@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 |
Sorry, I was trying to say the opposite of that.
Is printing the paths of files that need to be formatted also sufficient for that? Neither |
@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 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 Thanks for your thoughtful consideration of this feature request! |
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:
|
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 |
Following |
Is this switch limited to the dry run mode? Or if set to |
If I understand https://github.com/dart-lang/dart_style/blob/master/lib/src/formatter_options.dart#L216 correctly, it always returns I'll update the PR according to this. |
That matches what I'm seeing, and seems reasonable. Do you have concerns about printing the names of files that need formatting? |
A small cleanup is being forced pushed in a minute... |
...pushed. |
After merging #106 this issue is finally closeable. 🌞 |
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.
The text was updated successfully, but these errors were encountered: