Skip to content

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

Merged
merged 16 commits into from
Apr 12, 2025

Conversation

YongGoose
Copy link
Member

@YongGoose YongGoose commented Mar 15, 2025

🔹 Related Document

https://lists.apache.org/thread/b4mmng4t0tog6bd58y7y9vnr02jn0q9f

🔹 Key Changes:

  • Configured Spotless Maven Plugin to automatically format Java files using Palantir Java Format.
  • JDK version-based profile activation:
    • JDK 8 to <11 → Uses palantirJavaFormat.version = 1.1.0
    • JDK 11 to <21 → Uses palantirJavaFormat.version = 2.28.0
    • JDK 21+ → Uses palantirJavaFormat.version = 2.39.0
  • Excluded specific directories/files from formatting:
    • script/, generated/, antlr/mysql/parser/, antlr/mysql/antlr/, etc.
  • Integrated with Maven’s process-sources phase, ensuring formatting is applied during builds.
  • Enabled unused import removal, trailing whitespace trimming, and newline enforcement at EOF.

🔹 How to Verify:

1️⃣ Run Maven build to ensure Spotless is applied automatically:

mvn clean install

2️⃣ Manually apply formatting with:

mvn spotless:apply

3️⃣ Verify excluded directories remain unchanged.

🔹 Screen shots

image
image

ps. I found that applying Spotless resulted in 1,914 files being modified!!
image

@YongGoose
Copy link
Member Author

@funky-eyes @xingfudeshi

What do you think? 🤔

@slievrly
Copy link
Member

@YongGoose Can Spotless only work on the change files instead of all files?

Copy link

codecov bot commented Mar 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 54.24%. Comparing base (6083fa1) to head (0b3d1e4).
Report is 6 commits behind head on 2.x.

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           

see 3 files with indirect coverage changes

Impacted file tree graph

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@YongGoose
Copy link
Member Author

Can Spotless only work on the change files instead of all files?

No, Spotless can be applied to all files as well.

@xingfudeshi xingfudeshi self-requested a review March 17, 2025 03:36
@funky-eyes
Copy link
Contributor

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.

@YongGoose
Copy link
Member Author

YongGoose commented Mar 19, 2025

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!

Although there is the drawback that spotless won’t be applied to unmodified files, after weighing the trade-offs with the git history, we determined that it’s better to only modify the changed files.

@funky-eyes
Copy link
Contributor

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!

Although there is the drawback that spotless won’t be applied to unmodified files, after weighing the trade-offs with the git history, we determined that it’s better to only modify the changed 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?

@YongGoose
Copy link
Member Author

YongGoose commented Mar 20, 2025

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 git diff feature with ratchet.
I haven’t actually implemented it yet, but it does seem feasible.

@funky-eyes
Copy link
Contributor

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 git diff feature with pre-commit hook. I haven’t actually implemented it yet, but it does seem feasible.

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.

@YongGoose YongGoose marked this pull request as ready for review March 20, 2025 09:49
<exclude>**/antlr/mysql/antlr/*.*</exclude>
<exclude>**/antlr/mysql/stream/ANTLRNoCaseStringStream.java</exclude>
</excludes>
<ratchetFrom>HEAD</ratchetFrom>
Copy link
Member Author

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).

Copy link
Contributor

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

Copy link
Member Author

Choose a reason for hiding this comment

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

@funky-eyes

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?

Copy link
Member

Choose a reason for hiding this comment

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

@funky-eyes

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?

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
image
4. mvn spotless:check
5. Then I got the outputs like this
image

Copy link
Contributor

Choose a reason for hiding this comment

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

@funky-eyes

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?

I think it can be done this way

Copy link
Contributor

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?

Copy link
Member Author

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 to 2.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.
image

Copy link
Member Author

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.

Copy link
Member Author

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]

Copy link
Contributor

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?

@funky-eyes funky-eyes added this to the 2.4.0 milestone Mar 27, 2025
@funky-eyes funky-eyes added the type: feature Category issues or prs related to feature request. label Mar 27, 2025
@xingfudeshi
Copy link
Member

xingfudeshi commented Mar 27, 2025

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 git diff feature with pre-commit hook. I haven’t actually implemented it yet, but it does seem feasible.

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.

I agree with your point .

@YongGoose YongGoose force-pushed the feature/apply-spotless branch from ad8bdcd to 749319e Compare March 31, 2025 06:45
@YongGoose YongGoose force-pushed the feature/apply-spotless branch from 749319e to 3bee817 Compare March 31, 2025 06:53
@YongGoose YongGoose force-pushed the feature/apply-spotless branch from ba28448 to a31ddf8 Compare March 31, 2025 06:57
</configuration>
<executions>
<execution>
<goals>
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

ok.

@YongGoose YongGoose requested a review from slievrly April 10, 2025 07:56
@slievrly slievrly changed the title feature: Apply Spotless with Palantir Java Format optimize: Apply Spotless with Palantir Java Format Apr 12, 2025
Copy link
Member

@slievrly slievrly left a comment

Choose a reason for hiding this comment

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

LGTM

@slievrly slievrly merged commit 70b0ade into apache:2.x Apr 12, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Category issues or prs related to feature request.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants