kotlin-language-server icon indicating copy to clipboard operation
kotlin-language-server copied to clipboard

wishlist: bazel support

Open rbtcollins opened this issue 8 months ago • 4 comments

The classpath script looks like it will work, but it has some caveats. In a monorepo there will be many distinct kotlin targets. IDEA seems to handle this ok, but its not clear how to make sure each target gets a different classpath with this LSP (and noting that we can't assume every target will have the access to all jars, nor even that they will all be of the same version (though that is obviously the common case)).

Figuring out what targets are running kotlin and what their inputs would be is fairly straightforward in bazel (query the action cache and parse the results, then build the input targets.

I'm not sure about timelines, but this is perhaps something we could put together, if there is interest in having the feature.

rbtcollins avatar Mar 15 '25 18:03 rbtcollins

We forked the LSP internally and added some support for Bazel and some other functionality (like Go-to/hover on source jars). It works with a companion VSCode extension (different from the official Kotlin one). The way it works is similar to the Bazel workflow in IDEA with the bazel plugin, you partially "sync" directories/bazel packages that you work on, and this triggers a bazel build with custom aspects we wrote which generates additional artifacts like all the classpath entries and metadata files (like paths to source jars and the symbols available), and the LSP then is notified of these artifacts and indexes them. Works ok with our setup so far with rules_kotlin and rules_jvm_external. There's still a single global classpath (but I think it's the same with the Bazel plugin?). We had to customize the LSP quite a bit though. It would great if we can port back that functionality back here (although the Kotlin extension would also need to support this bazel sync mode), and I'm happy to collaborate on this if folks are interested.

smocherla-brex avatar Mar 19 '25 19:03 smocherla-brex

That sounds pretty amazing. Perhaps you can publish what you have somewhere?

rbtcollins avatar Mar 20 '25 16:03 rbtcollins

I'm currently working on that, I'll share links here once I have it published to a public repo.

smocherla-brex avatar Mar 23 '25 16:03 smocherla-brex

I've published the extension I wrote along with the fork of the LSP with bazel support. I've added some documentation as well and you can download the VSIX from here to test. I've still not published it to the marketplace. As you can see, the LSP right now only works in tandem with that extension as I had to change things quite a bit to adapt it to Bazel. We could potentially move it into this repo/the Kotlin extension repo but that would require adding back Gradle/Maven support, which was challenging to do while keeping the Bazel support. Feel free to try it out and share any feedback, or open issues there.

smocherla-brex avatar Mar 25 '25 17:03 smocherla-brex