Skip to content

Only last step in kotlin {} would apply #2117

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

Closed
6 tasks done
ziqiao-wang opened this issue May 23, 2024 · 8 comments
Closed
6 tasks done

Only last step in kotlin {} would apply #2117

ziqiao-wang opened this issue May 23, 2024 · 8 comments

Comments

@ziqiao-wang
Copy link

ziqiao-wang commented May 23, 2024

If you are submitting a bug, please include the following:

  • summary of problem: I use both ktfmt and ktlint to format my kotlin codes. I added both ktfmt and ktlint to kotlin block in gradle spotless configuration, it used to be applied in sequential order but seems now only the last step got applied.
  • Gradle or Maven version: 8.7
  • spotless version: 6.25.0
  • operating system and version: macOS 14.5
  • copy-paste your full Spotless configuration block(s), and a link to a public git repo that reproduces the problem if possible
        kotlin {
            toggleOffOn()
            target(ktTargets, ktsTargets)
    
            ktfmt(ktfmt_version).kotlinlangStyle()
    
            ktlint(libs.versions.ktlint.get())
                .setEditorConfigPath(file(".editorconfig"))
                .editorConfigOverride(
                    mapOf(
                        "ij_kotlin_allow_trailing_comma" to "true",
                        "ij_kotlin_allow_trailing_comma_on_call_site" to "true",
                        "ij_kotlin_packages_to_use_import_on_demand" to "*",
                        "ktlint_standard_property-naming" to "disabled",
                        "ktlint_standard_discouraged-comment-location" to "disabled",
                    ),
                )
        }
  • copy-paste the full content of any console errors emitted by gradlew spotless[Apply/Check] --stacktrace: N/A

If you're just submitting a feature request or question, no need for the above.

@nedtwigg
Copy link
Member

I don't think that's true. Both will run, it's just that ktlint runs last and undoes some of ktfmt. What's the bug?

@ziqiao-wang
Copy link
Author

I don't think that's true. Both will run, it's just that ktlint runs last and undoes some of ktfmt. What's the bug?

I have something like this

NettyChannelBuilder.forAddress(host, port)
        .usePlaintext()
        .apply {
        }
        .build()

with ktfmt(0.46) applied then ktlint(1.0.1) it will be formatted like above
however, with spotless 6.25.0
If I have unformatted code like below

NettyChannelBuilder.forAddress(host, port)
        .usePlaintext()
        .apply {
        } .build()

it will be formatted like

NettyChannelBuilder.forAddress(host, port)
        .usePlaintext()
        .apply {
        }.build()

I am currently hacking it with below to get ktfmt applied first then ktlint

    kotlinGradle {
        toggleOffOn()
        target(ktTargets, ktsTargets)

        ktfmt(libs.versions.ktfmt.asProvider().get()).kotlinlangStyle().configure {
            it.setMaxWidth(120)
            it.setRemoveUnusedImport(true)
        }
    }

    kotlin {
        toggleOffOn()
        target(ktTargets, ktsTargets)

        ktlint(libs.versions.ktlint.get())
            .setEditorConfigPath(file(".editorconfig"))
            .editorConfigOverride(
                mapOf(
                    "ij_kotlin_allow_trailing_comma" to "true",
                    "ij_kotlin_allow_trailing_comma_on_call_site" to "true",
                    "ij_kotlin_packages_to_use_import_on_demand" to "*",
                    "ktlint_standard_property-naming" to "disabled",
                    "ktlint_standard_discouraged-comment-location" to "disabled",
                ),
            )
    }
    
    
tasks.named("spotlessKotlin").configure { dependsOn("spotlessKotlinGradle") }

tasks.named("spotlessKotlinApply").configure { dependsOn("spotlessKotlinGradleApply") }

tasks.named("spotlessKotlinGradle").configure { finalizedBy("spotlessKotlin") }

tasks.named("spotlessKotlinGradleApply").configure { finalizedBy("spotlessKotlinApply") }

@nedtwigg
Copy link
Member

It looks you prefer the ktfmt style. Your workaround is a verbose way to put the ktfmt step after the ktlint step.

My advice is to remove the ktlint step entirely, you probably don't want both. But if you do really want both, the order matters.

@ziqiao-wang
Copy link
Author

It looks you prefer the ktfmt style. Your workaround is a verbose way to put the ktfmt step after the ktlint step.

My advice is to remove the ktlint step entirely, you probably don't want both. But if you do really want both, the order matters.

I actually put ktlint after ktfmt. I mostly want ktfmt but needs ktlint to redo some indentation. Now whatever order I put in kotlin {}, always last one got applied 😭. I verified this by reformatting the whole repo with kotlin { ktfmt() ktlint()} or kotlin { ktlint() ktfmt() } always seeing a bunch of diff(with codes formatted by my current workaround)

@nedtwigg
Copy link
Member

This is the expected behavior. The steps aren't doing "checks", they Function<String, String>. So the next one will wipe out parts of the previous one.

@ziqiao-wang
Copy link
Author

Member

If that's the case I would expect my workaround got same wiping, right? but it's not, so I think it's ktfmt not got applied

@nedtwigg
Copy link
Member

Aha! Your workaround is doing two things:

@ziqiao-wang
Copy link
Author

Aha! Your workaround is doing two things:

Gotcha, I guess it's related to #1599 then, cuz I am not actually swapping orders, it's still ktfmt => ktlint 😂 .

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

No branches or pull requests

2 participants