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

KAFKA-10787: Add import ordering checkstyle rule and configure an automatic formatter #16097

Merged
merged 29 commits into from
Jun 2, 2024

Conversation

gongxuanzhang
Copy link
Contributor

@gongxuanzhang gongxuanzhang commented May 28, 2024

add a import-order rule. It includes the following parts

  1. checkstyle import-order
  2. suppress all module
  3. in Intellj IDEA auto format coding configuration

checkstyle import-order

In order to uniform coding specifications in Kafka. This is the first step.
Referred to #10428 import order .
We specify the import order as "kafka, org.apache.kafka, com, net, org, java, javax and static imports."

suppress all module

If the detection function is enabled,A lot of code needs to be changed.
In order to pass CI and minimal modification. suppress all module checkstyle import-order.
We can open the module step by step.

in Intellj IDEA auto format coding configuration

If we want to uniform all code. The manual approach is cumbersome and error-prone.
And we need everyone to follow the rules in the future. So we need auto format coding tools.
In this PR I add a IDEA configuration and update gitignore file

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@gongxuanzhang could you please include the auto-formater in this PR? That can save our life :)

.gitignore Outdated
@@ -13,6 +13,7 @@ project/boot/
project/plugins/project/
patch-process/*
.idea
!/.idea/codeStyles
Copy link
Member

Choose a reason for hiding this comment

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

the whole idea folder is not in version control, so do we need this line?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the whole idea folder is not in version control, so do we need this line?

It has to be, and it's very important.

This file will allow you to import your idea automatically into the project's code format

Copy link
Member

Choose a reason for hiding this comment

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

It seems to me using build tool to address that is more acceptable. We all use gradle to build kafka but there are many IDE.

We can add IDE supports in the future but could you please keep this PR simple for now?

@gongxuanzhang
Copy link
Contributor Author

I revised this PR.It add spotless gradle plugin and mini configuration.
It contains two part.

  1. checkstyle import order rule. But suppress all module. you can change checkstyle/suppressions.xml open module check If you want
  2. auto format import order. Again, I did not open in any module. If you want open the auto format. Just add a module name into build.gradle #spotlessApplyModules like def spotlessApplyModules = ['core'] .then run ./gradlew spotlessApply you will get formated code module

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@gongxuanzhang nice patch. BTW, could you add docs to README for spotlessApply?

build.gradle Outdated Show resolved Hide resolved
build.gradle Outdated
@@ -1007,7 +1026,7 @@ project(':core') {
testImplementation libs.junitJupiter
testImplementation libs.slf4jlog4j
testImplementation libs.caffeine

Copy link
Member

Choose a reason for hiding this comment

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

please revert this change

@@ -361,4 +361,7 @@
<suppress checks="(ClassDataAbstractionCoupling|ClassFanOutComplexity)"
files="(ReplicaFetcherThreadBenchmark).java"/>

<!--import order -->
<suppress checks="ImportOrder" files="^*$"/>
Copy link
Member

Choose a reason for hiding this comment

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

why we need this change?

Copy link
Contributor Author

@gongxuanzhang gongxuanzhang May 31, 2024

Choose a reason for hiding this comment

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

I think checkstyle should consistent with auto format . If you open the A module auto format, we should open the module check rule.
ImportOrder rule can't custom in each module(It's going to take a lot of changes,maybe should add build.gradle every module).
So i add this line in order that open rule some module in future,This is what I think is a more convenient way to modify by module

Copy link
Member

Choose a reason for hiding this comment

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

Making the consistency is extra effort. Also, those changes will complicate checkstyle files. Hence, I prefer to remove them if we do NOT use it actually.

build.gradle Outdated
@@ -787,6 +800,12 @@ subprojects {
skipProjects = [ ":jmh-benchmarks", ":trogdor" ]
skipConfigurations = [ "zinc" ]
}

afterEvaluate {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we can set spotless directly. For example:

  if (project.name in spotlessApplyModules) {
    apply plugin: 'com.diffplug.spotless'
    spotless {
      java {
        importOrder('kafka', 'org.apache.kafka', 'com', 'net', 'org', 'java', 'javax', '', '\\#')
        removeUnusedImports()
      }
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I change the PR,please review it @chia7712

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@gongxuanzhang thanks for updated PR.

README.md Outdated
@@ -232,6 +232,12 @@ You can run checkstyle using:
The checkstyle warnings will be found in `reports/checkstyle/reports/main.html` and `reports/checkstyle/reports/test.html` files in the
subproject build directories. They are also printed to the console. The build will fail if Checkstyle fails.

#### Spotless ####
This plugin can review code by rules,it is disabled by default. You can enable it by setting `build.gradle` add a module name into the `spotlessApplyModules` variable like
`def spotlessApplyModules = ['core']`,then you can run spotless using:
Copy link
Member

Choose a reason for hiding this comment

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

spotlessApplyModules is a temporary limit, and we don't need to expose it.

@@ -28,6 +28,8 @@ scalaVersion=2.13.14
# Adding swaggerVersion in gradle.properties to have a single version in place for swagger
# New version of Swagger 2.2.14 requires minimum JDK 11.
swaggerVersion=2.2.8
# 6.14.0 and newer are need for java 11
spotlessVersion=6.13.0
Copy link
Member

Choose a reason for hiding this comment

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

spotlessVersion is used by only one-line, hence we can inline it.

@@ -361,4 +361,7 @@
<suppress checks="(ClassDataAbstractionCoupling|ClassFanOutComplexity)"
files="(ReplicaFetcherThreadBenchmark).java"/>

<!--import order -->
<suppress checks="ImportOrder" files="^*$"/>
Copy link
Member

Choose a reason for hiding this comment

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

Making the consistency is extra effort. Also, those changes will complicate checkstyle files. Hence, I prefer to remove them if we do NOT use it actually.

@gongxuanzhang
Copy link
Contributor Author

I update this PR,plz review it

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@gongxuanzhang thanks for updated PR

build.gradle Outdated
@@ -47,7 +47,8 @@ plugins {
// Updating the shadow plugin version to 8.1.1 causes issue with signing and publishing the shadowed
// artifacts - see https://github.com/johnrengelman/shadow/issues/901
id 'com.github.johnrengelman.shadow' version '8.1.0' apply false
id 'com.diffplug.spotless' version '6.14.0' apply false // 6.14.1 and newer require Java 11 at compile time, so we can't upgrade until AK 4.0
// 6.14.0 and newer are need for java 11
Copy link
Member

Choose a reason for hiding this comment

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

how about: the minimum required JRE of 6.14.0+ is 11

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -82,6 +82,10 @@
<property name="file" value="${config_loc}/${importControlFile}"/>
</module>

<!-- import order rule not config in the checkstyle.xml.
Copy link
Member

Choose a reason for hiding this comment

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

how about:

DON'T define any import order here! Import order check/format is addressed by spotless.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I update that @chia7712

README.md Outdated

The checkstyle warnings will be found in `reports/checkstyle/reports/main.html` and `reports/checkstyle/reports/test.html` files in the
subproject build directories. They are also printed to the console. The build will fail if Checkstyle fails.

#### Spotless ####
This plugin can review code by rules and can also help you check the code , it is disabled by default. Some of our code reviews use `spotless` instead of `checkstyle`:
Copy link
Member

@chia7712 chia7712 Jun 2, 2024

Choose a reason for hiding this comment

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

The import order is a part of static check. please call spotlessApply to optimize the imports of Java codes before filing pull request.

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

LGTM!

@chia7712 chia7712 merged commit 342e691 into apache:trunk Jun 2, 2024
1 check failed
wernerdv pushed a commit to wernerdv/kafka that referenced this pull request Jun 3, 2024
TaiJuWu pushed a commit to TaiJuWu/kafka that referenced this pull request Jun 8, 2024
gongxuanzhang added a commit to gongxuanzhang/kafka that referenced this pull request Jun 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants