Skip to content

Commit 9ff87e5

Browse files
committed
use try catch clause for more accurate relative path check.
1 parent e9d7c81 commit 9ff87e5

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

common-util/src/main/kotlin/com/google/devtools/ksp/processing/impl/CodeGeneratorImpl.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,13 @@ class CodeGeneratorImpl(
156156
}
157157

158158
private val File.relativeFile: File
159-
get() =
160-
if (this.startsWith(buildDir))
159+
get() {
160+
val buildDirPrefix = if (buildDir.path.startsWith("/")) buildDir.path else buildDir.path.replace("\\", "/")
161+
return if (this.startsWith(buildDirPrefix))
161162
relativeTo(buildDir)
162163
else
163164
relativeTo(projectBase)
165+
}
164166

165167
private fun associate(sources: List<KSFile>, outputPath: File) {
166168
if (!isIncremental)

compiler-plugin/src/main/kotlin/com/google/devtools/ksp/Incremental.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,14 @@ class IncrementalContext(
230230

231231
// Ugly, but better than copying the private logics out of stdlib.
232232
// TODO: get rid of `relativeTo` if possible.
233-
private fun String.toRelativeFile(): File =
234-
if (this.startsWith(buildDir.path))
233+
private fun String.toRelativeFile(): File {
234+
val buildDirPrefix = if (buildDir.path.startsWith("/")) buildDir.path else buildDir.path.replace("\\", "/")
235+
return if (this.startsWith(buildDirPrefix)) {
235236
File(this).relativeTo(buildDir)
236-
else
237+
} else {
237238
File(this).relativeTo(baseDir)
239+
}
240+
}
238241

239242
private val KSFile.relativeFile
240243
get() = filePath.toRelativeFile()

integration-tests/src/test/kotlin/com/google/devtools/ksp/test/PlaygroundIT.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ class PlaygroundIT {
6565
project.restore("workload/build.gradle.kts")
6666
}
6767

68+
@Test
69+
fun testArbitraryBuildDir() {
70+
Assume.assumeTrue(System.getProperty("os.name").startsWith("Windows", ignoreCase = true))
71+
val gradleRunner = GradleRunner.create().withProjectDir(project.root)
72+
73+
File(project.root, "workload/build.gradle.kts")
74+
.appendText("project.buildDir = File(\"D:/build/\")")
75+
val result = gradleRunner.withArguments("build").build()
76+
77+
Assert.assertEquals(TaskOutcome.SUCCESS, result.task(":workload:build")?.outcome)
78+
79+
val artifact = File("D:/build/libs/workload-1.0-SNAPSHOT.jar")
80+
Assert.assertTrue(artifact.exists())
81+
82+
JarFile(artifact).use { jarFile ->
83+
Assert.assertTrue(jarFile.getEntry("TestProcessor.log").size > 0)
84+
Assert.assertTrue(jarFile.getEntry("hello/HELLO.class").size > 0)
85+
Assert.assertTrue(jarFile.getEntry("g/G.class").size > 0)
86+
Assert.assertTrue(jarFile.getEntry("com/example/AClassBuilder.class").size > 0)
87+
}
88+
}
89+
6890
@Test
6991
fun testAllowSourcesFromOtherPlugins() {
7092
fun checkGBuilder() {

0 commit comments

Comments
 (0)