Skip to content
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

[Feature][Style] Enable spotless to manage imports #11458

Merged
merged 3 commits into from
Aug 13, 2022

Conversation

EricGao888
Copy link
Member

@EricGao888 EricGao888 commented Aug 12, 2022

Purpose of the pull request

Brief change log

  1. Configure Spotless to remove unused imports.
  2. Configure Spotless to sort imports in better order (instead of simply sorting import alphabetically).
  3. Configure Spotless to remove wildcards imports.
  4. Configure Spotless to remove empty line indents in pom.xml.
  5. Reformat ZeppelinTask.java as an example.

Verify this pull request

  • Verified by manual tests.

@EricGao888 EricGao888 self-assigned this Aug 12, 2022
@EricGao888 EricGao888 marked this pull request as ready for review August 12, 2022 14:13
0=java
1=javax
2=org
3=com
Copy link
Member Author

Choose a reason for hiding this comment

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

FYI, we could not have an empty line at the end of this file because it would cause Spotless to fail to parse the imports management rules.

Copy link
Member

Choose a reason for hiding this comment

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

If we hope to manage the import order, it's better to use the origin order

org.apache.dolphinscheduler,org.apache,java,javax,org,com

otherwise, the default order is still ok.

Copy link
Member Author

Choose a reason for hiding this comment

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

If we hope to manage the import order, it's better to use the origin order

org.apache.dolphinscheduler,org.apache,java,javax,org,com

otherwise, the default order is still ok.

Sure, I will give a try. If not work, will recover it.

Copy link
Member Author

Choose a reason for hiding this comment

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

@ruanwenjun Updated. It works well.

@ruanwenjun ruanwenjun added this to the 3.0.1 milestone Aug 12, 2022
@sonarcloud
Copy link

sonarcloud bot commented Aug 12, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

Copy link
Member

@ruanwenjun ruanwenjun left a comment

Choose a reason for hiding this comment

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

LGTM

@EricGao888 EricGao888 merged commit 896fef6 into apache:dev Aug 13, 2022
<importOrder>
<file>style/eclipse.importorder</file>
</importOrder>
<replaceRegex>
Copy link
Member

Choose a reason for hiding this comment

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

This removes the star import. But will it instead replaces the star import with the used specific class import? If not, this will cause compiling failed. Can you change a class with this example?

Copy link
Member Author

Choose a reason for hiding this comment

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

@kezhenxu94 diffplug/spotless#649 (comment) It is hardly possible for Spotless to replace with wildcards with specific imports, thus this is some kind of workaround actually. To prevent developers from pushing the commits without knowing that wildcards has been removed by their pre-commit hook, one possible solution is to add a message in pre-commit hook to inform them of this behavior. WDYT?

Copy link
Member Author

@EricGao888 EricGao888 Aug 13, 2022

Choose a reason for hiding this comment

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

@kezhenxu94 FYI, I also added a comment in the origin issue to seek help to see if there is some good practice to handle wildcard imports with Spotless: diffplug/spotless#649 (comment)

Copy link
Member

Choose a reason for hiding this comment

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

@kezhenxu94 FYI, I also added a comment in the origin issue to seek help to see if there is some good practice to handle wildcard imports with Spotless: diffplug/spotless#649 (comment)

Wow. The issue has been 2 years...

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 removes the star import. But will it instead replaces the star import with the used specific class import? If not, this will cause compiling failed. Can you change a class with this example?

@kezhenxu94 Just a follow-up, as I've tested it locally, if we use mvn spotless:check instead of mvn spotless:apply, it only blocks the wildcard imports without removing them. To improve, we could change the pre-commit hook from mvn spotless:apply to mvn spotless:check and add an output message to inform contributors that they should remove wildcard imports. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

This removes the star import. But will it instead replaces the star import with the used specific class import? If not, this will cause compiling failed. Can you change a class with this example?

@kezhenxu94 Just a follow-up, as I've tested it locally, if we use mvn spotless:check instead of mvn spotless:apply, it only blocks the wildcard imports without removing them. To improve, we could change the pre-commit hook from mvn spotless:apply to mvn spotless:check and add an output message to inform contributors that they should remove wildcard imports. WDYT?

Can you identify the failure of spotless:check is because of wildcard imports or other code style issues?

Copy link
Member Author

Choose a reason for hiding this comment

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

@kezhenxu94 Yes, I just double-checked it. To reproduce, you might change

to import java.util.*; and run mvn spotless:check.

Copy link
Member

Choose a reason for hiding this comment

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

@kezhenxu94 Yes, I just double-checked it. To reproduce, you might change

to import java.util.*; and run mvn spotless:check.

How can yo tell the spotless:check failure is because of wildcard import, if I have other code style issues and no wildcard import?

Copy link
Member Author

Choose a reason for hiding this comment

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

@kezhenxu94 Yes, I just double-checked it. To reproduce, you might change

to import java.util.*; and run mvn spotless:check.

How can yo tell the spotless:check failure is because of wildcard import, if I have other code style issues and no wildcard import?

Good question, since each rule has its name, I think there might be a way to configure Spotless to explicitly tell which rule the code fails. I will take a look and see if there is a way to do so.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature][Style] Enable spotless to manage imports
4 participants