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

Detekt: Add auto-correct functionality for OrtImportOrder #9768

Merged
merged 2 commits into from
Jan 17, 2025

Conversation

MarcelBochtler
Copy link
Member

Add the functionality to use Detekt's auto-correct functionality 1 to
fix an incorrect import order or missing empty lines between top level
domains.
To use this, run the detekt Gradle task with the --auto-correct
flag:

./gradlew detekt --auto-correct

auto-correct can also be useful as a Git pre-commit hook using the
detekt-cli 2:

detekt --plugins /path/to/detekt-rules.jar -c .detekt.yml -i \
  $(git diff --name-only \
    | grep -E '\.kt$|\.kts$' \
    | tr ',\n' ',' \
    | sed '$s/,$/\n/')

@MarcelBochtler MarcelBochtler marked this pull request as ready for review January 16, 2025 20:04
@MarcelBochtler MarcelBochtler requested a review from a team as a code owner January 16, 2025 20:04
Copy link

codecov bot commented Jan 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 68.07%. Comparing base (c504d62) to head (48c361c).
Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##               main    #9768   +/-   ##
=========================================
  Coverage     68.07%   68.07%           
+ Complexity     1285     1284    -1     
=========================================
  Files           249      249           
  Lines          8827     8827           
  Branches        918      918           
=========================================
  Hits           6009     6009           
  Misses         2432     2432           
  Partials        386      386           
Flag Coverage Δ
funTest-docker 65.00% <ø> (ø)
funTest-non-docker 33.36% <ø> (ø)
test-ubuntu-24.04 35.89% <ø> (ø)
test-windows-2022 35.87% <ø> (ø)

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.

Copy link
Member

@mnonnenmacher mnonnenmacher left a comment

Choose a reason for hiding this comment

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

@MarcelBochtler MarcelBochtler force-pushed the import-order-auto-correct branch from 7d5a4b1 to 8a08215 Compare January 16, 2025 21:08
@nnobelis
Copy link
Member

@MarcelBochtler This is really great.
It may look trivial, but I think the ORT import styling is a pain point for first time contributors.

Does the auto-correct also remove unused imports ?

@MarcelBochtler
Copy link
Member Author

MarcelBochtler commented Jan 17, 2025

Does the auto-correct also remove unused imports ?

We use Detekt's UnusedImports rule, which does not support auto-correcting.
Only ktlint formatting rules allow this (or custom rules, like the `OrtImportOrder).

We would therefore need to switch to the NoUnusedImports rule from Ktlint.
Here is the reasoning why we're not already using ktlint rules.

ort/.detekt.yml

Lines 23 to 24 in 7f8d3d3

# Formatting rules are implemented via the ktlint plugin. As ktlint does not allow exceptions, we need to disable
# respective rules completely.

But I did not research if this reasoning is still valid.

@@ -68,7 +68,7 @@ class OrtImportOrder(config: Config) : Rule(config) {
if (importPaths != expectedImportOrder) {
val path = importList.containingKtFile.absolutePath()
val message = "Imports in file '$path' are not sorted alphabetically or single blank lines are missing " +
"between different top-level packages"
"between different top-level packages."
Copy link
Member

Choose a reason for hiding this comment

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

FYI, while I'm a known fan of properly ending sentences with dots, in this case the dot was deliberately omitted to align with built-in detekt rules, which suffix the message with the name of the rule in square brackets, e.g. Needless blank line(s) [NoConsecutiveBlankLines].

So I'd have a slight preference for dropping this commit. But I'm also ok to keep 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.

Dropped the commit, I agree that this message should be in line with other detekt messages.

@sschuberth
Copy link
Member

Could you also update https://github.com/oss-review-toolkit/ort/blob/main/website/docs/development.md#detekt?

I believe this comment is still open, @MarcelBochtler. An oversight?

Add the functionality to use Detekt's auto-correct functionality [1] to
fix an incorrect import order or missing empty lines between top level
domains.
To use this, run the `detekt` Gradle task with the `--auto-correct`
flag:
```
./gradlew detekt --auto-correct
```

[1]: https://detekt.dev/docs/introduction/extensions/#autocorrect-property

Signed-off-by: Marcel Bochtler <[email protected]>
@MarcelBochtler MarcelBochtler force-pushed the import-order-auto-correct branch from 8a08215 to 48c361c Compare January 17, 2025 15:46
@MarcelBochtler MarcelBochtler requested a review from a team as a code owner January 17, 2025 15:46
@MarcelBochtler
Copy link
Member Author

Could you also update https://github.com/oss-review-toolkit/ort/blob/main/website/docs/development.md#detekt?

I believe this comment is still open, @MarcelBochtler. An oversight?

I was aware of it ;)
Added the documentation now.

I also dropped the information about a pre-commit hook from the commit message.
I don't think that the --auto-correct feature should be used as a pre-commit hook, because the developer should have a look if the changed code is correct, before commiting it.

@MarcelBochtler MarcelBochtler merged commit 78edc76 into main Jan 17, 2025
26 checks passed
@MarcelBochtler MarcelBochtler deleted the import-order-auto-correct branch January 17, 2025 17:05
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.

4 participants