bazelbuild / rules_kotlin

Bazel rules for Kotlin
Apache License 2.0
334 stars 210 forks source link

Kotlin stdlibs from toolchain can conflict with Maven deps #752

Open SanjayVas opened 2 years ago

SanjayVas commented 2 years ago

The Kotlin toolchain hard-codes stdlib dependencies from @com_github_jetbrains_kotlin. This means that if you're writing a library using rules_kotlin, anything downstream that depends on stdlibs via Maven can get two copies.

At minimum, the stdlib targets in @com_github_jetbrains_kotlin should have maven_coordinates tags so that libraries exported using rules_jvm_external's java_export/kt_jvm_export rules will list the appropriate Maven dependencies rather than containing their own copies. If these are also specified as override_targets to maven_install, then that should also cover downstream Bazel consumers.

What would be more flexible is additionally allowing stdlibs to be specified/overridden in define_kt_toolchain. If exposing the jvm_runtime/jvm_stdlibs attrs is too error-prone, perhaps have a specific label attr for each known stdlib.

Related to #41.

rockwotj commented 2 years ago

@SanjayVas one thing we do is set the -no-stdlib flag in the compiler options and we have a macro that we use and always explictly adds the stdlib from maven coordinates.

SanjayVas commented 1 year ago

@SanjayVas one thing we do is set the -no-stdlib flag in the compiler options and we have a macro that we use and always explictly adds the stdlib from maven coordinates.

I tried this with rules_kotlin 1.8.1. Unfortunately even with include_stdlibs = "none" I'm still seeing stdlib implicitly added. Here's a snippet from a -kt.jar-0.params file:

--kotlin_passthrough_flags
-no-stdlib
-Xsam-conversions=class
-Xlambdas=class
-opt-in=kotlin.RequiresOptIn
--direct_dependencies
external/com_github_jetbrains_kotlin/lib/annotations-13.0.jar
external/com_github_jetbrains_kotlin/lib/kotlin-stdlib.jar
external/com_github_jetbrains_kotlin/lib/kotlin-stdlib-jdk7.jar
external/com_github_jetbrains_kotlin/lib/kotlin-stdlib-jdk8.jar
Bencodes commented 1 year ago

include_stdlibs doesn't control what gets passed into the --direct_dependencies flag. That option just translates to a Kotlinc flag that the compiler internally handles.

https://github.com/bazelbuild/rules_kotlin/blob/dc98c15a7e26c4c02197b4a4ed2eb78122b5b712/src/main/starlark/core/options/opts.kotlinc.bzl#L55

SanjayVas commented 1 year ago

include_stdlibs doesn't control what gets passed into the --direct_dependencies flag. That option just translates to a Kotlinc flag that the compiler internally handles.

https://github.com/bazelbuild/rules_kotlin/blob/dc98c15a7e26c4c02197b4a4ed2eb78122b5b712/src/main/starlark/core/options/opts.kotlinc.bzl#L55

Does this basically mean that @rockwotj's suggestion wouldn't actually work? If so, what's the correct way to ensure that kt_jvm_library does not add the above stdlib dependencies on its own?

Bencodes commented 1 year ago

We do what rockwotj does and just provide our own version of the std libraries that we pull from rules_jvm_external as well as passing the -no-stdlib option.

Having the com_github_jetbrains_kotlin ones passed as a direct dependency might actually be a bug caused by the rules_kotlin builders/workers need it on their classpath https://github.com/bazelbuild/rules_kotlin/blob/8d79e6a520df5a43c195bf92a31e7539fbb2faae/src/main/kotlin/io/bazel/kotlin/builder/BUILD#L25-L26