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

Compose rules not working in ktlint-intellij-plugin (follow-up on #176) #190

Closed
paul-dingemans opened this issue Jan 23, 2024 · 2 comments · Fixed by #195
Closed

Compose rules not working in ktlint-intellij-plugin (follow-up on #176) #190

paul-dingemans opened this issue Jan 23, 2024 · 2 comments · Fixed by #195

Comments

@paul-dingemans
Copy link

paul-dingemans commented Jan 23, 2024

As reported in #176 (Originally posted by @chrimaeon in #176 (comment)), the fix for running the compose rules with the ktlint-intellij-plugin only partially works. Given code sample below:

import androidx.compose.material3.Text
import androidx.compose.runtime.Composable

@Composable
fun Post() {
    Text("foo")
}

does not show violation below which is shown by Ktlint CLI"

Post.kt:11:5: This @Composable function emits content but doesn't have a modifier parameter.

See https://mrmans0n.github.io/compose-rules/rules/#when-should-i-expose-modifier-parameters for more information. (compose:modifier-missing-check)

Summary error count (descending) by rule:
  compose:modifier-missing-check: 1

Process finished with exit code 1

with ktlint-compose 0.3.9 and intellij-ktlint-plugin 0.20.0

After investigation in ktlint and ktlint-intellij-plugin, I have found the cause of the problem. Like in #176 the cause is the embedded kotlin compiler which conflicts with packages provided by Intellij IDEA itself. In the ktlint-intellij-plugin those problems are resolved with relocating packages.

I have tried to relocate the packages in ktlint to prevent that third party supplier of rules are not affected by this but it didn't work out as expected. For now, I see no other solution than that each third party supplier of rules applies relocation of packages similar as is done in ktlint-intellij-plugin. For compose rules that would mean that configuration below has to be added to build file of ktlint-rules:

shadowJar {
    // Relocate packages that may conflict with ktlint-intellij-plugin. The rule set won't work when relocations below
    // are not kept in sync with https://github.com/nbadal/ktlint-intellij-plugin/blob/main/lib/build.gradle.kts
    relocate("org.jetbrains.concurrency", "shadow.org.jetbrains.concurrency")
    relocate("org.jetbrains.kotlin.psi.KtPsiFactory", "shadow.org.jetbrains.kotlin.psi.KtPsiFactory")
    relocate("org.jetbrains.kotlin.psi.psiUtil", "shadow.org.jetbrains.kotlin.psi.psiUtil")
    relocate("org.jetbrains.org", "shadow.org.jetbrains.org")
}

On my local machine, I have build the compose rules for ktlint with changes above. Both ktlint CLI and ktlint-intelli-plugin now report the violation as requested by OP.

Screenshot 2024-01-23 at 12 00 32

Screenshot 2024-01-23 at 12 00 47

@mrmans0n
Copy link
Owner

Thanks (as always) for the thorough debugging and solution. Will get to it as soon as I can 😁

@paul-dingemans
Copy link
Author

Ah cool. Thanks for fixing.

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 a pull request may close this issue.

2 participants