Skip to content
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

Filter non-kotlin code out of generated sources #263

Merged
merged 11 commits into from
Feb 24, 2020
Merged
2 changes: 1 addition & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ rbe_autoconfig(
)

android_sdk_repository(name = "androidsdk")
android_ndk_repository(name = "androidndk")
android_ndk_repository(name = "androidndk")
3 changes: 3 additions & 0 deletions kotlin/internal/jvm/impl.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): 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<String>): 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()
}
7 changes: 7 additions & 0 deletions src/test/kotlin/io/bazel/kotlin/builder/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -98,6 +104,7 @@ test_suite(
":KotlinBuilderJsTest",
":KotlinBuilderJvmBasicTest",
":KotlinBuilderJvmKaptTest",
":KotlinJvmTaskExecutorTest",
":SourceJarCreatorTest",
],
visibility = ["//visibility:public"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public final List<String> outLines() {
return outLines;
}

final void resetForNext() {
public final void resetForNext() {
outLines = null;
label = "a_test_" + counter.incrementAndGet();
infoBuilder
Expand Down Expand Up @@ -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) {
Expand All @@ -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> R runCompileTask(
CompilationTaskInfo info, T task, BiFunction<CompilationTaskContext, T, R> operation) {
String curDir = System.getProperty("user.dir");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}