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

Lint Kotlin examples #3923

Merged
merged 40 commits into from
Nov 15, 2024
Merged

Lint Kotlin examples #3923

merged 40 commits into from
Nov 15, 2024

Conversation

myyk
Copy link
Contributor

@myyk myyk commented Nov 8, 2024

Last change I think needed for #3829

Overview:

  • Changes KtfmtModule to set the error code when there's changes like is true of the other formatters.
  • Some simplifications were made to the example/package.mill based on similarities with Java/Kotlin.
  • Files were formatted
  • CI was updated

test("testSimple") {
generateHtml("hello") shouldBe "<h1>hello</h1>"
}
class FooTest :
Copy link
Contributor

@0xnm 0xnm Nov 8, 2024

Choose a reason for hiding this comment

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

this doesn't look like a reasonable change, but I don't know ktfmt default rules. Overall, in the Kotlin ecosystem ktlint is much more popular than ktfmt.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is just the default ktfmt as there's no configuration. I'm guessing it's better to any formatter in place and we can let which the formatter battle to happen in parallel since it should then be a fairly easy switch.

Copy link
Contributor

Choose a reason for hiding this comment

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

Then why not use ktlint (as it is proposed in #3829) provided by KtlintModule? It will require some changes to the run in the module-independent manner, but it will be easy to do by looking on KtfmtModule.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, it is ktlint in the issue. I misread that. Forgive me, I am actually dyslexic.

Copy link
Member

Choose a reason for hiding this comment

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

No worries, let's try ktlint and see how it looks. Then we can pick the one that looks better.

I noticed KtlintModule does not have the companion object extends External Module that the other autoformatters do, making it more annoying to use ad-hoc. We should probably add such an companion external module. KtfmtModule, ScalafmtModule, and PalantirFormatModule can be used as a reference as to how it is done

@myyk myyk mentioned this pull request Nov 10, 2024
1 task
@myyk
Copy link
Contributor Author

myyk commented Nov 10, 2024

I think I need to adjust the linter tests bc I've changed the implementation. Or change the default to not see the status code.

@lihaoyi lihaoyi mentioned this pull request Nov 14, 2024
lihaoyi added a commit that referenced this pull request Nov 14, 2024
Rebootstraps on top of #3961
which we need for #3923
.editorconfig Outdated
Comment on lines 18 to 28
ktlint_standard_no-wildcard-imports = disabled
ktlint_standard_no-consecutive-blank-lines = disabled
ktlint_standard_final-newline = disabled
ktlint_standard_filename = disabled
ktlint_standard_property-naming = disabled
ktlint_standard_class-naming = disabled
ktlint_standard_no-consecutive-comments = disabled
ktlint_standard_import-ordering = disabled
ktlint_standard_function-naming = disabled
ktlint_standard_backing-property-naming = disabled
ktlint_standard_no-empty-file = disabled
Copy link
Contributor

Choose a reason for hiding this comment

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

what is the need for disabling so many defaults?

Also, to have a less of the change in this PR due to re-formatting you can change the default style intellij_idea (see https://pinterest.github.io/ktlint/latest/rules/configuration-ktlint/#code-style), because Intellij IDEA built-in formatter was used for the majority of Kotlin files in this repo.

@myyk
Copy link
Contributor Author

myyk commented Nov 14, 2024

There's failures in here because ktlint is formatting the files in kotlinlib/test/resources/contrib/ktfmt. We would like to skip formatting on those files and possibly other files such as all of the ones in out/.

ktlint says they can ignore files, but it seems to not be working for me: https://pinterest.github.io/ktlint/latest/faq/#how-do-i-disable-ktlint-for-generated-code

[out/**/*]
ktlint = disabled

@0xnm
Copy link
Contributor

0xnm commented Nov 14, 2024

It should be working, it was added quite a time ago in 1.0.0 and I suppose it is stable. I would suggest checking what is the value of the working dir when running ktlint. Disabling so many rules to solve this issue seems fragile and quite hacky approach.

@myyk
Copy link
Contributor Author

myyk commented Nov 14, 2024

@0xnm, yeah, I saw that too about it being added in 1.0.0. I just did a deep dive. It's because the .editorconfig file isn't being configured to be used... This is all very strange to me because it's settings seem to be being picked up even when it's not set to be used.

This is true while directly calling ktlint from the command line using java as well. I think it's defaulting to .editorconfig for formatting but not using it for disabling. It's weird. I'll make a PR to set that path.

Update: #3966 could fix this

@myyk
Copy link
Contributor Author

myyk commented Nov 14, 2024

I have 0 opinions on how Kotlin code should be formatted. I am happy for @0xnm and @lihaoyi to sort that out.

@lihaoyi
Copy link
Member

lihaoyi commented Nov 14, 2024

The reason i disabled all the rules was mostly to just focus on formatting, rather than other linters since thats what our other autoformatters do. I don't mind turning them back on if @0xnm prefers, it would just mean a bit more work to fix up the code to compliance

@lihaoyi
Copy link
Member

lihaoyi commented Nov 14, 2024

@myyk let's go with the intellij style that @0xnm suggested, and removing all the config overrides except one that we leave just to exercise the "ktlint picks up the config" code path. You'll need to resolve the various lint errors but it shouldnt be too hard

@lihaoyi
Copy link
Member

lihaoyi commented Nov 14, 2024

#3967 should pull in the latest changes to .editorconfig defaults and allow them to be used in Mill's own build

@myyk
Copy link
Contributor Author

myyk commented Nov 15, 2024

Looks like we need to revert the expected broken ktlint code and ignore it in .editorconfig like for ktfmt test examples.

@lihaoyi
Copy link
Member

lihaoyi commented Nov 15, 2024

example/kotlinlib/linting/2-ktlint is still failing. The problem appears to be that ktlint traverses the filesystem hierarchy, starting from its PWD of out/example/kotlinlib/linting/2-ktlint/local/test.dest/sandbox, all the up the ., and then uses that .editorconfig file to ignore the files inside.

@lihaoyi
Copy link
Member

lihaoyi commented Nov 15, 2024

For now I skipped the out/**/* lint seems that's the problematic line that blocks all linting from happening in the out folder, and is picked up even when running inside a subfolder.

@myyk
Copy link
Contributor Author

myyk commented Nov 15, 2024

Your finding is another interesting one. ktlint is full of a lot of surprises. I don't understand when it wants to follow the config and when it doesn't.

@lihaoyi
Copy link
Member

lihaoyi commented Nov 15, 2024

@myyk it appears we're not the only one to hit issues https://www.reddit.com/r/Kotlin/comments/1gdlc73/ktlint_alternatives/, but I guess it's the standard so that's not our problem to fix.

@myyk
Copy link
Contributor Author

myyk commented Nov 15, 2024

@lihaoyi yeah, I agree.

@lihaoyi lihaoyi merged commit 4cddacd into com-lihaoyi:main Nov 15, 2024
27 checks passed
@lihaoyi
Copy link
Member

lihaoyi commented Nov 15, 2024

Going to call this done and close out the bounty over PayNow

@lefou lefou added this to the 0.12.3 milestone Nov 17, 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.

4 participants