pubref / rules_kotlin

Bazel rules for Kotlin
Other
159 stars 20 forks source link

java_deps attribute isn't pulling in jars exported by java_library? #11

Closed guyshefner closed 7 years ago

guyshefner commented 7 years ago

Hi, thanks for adding the java_deps attribute! It's working for the straightforward case of a java_library built from java sources, but doesn't seem to be working for the case of a java_library which exports other jars.

Here are steps to reproduce using examples/helloworld:

  1. Add the following to the WORKSPACE file:
    maven_jar(
    name = "com_google_guava_guava_21_0",
    artifact = "com.google.guava:guava:jar:21.0",
    )
  2. Add the following to examples/helloworld/BUILD:
    java_library(
    name = "guava",
    exports = [
        "@com_google_guava_guava_21_0//jar",
    ],
    )
  3. In the same BUILD file, just to show it working for java rules, add ":guava" to the deps attribute of the java_binary named "main_java", like so:
    java_binary(
    name = "main_java",
    main_class = "examples.helloworld.Main",
    srcs = ["Main.java"],
    deps = [":rules_kt", ":guava"],
    )
  4. Also to show it working for java, add the following import line to Main.java:
    import com.google.common.base.Joiner;  // This compiles.
  5. Then, in the BUILD file, add ":guava" to the java_deps attribute of the kotlin_binary named "main_kt", like so:
    kotlin_binary(
    name = "main_kt",
    main_class = "examples.helloworld.MainKt",
    srcs = ["main.kt"],
    deps = [":rules"],
    java_deps = [":milk", ":guava"]
    )
  6. Up to this point, observe the following command works:
    bazel build //examples/helloworld:all
  7. However, this step causes a failure to build: Add the following import line to main.kt:
    // This import fails to resolve.
    import com.google.common.base.Joiner

The failure is:

rules_kotlin/examples/helloworld/main.kt:3:12: error: unresolved reference: google
import com.google.common.base.Joiner
           ^

The same problem happens with the kotlin_library rule. I'm using bazel 0.4.5, and rules_kotlin cloned at 522a1e700c0d05610f9dda6c4a40e3ecb4413f68.

pcj commented 7 years ago

Thanks @guyshefner, looking into it. Here's the workaround. First note that the generated BUILD file for any maven_jar actually contains two rules:

$ less ./bazel-rules_kotlin/external/com_google_guava_guava_21_0/jar/BUILD.bazel
# DO NOT EDIT: automatically generated BUILD.bazel file for maven_jar rule com_google_guava_guava_21_0
java_import(
    name = 'jar',
    jars = ['guava-21.0.jar'],
    visibility = ['//visibility:public']
)

filegroup(
    name = 'file',
    srcs = ['guava-21.0.jar'],
    visibility = ['//visibility:public']
)

Apologies if you knew that already, took me a long time until I realized it. Thus, you can always get at the jar file using the filegroup target rule. Therefore, this can be fed into the jars attribute of a kotlin_binary rule. So, the following compiles:

kotlin_binary(
    name = "main_kt",
    main_class = "examples.helloworld.MainKt",
    srcs = ["main.kt"],
    deps = [":rules"],
    java_deps = [":milk", ":guava"],
    jars = [
        "@com_google_guava_guava_21_0//jar:file",
    ],
)

Still awkward however as it requires the end-user of rules_kotlin to have to differentiate this.

pcj commented 7 years ago

Pushed a new version that implements the java_deps attribute more robustly than the previous hackery, tagged as v0.2.2.

https://github.com/pubref/rules_kotlin/commit/8de6f191de3a59f22886237150bcf7b9dd6c04f1