From 4b8a4e2bdade7fc09247a251abc17fbb1470d270 Mon Sep 17 00:00:00 2001 From: Jiaxiang Chen Date: Tue, 11 Oct 2022 00:09:35 -0700 Subject: [PATCH] use try catch clause for more accurate relative path check. --- .../ksp/processing/impl/CodeGeneratorImpl.kt | 6 +++-- .../com/google/devtools/ksp/Incremental.kt | 9 +++++--- .../google/devtools/ksp/test/PlaygroundIT.kt | 22 +++++++++++++++++++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/common-util/src/main/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImpl.kt b/common-util/src/main/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImpl.kt index ed017edb10..f840b8c208 100644 --- a/common-util/src/main/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImpl.kt +++ b/common-util/src/main/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImpl.kt @@ -156,11 +156,13 @@ class CodeGeneratorImpl( } private val File.relativeFile: File - get() = - if (this.startsWith(buildDir)) + get() { + val buildDirPrefix = if (buildDir.path.startsWith("/")) buildDir.path else buildDir.path.replace("\\", "/") + return if (this.startsWith(buildDirPrefix)) relativeTo(buildDir) else relativeTo(projectBase) + } private fun associate(sources: List, outputPath: File) { if (!isIncremental) diff --git a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt index 2de08ff2e4..d3990914a1 100644 --- a/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt +++ b/compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt @@ -230,11 +230,14 @@ class IncrementalContext( // Ugly, but better than copying the private logics out of stdlib. // TODO: get rid of `relativeTo` if possible. - private fun String.toRelativeFile(): File = - if (this.startsWith(buildDir.path)) + private fun String.toRelativeFile(): File { + val buildDirPrefix = if (buildDir.path.startsWith("/")) buildDir.path else buildDir.path.replace("\\", "/") + return if (this.startsWith(buildDirPrefix)) { File(this).relativeTo(buildDir) - else + } else { File(this).relativeTo(baseDir) + } + } private val KSFile.relativeFile get() = filePath.toRelativeFile() diff --git a/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/PlaygroundIT.kt b/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/PlaygroundIT.kt index 2e16375603..73640fa1e5 100644 --- a/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/PlaygroundIT.kt +++ b/integration-tests/src/test/kotlin/com/google/devtools/ksp/test/PlaygroundIT.kt @@ -65,6 +65,28 @@ class PlaygroundIT { project.restore("workload/build.gradle.kts") } + @Test + fun testArbitraryBuildDir() { + Assume.assumeTrue(System.getProperty("os.name").startsWith("Windows", ignoreCase = true)) + val gradleRunner = GradleRunner.create().withProjectDir(project.root) + + File(project.root, "workload/build.gradle.kts") + .appendText("project.buildDir = File(\"D:/build/\")") + val result = gradleRunner.withArguments("build").build() + + Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:build")?.outcome) + + val artifact = File("D:/build/libs/workload-1.0-SNAPSHOT.jar") + Assert.assertTrue(artifact.exists()) + + JarFile(artifact).use { jarFile -> + Assert.assertTrue(jarFile.getEntry("TestProcessor.log").size > 0) + Assert.assertTrue(jarFile.getEntry("hello/HELLO.class").size > 0) + Assert.assertTrue(jarFile.getEntry("g/G.class").size > 0) + Assert.assertTrue(jarFile.getEntry("com/example/AClassBuilder.class").size > 0) + } + } + @Test fun testAllowSourcesFromOtherPlugins() { fun checkGBuilder() {