[IC] Relocatable IC caches for projects with custom buildDir

IC caches often contain file paths. To make them relocatable, we need
to convert these file paths into relative paths, relative to a base
directory.
  - If the file paths are source files, we can use the root project
    directory as base.
  - If the file paths are class files, we should use the classes
    directory as base (before this commit, we used the root project
    directory in both cases, that's why we hit KT-58547).

The key changes in this commit include:
  - RelocatableFileToPathConverter: converts paths to relative paths
  - IncrementalCompilationContext: contains 2 different path converters,
    one for source files and one for class files
  - SourceToOutputFilesMap: maps source files to class files using the
    above path converters
  - IncrementalCompilerRunner: creates the path converters based on file
    locations

Test: RelocatableFileToPathConverterTest unit test
      SourceToOutputFilesMapTest unit test
      BuildCacheRelocationIT.testCustomBuildDirectory integration test
^KT-58547 Fixed
This commit is contained in:
Hung Nguyen
2023-08-10 11:05:55 +01:00
committed by Space Team
parent 432c9fe592
commit 894ba9ab80
30 changed files with 329 additions and 293 deletions
@@ -28,7 +28,7 @@ import java.util.*
class BuildDiffsStorageTest {
lateinit var storageFile: File
private val random = Random(System.currentTimeMillis())
private val icContext = IncrementalCompilationContext(null)
private val icContext = IncrementalCompilationContext()
@Before
fun setUp() {
@@ -7,8 +7,7 @@ package org.jetbrains.kotlin.incremental.snapshots
import org.jetbrains.kotlin.TestWithWorkingDir
import org.jetbrains.kotlin.incremental.IncrementalCompilationContext
import org.jetbrains.kotlin.incremental.storage.FileToPathConverter
import org.jetbrains.kotlin.incremental.storage.IncrementalFileToPathConverter
import org.jetbrains.kotlin.incremental.storage.RelocatableFileToPathConverter
import org.junit.After
import org.junit.Assert.assertArrayEquals
import org.junit.Before
@@ -24,9 +23,9 @@ class FileSnapshotMapTest : TestWithWorkingDir() {
super.setUp()
val caches = File(workingDir, "caches").apply { mkdirs() }
val snapshotMapFile = File(caches, "snapshots.tab")
val pathConverter = IncrementalFileToPathConverter((workingDir.canonicalFile))
val pathConverter = RelocatableFileToPathConverter((workingDir.canonicalFile))
val icContext = IncrementalCompilationContext(
pathConverter = pathConverter
pathConverterForSourceFiles = pathConverter
)
snapshotMap = FileSnapshotMap(snapshotMapFile, icContext)
}
@@ -5,131 +5,105 @@
package org.jetbrains.kotlin.incremental.storage
import org.jetbrains.kotlin.TestWithWorkingDir
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertNull
import org.jetbrains.kotlin.incremental.IncrementalCompilationContext
import org.junit.After
import org.junit.Assert.assertArrayEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.io.File
import kotlin.properties.Delegates
import kotlin.test.assertFailsWith
class SourceToOutputFilesMapTest : TestWithWorkingDir() {
private var stofMap: SourceToOutputFilesMap by Delegates.notNull()
private var pathConverter: FileToPathConverter by Delegates.notNull()
class SourceToOutputFilesMapTest {
@get:Rule
val tmpDir = TemporaryFolder()
private lateinit var srcDir: File
private lateinit var classesDir: File
private lateinit var stofMap: SourceToOutputFilesMap
private lateinit var fooDotKt: File
private lateinit var fooDotClass: File
@Before
override fun setUp() {
super.setUp()
val caches = File(workingDir, "caches").apply { mkdirs() }
val stofMapFile = File(caches, "stof.tab")
pathConverter = IncrementalFileToPathConverter((workingDir.canonicalFile))
val icContext = IncrementalCompilationContext(
pathConverter = pathConverter
fun setUp() {
val workingDir = tmpDir.root
srcDir = workingDir.resolve("src")
classesDir = workingDir.resolve("classes")
stofMap = SourceToOutputFilesMap(
storageFile = workingDir.resolve("stof.tab"),
icContext = IncrementalCompilationContext(
pathConverterForSourceFiles = RelocatableFileToPathConverter(srcDir),
pathConverterForOutputFiles = RelocatableFileToPathConverter(classesDir),
)
)
stofMap = SourceToOutputFilesMap(stofMapFile, icContext)
fooDotKt = srcDir.resolve("Foo.kt")
fooDotClass = classesDir.resolve("Foo.class")
}
@After
override fun tearDown() {
fun tearDown() {
stofMap.flush(false)
stofMap.closeForTest()
super.tearDown()
}
@Test
fun testEmptyGetReturnsEmpty() {
assertTrue(stofMap.get(File("")).isEmpty())
fun testNoSetGetReturnsNull() {
assertNull(stofMap[fooDotKt])
}
@Test
fun testSetGetOneReturnsOne() {
stofMap.set(
File(""),
listOf(File("one").canonicalFile))
assertEquals(
listOf(File("one").canonicalFile),
stofMap.get(File("")))
fun testSetOneGetReturnsOne() {
stofMap[fooDotKt] = listOf(fooDotClass)
assertEquals(listOf(fooDotClass), stofMap[fooDotKt])
}
@Test
fun testSetDupeReturnsUnique() {
stofMap.set(
File(""),
listOf(File("one").canonicalFile, File("one").canonicalFile, File("one").canonicalFile))
fun testSetDupeGetReturnsUnique() {
stofMap[fooDotKt] = listOf(fooDotClass, fooDotClass)
assertEquals(
listOf(File("one").canonicalFile),
stofMap.get(File("")))
assertEquals(listOf(fooDotClass), stofMap[fooDotKt])
}
@Test
fun testSetOverwriteReturnsNew() {
stofMap.set(
File(""),
listOf(File("old").canonicalFile, File("old").canonicalFile, File("old").canonicalFile))
stofMap.set(
File(""),
listOf(File("one").canonicalFile, File("two").canonicalFile, File("three").canonicalFile))
fun testSetOverwriteGetReturnsNew() {
val fooKtDotClass = classesDir.resolve("FooKt.class")
stofMap[fooDotKt] = listOf(fooDotClass)
stofMap[fooDotKt] = listOf(fooKtDotClass)
assertArrayEquals(
listOf(File("one").canonicalFile, File("two").canonicalFile, File("three").canonicalFile).toSortedPaths(),
stofMap.get(File("")).toSortedPaths())
assertEquals(listOf(fooKtDotClass), stofMap[fooDotKt])
}
@Test
fun testRelativeInReturnsAbsolute() {
stofMap.set(
File(""),
listOf(File("one"), File("two"), File("three")))
assertArrayEquals(
listOf(File("one").canonicalFile, File("two").canonicalFile, File("three").canonicalFile).toSortedPaths(),
stofMap.get(File("")).toSortedPaths()
)
}
@Test
fun testSetRelativeGetAbsolute() {
stofMap.set(
File("blah"),
listOf(File("one"), File("two"), File("three")))
assertArrayEquals(
listOf(File("one").canonicalFile, File("two").canonicalFile, File("three").canonicalFile).toSortedPaths(),
stofMap.get(File("blah").canonicalFile).toSortedPaths()
)
}
@Test
fun testSetRemove() {
stofMap.set(
File("blah"),
listOf(File("one"), File("two"), File("three")))
assertArrayEquals(
listOf(File("one").canonicalFile, File("two").canonicalFile, File("three").canonicalFile).toSortedPaths(),
stofMap.remove(File("blah")).toSortedPaths()
)
assertTrue(stofMap.get(File("blah")).isEmpty())
}
@Test
fun testSetRemoveLoop() {
repeat(5) {
stofMap.set(
File("blah"),
listOf(File("one"), File("two"), File("three"))
)
assertArrayEquals(
listOf(File("one").canonicalFile, File("two").canonicalFile, File("three").canonicalFile).toSortedPaths(),
stofMap.remove(File("blah")).toSortedPaths()
)
assertTrue(stofMap.get(File("blah")).isEmpty())
fun testSetRelativeFails() {
assertFailsWith<IllegalStateException> {
stofMap[fooDotKt] = listOf(File("relativePath"))
}
}
private fun Iterable<File>.toSortedPaths(): Array<String> =
map { it.canonicalPath }.sorted().toTypedArray()
@Test
fun testGetRelativeFails() {
stofMap[fooDotKt] = listOf(fooDotClass)
assertFailsWith<IllegalStateException> {
stofMap[fooDotKt.relativeTo(srcDir)]
}
}
@Test
fun testGetAndRemove() {
stofMap[fooDotKt] = listOf(fooDotClass)
assertEquals(listOf(fooDotClass), stofMap.getAndRemove(fooDotKt))
assertNull(stofMap[fooDotKt])
}
}