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

Fix code formatting with ktfmt (fixes #304) #330

Closed
wants to merge 3 commits into from

Conversation

rami3l
Copy link

@rami3l rami3l commented Feb 19, 2022

Following previous discussion in #303 (comment), this PR is an attempt to resolve #304:

Questions

  • Why do we need to vendor ktfmt for this project?
  • CI check should have been triggered on PR?

@rami3l rami3l changed the title Fix code formatting with ktfmt Fix code formatting with ktfmt (fixes #304) Feb 19, 2022
@fwcd fwcd added the formatting Related to source code formatting label Feb 23, 2022
@fwcd
Copy link
Owner

fwcd commented Mar 18, 2022

We're vendoring ktfmt (here) since it uses the embedded Kotlin compiler rather than the standard one, which we use. The embedded Kotlin compiler uses other package paths (e.g. org.jetbrains.kotlin.com.intellij instead of com.intellij), which causes weird clashes at runtime, for example:

java.lang.NoSuchMethodError: 'org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment$Companion.createForProduction(org.jetbrains.kotlin.com.intellij.openapi.Disposable, org.jetbrains.kotlin.config.CompilerConfiguration, org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles)'
            at com.facebook.ktfmt.format.Parser.parse(Parser.kt:45)
            at com.facebook.ktfmt.format.Formatter.sortedAndDistinctImports(Formatter.kt:141)
            at com.facebook.ktfmt.format.Formatter.format(Formatter.kt:84)
            at org.javacs.kt.formatting.FormatterKt.formatKotlinCode(Formatter.kt:10)
            at org.javacs.kt.KotlinTextDocumentService$formatting$1.invoke(KotlinTextDocumentService.kt:204)
            at org.javacs.kt.KotlinTextDocumentService$formatting$1.invoke(KotlinTextDocumentService.kt:199)
            at org.javacs.kt.util.AsyncExecutor.compute$lambda-2(AsyncExecutor.kt:19)
            at java.base/java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1700)
            at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
            at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
            at java.base/java.lang.Thread.run(Thread.java:829)

While this is non-ideal, it's currently the best solution I could find that works for our setup. A better solution would be to automate this as part of our Gradle build (i.e. rewrite the package paths and replace the transitive kotlin-compiler-embeddable dependency with kotlin-compiler - basically do this automatically), but I couldn't figure out a clean way of doing so yet.

@rami3l
Copy link
Author

rami3l commented Mar 18, 2022

@fwcd
Thanks a lot for your reply!

Just being curious: given the context in the official thread, what would possibly be the downside if we also switch to kotlin-compiler-embeddable? It seems that ktfmt has done exactly that in facebook/ktfmt#58.

I'm trying my best to get familiar with the current codebase, please inform me if I missed anything :)

@fwcd
Copy link
Owner

fwcd commented Mar 19, 2022

We tried this and it didn't work since there were other dependencies which needed the non-embedded compiler and didn't have an embedded version themselves (I think it was the kotlin-scripting-compiler or perhaps the IDEA plugin, which we no longer depend on though, can't remember). Might be worth trying again, but I haven't gotten around to doing so (though feel free to experiment).

@rami3l
Copy link
Author

rami3l commented Mar 20, 2022

@fwcd
I see. I would like to see what I can to to solve this dependency problem.

However, due to the fact that the incoherent formatting might cause frequent merge conflicts, can we first merge what has been changed for now (if you agree, of course), and we will continue our discussion in another issue (#338)?

Thanks a lot :)

@rami3l
Copy link
Author

rami3l commented Jul 11, 2022

@fwcd
Hi again! I would really like to help, but It seems to me that formatting the code base and changing the project config at the same time gets way too hairy.

So this got me thinking, maybe a better approach to get this merged is to first change all the configs without changing the actual formatting (probably need to disable the formatting check in gradle build first), and then somehow prompt the open PRs to use the config to format the code on their side...

I believe this could possibly reduce the number of conflicts for the authors of unmerged PRs at the moment. What do you think?


cc @kievitsp from #318 (comment)

@rami3l rami3l closed this Nov 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatting Related to source code formatting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a linter to the language server code itself
2 participants