-
Notifications
You must be signed in to change notification settings - Fork 213
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
WIP: Coverage in Kotlin #429
Conversation
so, coverage works for me on this branch, at least in the environment I'm using (JUnit 5, Java 11 toolchain). however, coverage does not show for targets that touch Java plugins. in my case, whether the targets are Kotlin or Java, if there is a plugin involved with building them, there is no coverage. otherwise, kotlin sources show up in coverage report as expected. the issue w/plugins is likely blocked on the discussion/fix in bazelbuild/bazel#5426. |
@sgammon Thank you for resuscitating this work! |
@hchauvin it is a poor imitation of your original PR but it works!! lol. also it will need tests, which i will copy in soon once i isolate this plugin issue. |
Builds on previous work (bazelbuild#52) to add coverage support to Kotlin targets in Bazel. So far, it's working (in basic form), but seems to omit major swaths of our code when I test it on a live codebase. I'm not sure why that happens yet.
eb36ec3
to
d8183e1
Compare
i have discovered a few issues with this work, which should block it before merge:
any ideas, @hsyed / @cgruber? first issue above feels like some hard-coded path. second seems like a subtle bug which was discussed in bazelbuild/bazel#5426 but may end up being Kotlin-specific. the repro can be run with tested against bazel |
Hi @sgammon, thank you for your effort! I am really looking for it! When I try using your branch to test in my project with coverage, it fails with error:
I checked what the command So my classpath doesn't contain Are there configurations needed to make it work? |
I found that I have to use the following patch to add diff --git a/kotlin/internal/jvm/impl.bzl b/kotlin/internal/jvm/impl.bzl
index 9771bbf..5ed52fa 100644
--- a/kotlin/internal/jvm/impl.bzl
+++ b/kotlin/internal/jvm/impl.bzl
@@ -235,8 +235,13 @@ _SPLIT_STRINGS = [
]
def kt_jvm_junit_test_impl(ctx):
+ if ctx.configuration.coverage_enabled:
+ runner = ctx.files._bazel_test_runner + ctx.files._jacocorunner
+ else:
+ runner = ctx.files._bazel_test_runner
+
providers = _kt_jvm_produce_jar_actions(ctx, "kt_jvm_test")
- runtime_jars = depset(ctx.files._bazel_test_runner, transitive = [providers.java.transitive_runtime_jars])
+ runtime_jars = depset(runner, transitive = [providers.java.transitive_runtime_jars])
test_class = ctx.attr.test_class |
context.execute("instrument class files") { | ||
jacocoProcessor.instrument(this) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There may have an issue with the inline function across modules.
If the compiled jar has only instrumented byte code, calling the inline function when running the test will result in an error like
java.lang.IllegalAccessError: class com.package.Foo tried to access private method 'boolean[] com.package.BarKt.$jacocoInit()'
The reason is that when compiling Bar.kt
, it has only the instrumented version of Foo
. And the compiler embeds the bytecode of the instrumented inline function.
I think the solution is to create two jars, the uninstrumented one for compilation and the instrumented one for runtime.
Builds on previous work (#52) to add coverage support to Kotlin targets in Bazel.
So far, it's working (in basic form), but seems to omit major swaths of our code when I test it on a live codebase. I'm not sure why that happens yet.