-
Notifications
You must be signed in to change notification settings - Fork 8.8k
optimize: Apply Spotless with Palantir Java Format #7223
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
Conversation
What do you think? 🤔 |
@YongGoose Can Spotless only work on the change files instead of all files? |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## 2.x #7223 +/- ##
=========================================
Coverage 54.23% 54.24%
- Complexity 7267 7269 +2
=========================================
Files 1178 1178
Lines 41952 41952
Branches 4923 4923
=========================================
+ Hits 22753 22756 +3
+ Misses 17048 17045 -3
Partials 2151 2151 🚀 New features to boost your workflow:
|
No, Spotless can be applied to all files as well. |
I think this approach is very effective. Although it involves modifying many files, I believe as long as the CI passes, there won't be any significant issues. Even though it may conflict with other PRs, I think this cost is worthwhile. |
Thank you for your comment! However, after discussing with @slievrly on the mailing lists, we decided to apply spotless only to the modified files, so it looks like we won’t be modifying many files!
|
It would be great if this can be achieved, but when I run the mvn spotless:apply command locally, how can I ensure that only the files I have modified are formatted? |
I believe it can be done by using the |
Ok, I hope this solution is feasible. If it's not, I don't mind modifying all the files; I don't think it will have too much of an impact. |
<exclude>**/antlr/mysql/antlr/*.*</exclude> | ||
<exclude>**/antlr/mysql/stream/ANTLRNoCaseStringStream.java</exclude> | ||
</excludes> | ||
<ratchetFrom>HEAD</ratchetFrom> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slievrly @funky-eyes @xingfudeshi
I configured <ratchetFrom>HEAD</ratchetFrom>
so that only the modified files are formatted by comparing them with the currently checked-out commit (HEAD).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@slievrly @funky-eyes @xingfudeshi
I configured
<ratchetFrom>HEAD</ratchetFrom>
so that only the modified files are formatted by comparing them with the currently checked-out commit (HEAD).
Good job! I will pull this PR to my local machine for related testing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, our baseline is set to HEAD
, which means that only files changed since the last commit are considered. This creates a potential issue: if code with incorrect formatting gets committed, checkstyle
will catch it, but Spotless
won’t apply its fixes, and we lose the intended benefit.
To address this, we tried switching the baseline from HEAD to origin/2.x
. However, the branch wasn’t recognized. 🥹
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, our baseline is set to
HEAD
, which means that only files changed since the last commit are considered. This creates a potential issue: if code with incorrect formatting gets committed,checkstyle
will catch it, butSpotless
won’t apply its fixes, and we lose the intended benefit.To address this, we tried switching the baseline from HEAD to
origin/2.x
. However, the branch wasn’t recognized. 🥹What do you think?
Hi, I pulled your code and followed the steps. Seems to work!
1. git checkout feature/apply-spotless
2. Change the value of ratchetFrom from HEAD to feature/apply-spotless
3. Add some spaces in the method of Java class, like this

4. mvn spotless:check
5. Then I got the outputs like this

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, our baseline is set to
HEAD
, which means that only files changed since the last commit are considered. This creates a potential issue: if code with incorrect formatting gets committed,checkstyle
will catch it, butSpotless
won’t apply its fixes, and we lose the intended benefit.To address this, we tried switching the baseline from HEAD to
origin/2.x
. However, the branch wasn’t recognized. 🥹What do you think?
I think it can be done this way
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In other words, unless we format and commit all the code at once, we will not be able to prompt the user to run mvn spotless:apply
. What if we change the HEAD
to 2.x
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we change the
HEAD
to2.x
?
I think this approach is a good one.
However, when I tried running it locally, the behavior was different from what I expected, so I'm reconsidering it.
To explain in more detail, switching to origin/2.x
should apply changes only to the files that have been modified from 2.x
.
Even though there were no changes in my local branch, some recently committed files in 2.x
were also modified.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Still, I think setting it to 2.x
is a better approach than setting it to HEAD
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this commit 4f07580 i changed ratchet from to origin/2.x
Then below error occured.
Error: Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.44.3:apply (default) on project seata-parent: Execution default of goal com.diffplug.spotless:spotless-maven-plugin:2.44.3:apply failed: No such reference 'origin/2.x' -> [Help 1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that after setting it to 2.x, the number of changed files is quite large, unless we are willing to accept changes to all files. Otherwise, I think we can initially set it to HEAD, merge this PR, and then create an issue to discuss the remaining problems.
@slievrly What do you think?
I agree with your point . |
ad8bdcd
to
749319e
Compare
749319e
to
3bee817
Compare
ba28448
to
a31ddf8
Compare
</configuration> | ||
<executions> | ||
<execution> | ||
<goals> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How it works in github action workflow, I didn't see this profile id included in workflow.In addition to checking if the format is as expected, is it possible to automatically format changes in the workflow and commit them to PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't work in GitHub Actions.
Automatically formatting and committing changes seems more suitable for a git hook
, which would require users to modify their local .git
folder.
For now, in this PR, I’ve applied Spotless
.
I’m planning to explore additional improvements in a separate PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
🔹 Related Document
https://lists.apache.org/thread/b4mmng4t0tog6bd58y7y9vnr02jn0q9f
🔹 Key Changes:
palantirJavaFormat.version = 1.1.0
palantirJavaFormat.version = 2.28.0
palantirJavaFormat.version = 2.39.0
script/
,generated/
,antlr/mysql/parser/
,antlr/mysql/antlr/
, etc.process-sources
phase, ensuring formatting is applied during builds.🔹 How to Verify:
1️⃣ Run Maven build to ensure Spotless is applied automatically:
2️⃣ Manually apply formatting with:
3️⃣ Verify excluded directories remain unchanged.
🔹 Screen shots
ps. I found that applying Spotless resulted in 1,914 files being modified!!
