diff --git a/WORKSPACE b/WORKSPACE index 2dde36617..09b905cca 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -29,4 +29,4 @@ rbe_autoconfig( ) android_sdk_repository(name = "androidsdk") -android_ndk_repository(name = "androidndk") \ No newline at end of file +android_ndk_repository(name = "androidndk") diff --git a/kotlin/internal/jvm/impl.bzl b/kotlin/internal/jvm/impl.bzl index fa86a71e0..6b84e2a69 100644 --- a/kotlin/internal/jvm/impl.bzl +++ b/kotlin/internal/jvm/impl.bzl @@ -62,6 +62,9 @@ def _write_launcher_action(ctx, rjars, main_class, jvm_flags, args = "", wrapper "%javabin%": javabin, "%jvm_flags%": jvm_flags, "%set_jacoco_metadata%": "", + "%set_jacoco_main_class%": "", + "%set_jacoco_java_runfiles_root%": "", + "%set_java_coverage_new_implementation%": "", "%workspace_prefix%": ctx.workspace_name + "/", }, is_executable = True, diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/BazelWorker.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/BazelWorker.kt index b5f8638f2..2f865a132 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/BazelWorker.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/BazelWorker.kt @@ -82,13 +82,20 @@ class BazelWorker( val exitCode = try { delegate.apply(loadArguments(request.argumentsList, true)) } catch (e: RuntimeException) { - return if (wasInterrupted(e)) INTERUPTED_STATUS + val innerExitCode = if (wasInterrupted(e)) INTERUPTED_STATUS else ERROR_STATUS.also { System.err.println( "ERROR: Worker threw uncaught exception with args: ${request.argumentsList.joinToString(" ")}" ) e.printStackTrace(System.err) } + WorkerProtocol.WorkResponse.newBuilder() + .setOutput(buffer.toString()) + .setExitCode(innerExitCode) + .build() + .writeDelimitedTo(realStdOut) + realStdOut.flush() + return innerExitCode } WorkerProtocol.WorkResponse.newBuilder() diff --git a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutor.kt b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutor.kt index 8cb75f760..c326fd977 100644 --- a/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutor.kt +++ b/src/main/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutor.kt @@ -201,31 +201,31 @@ class KotlinJvmTaskExecutor @Inject internal constructor( it.execute() }.sourcesList.iterator() ) +} - /** - * Create a new [JvmCompilationTask] with sources found in the generatedSources directory. This should be run after - * annotation processors have been run. - */ - private fun JvmCompilationTask.expandWithGeneratedSources(): JvmCompilationTask = - expandWithSources( - File(directories.generatedSources).walkTopDown() - .filter { it.isFile } - .map { it.path } - .iterator() - ) - - private fun JvmCompilationTask.expandWithSources(sources: Iterator): JvmCompilationTask = - updateBuilder { builder -> - sources.partitionJvmSources( +/** + * Create a new [JvmCompilationTask] with sources found in the generatedSources directory. This should be run after + * annotation processors have been run. + */ +fun JvmCompilationTask.expandWithGeneratedSources(): JvmCompilationTask = + expandWithSources( + File(directories.generatedSources).walkTopDown() + .filter { it.isFile && IS_JVM_SOURCE_FILE.test(it.name) } + .map { it.path } + .iterator() + ) + +fun JvmCompilationTask.expandWithSources(sources: Iterator): JvmCompilationTask = + updateBuilder { builder -> + sources.partitionJvmSources( { builder.inputsBuilder.addKotlinSources(it) }, { builder.inputsBuilder.addJavaSources(it) }) - } + } - private fun JvmCompilationTask.updateBuilder( +fun JvmCompilationTask.updateBuilder( block: (JvmCompilationTask.Builder) -> Unit - ): JvmCompilationTask = - toBuilder().let { - block(it) - it.build() - } -} +): JvmCompilationTask = + toBuilder().let { + block(it) + it.build() + } \ No newline at end of file diff --git a/src/test/kotlin/io/bazel/kotlin/builder/BUILD b/src/test/kotlin/io/bazel/kotlin/builder/BUILD index 37db8ff13..25c745f22 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/BUILD +++ b/src/test/kotlin/io/bazel/kotlin/builder/BUILD @@ -67,6 +67,12 @@ kt_rules_test( srcs = ["tasks/jvm/KotlinBuilderJvmBasicTest.java"], ) +kt_rules_test( + name = "KotlinJvmTaskExecutorTest", + srcs = ["tasks/jvm/KotlinJvmTaskExecutorTest.kt"], + deps = ["@com_github_jetbrains_kotlin//:kotlin-test"], +) + # TODO(bazelbuild/rules_kotlin/issues/275): Remove full jar reference when the kt_rules_test handles jvm_import data better. _MAVEN_CENTRAL_PREFIX = "@kotlin_rules_maven//:v1/https/maven-central.storage.googleapis.com/repos/central/data" _AUTO_VALUE_PREFIX = "%s/com/google/auto/value" % _MAVEN_CENTRAL_PREFIX @@ -98,6 +104,7 @@ test_suite( ":KotlinBuilderJsTest", ":KotlinBuilderJvmBasicTest", ":KotlinBuilderJvmKaptTest", + ":KotlinJvmTaskExecutorTest", ":SourceJarCreatorTest", ], visibility = ["//visibility:public"], diff --git a/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java b/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java index bcf18ab5d..3cab60cec 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java +++ b/src/test/kotlin/io/bazel/kotlin/builder/KotlinAbstractTestBuilder.java @@ -63,7 +63,7 @@ public final List outLines() { return outLines; } - final void resetForNext() { + public final void resetForNext() { outLines = null; label = "a_test_" + counter.incrementAndGet(); infoBuilder @@ -96,8 +96,8 @@ public final void setDebugTags(String... tags) { infoBuilder.addAllDebug(Arrays.asList(tags)); } - final Path writeSourceFile(String filename, String[] lines) { - Path path = directory(DirectoryType.SOURCES).resolve(filename).toAbsolutePath(); + final Path writeFile(DirectoryType dirType, String filename, String[] lines) { + Path path = directory(dirType).resolve(filename).toAbsolutePath(); try (FileOutputStream fos = new FileOutputStream(path.toFile())) { fos.write(String.join("\n", lines).getBytes(UTF_8)); } catch (IOException e) { @@ -106,6 +106,14 @@ final Path writeSourceFile(String filename, String[] lines) { return path; } + public final Path writeSourceFile(String filename, String[] lines) { + return writeFile(DirectoryType.SOURCES, filename, lines); + } + + public final Path writeGeneratedSourceFile(String filename, String[] lines) { + return writeFile(DirectoryType.SOURCE_GEN, filename, lines); + } + private R runCompileTask( CompilationTaskInfo info, T task, BiFunction operation) { String curDir = System.getProperty("user.dir"); diff --git a/src/test/kotlin/io/bazel/kotlin/builder/KotlinJvmTestBuilder.java b/src/test/kotlin/io/bazel/kotlin/builder/KotlinJvmTestBuilder.java index 56c38a727..051cb3660 100644 --- a/src/test/kotlin/io/bazel/kotlin/builder/KotlinJvmTestBuilder.java +++ b/src/test/kotlin/io/bazel/kotlin/builder/KotlinJvmTestBuilder.java @@ -82,12 +82,12 @@ void setupForNext(CompilationTaskInfo.Builder taskInfo) { } @Override - JvmCompilationTask buildTask() { + public JvmCompilationTask buildTask() { return taskBuilder.build(); } public class TaskBuilder { - TaskBuilder() {} + public TaskBuilder() {} public void setLabel(String label) { taskBuilder.getInfoBuilder().setLabel(label); diff --git a/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutorTest.kt b/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutorTest.kt new file mode 100644 index 000000000..96f7a2758 --- /dev/null +++ b/src/test/kotlin/io/bazel/kotlin/builder/tasks/jvm/KotlinJvmTaskExecutorTest.kt @@ -0,0 +1,50 @@ +package io.bazel.kotlin.builder.tasks.jvm + +import io.bazel.kotlin.builder.KotlinJsTestBuilder +import io.bazel.kotlin.builder.KotlinJvmTestBuilder +import io.bazel.kotlin.model.JvmCompilationTask +import org.junit.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class KotlinJvmTaskExecutorTest { + + private val ctx = KotlinJvmTestBuilder() + + @Test + fun testSimpleGeneratedNonJvmSourcesIgnored() { + ctx.resetForNext() + ctx.writeGeneratedSourceFile( + "AGenClass.kt", + arrayOf("package something.gen;", "class AGenClass{}")) + ctx.writeGeneratedSourceFile( + "AnotherGenClass.java", + arrayOf("package something.gen;", "class AnotherGenClass{}")) + ctx.writeGeneratedSourceFile( + "ignore-me.txt", + arrayOf("contents do not matter")) + ctx.writeSourceFile( + "ignore-me-regular-src.kt", + arrayOf("contents do not matter")) + ctx.writeSourceFile( + "ignore-me-another-regular-src.java", + arrayOf("contents do not matter")) + val compileTask = ctx.buildTask() + + assertFalse(compileTask.hasInputs()) + + val expandedCompileTask = compileTask.expandWithGeneratedSources() + + assertFalse(compileTask.hasInputs()) + + assertTrue(expandedCompileTask.hasInputs()) + assertNotNull(expandedCompileTask.inputs.javaSourcesList.find { + path -> path.endsWith("a_test_1/generated_sources/AnotherGenClass.java") }) + assertEquals(expandedCompileTask.inputs.javaSourcesCount, 1) + assertNotNull(expandedCompileTask.inputs.kotlinSourcesList.find { + path -> path.endsWith("a_test_1/generated_sources/AGenClass.kt") }) + assertEquals(expandedCompileTask.inputs.kotlinSourcesCount, 1) + } +} \ No newline at end of file