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

Add custom ktlint rules to prevent Java interop issues #1414

Merged
merged 5 commits into from
Apr 18, 2024

Conversation

alancai98
Copy link
Member

@alancai98 alancai98 commented Apr 6, 2024

Relevant Issues

Description

Adds custom ktlint rules to report on Java interop issues mentioned in #1387:

  • top-level internal functions and values
  • top-level public functions and values without a @file:JvmName annotation

This PR integrates the checks into gradle and GH Actions. By default, the custom rules will not be run since there are quite a few usages of the above patterns throughout our code. These usages should be fixed in upcoming PRs. The following outlines how the custom rules can be run:

# No option -- runs the standard ktlint checks as before
❯ ./gradlew ktlintCheck

# `custom-ktlint-rules` parameter included -- runs the standard ktlint checks along with the custom ktlint rules
# Note: run on just the `main` sources; `test` sources are excluded
# Note: any detected issues won't fail-fast so that all detected issues can be reported
❯ ./gradlew ktlintCheck -Pcustom-ktlint-rules

# Run on a given subproject
❯ ./gradlew :partiql-ast:ktlintCheck -Pcustom-ktlint-rules

> Task :partiql-ast:ktlintMainSourceSetCheck
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToBinder.kt:21:17 Top-level public declaration found without `@file:JvmName` annotation: toBinder (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToBinder.kt:35:17 Top-level public declaration found without `@file:JvmName` annotation: toBinder (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/helpers/ToLegacyAst.kt:58:20 Top-level public declaration found without `@file:JvmName` annotation: toLegacyAst (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/normalize/Normalize.kt:22:22 Top-level public declaration found without `@file:JvmName` annotation: normalize (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:10:20 Top-level public declaration found without `@file:JvmName` annotation: sql (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:19:20 Top-level public declaration found without `@file:JvmName` annotation: sql (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:27:20 Top-level public declaration found without `@file:JvmName` annotation: sql (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:35:20 Top-level public declaration found without `@file:JvmName` annotation: sql (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:42:29 Top-level internal declaration found: concat (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:44:29 Top-level internal declaration found: concat (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:46:38 Top-level internal declaration found: plus (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:48:38 Top-level internal declaration found: plus (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:52:14 Top-level internal declaration found: NIL (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:54:14 Top-level internal declaration found: NL (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:56:14 Top-level internal declaration found: text (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:58:14 Top-level internal declaration found: link (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:60:14 Top-level internal declaration found: nest (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/Sql.kt:62:14 Top-level internal declaration found: list (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/ast/sql/SqlBlock.kt:10:21 Top-level public declaration found without `@file:JvmName` annotation: sql (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/lang/ast/meta.kt:63:20 Top-level public declaration found without `@file:JvmName` annotation: to (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/lang/ast/meta.kt:65:19 Top-level public declaration found without `@file:JvmName` annotation: find (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/lang/ast/meta.kt:67:19 Top-level public declaration found without `@file:JvmName` annotation: hasMeta (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/lang/ast/meta.kt:69:19 Top-level public declaration found without `@file:JvmName` annotation: add (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/lang/ast/SourceLocationMeta.kt:78:5 Top-level public declaration found without `@file:JvmName` annotation: UNKNOWN_SOURCE_LOCATION (cannot be auto-corrected)
./partiql-lang-kotlin/partiql-ast/src/main/kotlin/org/partiql/lang/ast/SourceLocationMeta.kt:80:19 Top-level public declaration found without `@file:JvmName` annotation: sourceLocation (cannot be auto-corrected)

Output of the GH Action step running the custom ktlint rules can be seen here: https://github.com/partiql/partiql-lang-kotlin/actions/runs/8639028556/job/23684585083?pr=1414.

Other Information

  • Updated Unreleased Section in CHANGELOG: [NO]

    • No public API changes.
  • Any backward-incompatible changes? [NO]

  • Any new external dependencies? [YES]

    • com.pinterest.ktlint:ktlint-core:0.42.1
    • com.pinterest.ktlint:ktlint-test:0.42.1
  • Do your changes comply with the Contributing Guidelines
    and Code Style Guidelines? [YES]

License Information

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@alancai98 alancai98 force-pushed the add-custom-ktlint-rules branch from 920f702 to f52f525 Compare April 6, 2024 00:35
Copy link

github-actions bot commented Apr 6, 2024

Conformance comparison report

Base (ab65143) 363d943 +/-
% Passing 92.54% 92.54% 0.00%
✅ Passing 5384 5384 0
❌ Failing 434 434 0
🔶 Ignored 0 0 0
Total Tests 5818 5818 0

Number passing in both: 5384

Number failing in both: 434

Number passing in Base (ab65143) but now fail: 0

Number failing in Base (ab65143) but now pass: 0

@alancai98 alancai98 force-pushed the add-custom-ktlint-rules branch from f52f525 to 15f6bfc Compare April 6, 2024 00:53
@codecov-commenter
Copy link

codecov-commenter commented Apr 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.16%. Comparing base (d059c81) to head (15f6bfc).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #1414   +/-   ##
=========================================
  Coverage     73.16%   73.16%           
  Complexity     2393     2393           
=========================================
  Files           247      247           
  Lines         17627    17627           
  Branches       3178     3178           
=========================================
  Hits          12896    12896           
  Misses         3854     3854           
  Partials        877      877           
Flag Coverage Δ
CLI 11.82% <ø> (ø)
EXAMPLES 80.07% <ø> (ø)
LANG 81.06% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@alancai98 alancai98 self-assigned this Apr 9, 2024
@alancai98 alancai98 force-pushed the add-custom-ktlint-rules branch from 4004c77 to 0336d9d Compare April 10, 2024 23:16
@alancai98 alancai98 changed the title Experiment w/ adding custom ktlint rule Add custom ktlint rules to prevent Java interop issues Apr 10, 2024
@alancai98 alancai98 marked this pull request as ready for review April 10, 2024 23:22
// Custom ktlint rules are added by adding to the `dependencies` block: https://github.com/JLLeitschuh/ktlint-gradle/tree/v10.2.0?tab=readme-ov-file#configuration
// Currently, we only run the rules when the `custom-ktlint-rules` property is set.
// Once we enable the custom rules to run by default, this conditional can be removed.
if (hasProperty("custom-ktlint-rules")) {
Copy link
Member Author

Choose a reason for hiding this comment

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

After the internal top-level and public top-level code issues are resolved, we can remove this property logic such that the custom ktlint rules will run by default.

# End state after existing code issues detected by custom ktlint rules are resolved
> ./gradlew ktlintCheck # will run standard and custom rules

Any detected issues in gradle and GH Actions will also fail the build.

const val pig = "0.6.2"
const val kotlinxCoroutines = "1.6.0"
const val kotlinxCoroutinesJdk8 = "1.6.0"
const val ktlint = "0.42.1" // we're on an old version of ktlint. TODO upgrade https://github.com/partiql/partiql-lang-kotlin/issues/1418
Copy link
Contributor

Choose a reason for hiding this comment

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

How did we go back from 11.6.0 to 0.42.1? Was the original one for the the gradle plugin, and this is for the custom rule dependencies?

Copy link
Member Author

Choose a reason for hiding this comment

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

There were a couple confusing things here previously.

The v0.42.1 refers to the default ktlint version that the 10.2.1 kotlin gradle plugin uses (see their source). I chose to use the same ktlint version for the custom rules that we were using for the default ktlintCheck/ktlintFormat runs. I didn't upgrade the default ktlint version from v0.42.1 to a newer one since there are some additional rules that our code is not compliant with. We can perform this ktlint version upgrade in a future PR (tracked in #1418).

Hope that makes things clearer.

const val pig = "0.6.2"
const val kotlinxCoroutines = "1.6.0"
const val kotlinxCoroutinesJdk8 = "1.6.0"
const val ktlint = "0.42.1" // we're on an old version of ktlint. TODO upgrade https://github.com/partiql/partiql-lang-kotlin/issues/1418
Copy link
Member Author

Choose a reason for hiding this comment

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

There were a couple confusing things here previously.

The v0.42.1 refers to the default ktlint version that the 10.2.1 kotlin gradle plugin uses (see their source). I chose to use the same ktlint version for the custom rules that we were using for the default ktlintCheck/ktlintFormat runs. I didn't upgrade the default ktlint version from v0.42.1 to a newer one since there are some additional rules that our code is not compliant with. We can perform this ktlint version upgrade in a future PR (tracked in #1418).

Hope that makes things clearer.

@@ -26,23 +26,23 @@ object Versions {
const val detekt = "1.20.0-RC1"
const val dokka = "1.6.10"
const val kotlin = "1.6.20"
const val ktlint = "10.2.1"
Copy link
Member Author

Choose a reason for hiding this comment

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

For clarity, we were actually using this version, 10.2.1 of the ktlint gradle plugin -- https://github.com/JLLeitschuh/ktlint-gradle/tree/v10.2.1.

@alancai98 alancai98 requested a review from rchowell April 17, 2024 18:28
@alancai98 alancai98 merged commit 4851cac into main Apr 18, 2024
10 checks passed
@alancai98 alancai98 deleted the add-custom-ktlint-rules branch April 18, 2024 00:37
alancai98 added a commit that referenced this pull request Apr 25, 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.

3 participants