Skip to content

Commit

Permalink
Fix dependencies order being stable
Browse files Browse the repository at this point in the history
  • Loading branch information
sellmair committed Apr 6, 2024
1 parent e119963 commit 58922b3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
6 changes: 3 additions & 3 deletions core/src/main/kotlin/io/sellmair/okay/kotlin/kotlinCompile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ suspend fun OkContext.kotlinCompile(): OkPath {
val kotlinSources = modulePath("src").walk().withExtension("kt")

val dependencies = mavenResolveCompileDependencies() +
kotlinCompileDependencies()
kotlinCompileDependencies()

return kotlinCompile(kotlinSources, dependencies, modulePath("build/classes"))
}
Expand All @@ -42,8 +42,8 @@ suspend fun OkContext.kotlinCompile(
return cachedCoroutine(
describeCoroutine("kotlinCompile", verbosity = Info),
input = sources.asInput() +
OkInputs(dependencies.map { it.asInput() }),
output = OkOutputs(listOf(OkOutputDirectory(outputDirectory)))
dependencies.map { it.asInput() }.asInput(),
output = OkOutputDirectory(outputDirectory)
) {
log("Compiling Kotlin")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,24 @@ internal suspend fun OkContext.mavenResolveDependencyTree(
}.asInput(),
output = OkOutput.none()
) {
val resolvedLock = ReentrantLock()
val resolved = mutableSetOf(*declaredDependencies.toTypedArray())

fun CoroutineScope.launchResolve(coordinates: MavenCoordinates): Job = launch(downloadDispatcher) {
val dependencies = mavenResolvePom(coordinates)?.dependencies.orEmpty()
.filter { it in scope }
.map { it.coordinates }

resolvedLock.withLock {
dependencies.forEach { dependency ->
if (resolved.add(dependency)) {
launchResolve(dependency)
withOkContext(downloadDispatcher) {
val results = declaredDependencies.toMutableSet()
var queue = declaredDependencies

while (queue.isNotEmpty()) {
val dependencies = queue.map { coordinates ->
async {
mavenResolvePom(coordinates)?.dependencies.orEmpty()
.filter { it in scope }.map { it.coordinates }
}
}.awaitAll().flatten()

queue = dependencies.filter { coordinates ->
results.add(coordinates)
}
}
}

coroutineScope {
declaredDependencies.forEach { dependency ->
launchResolve(dependency)
}
results.toList()
}

resolved.toList()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CacheTest {
newFile.deleteIfExists()
runOkTest(OkRoot(tempDir)) {
kotlinCompile()
assertCacheUpToDate(path("moduleB"), "kotlinCompile")
assertCacheRestored(path("moduleB"), "kotlinCompile")
assertCacheUpToDate(path("moduleA"), "kotlinCompile")
assertCacheUpToDate(rootPath(), "kotlinCompile")
}
Expand Down Expand Up @@ -98,7 +98,6 @@ class CacheTest {
runOkTest(OkRoot(tempDir)) {
kotlinPackage()
assertCacheMiss(rootPath(), "kotlinPackage")
assertNoDuplicateLogs()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ internal suspend fun assertCacheUpToDate(module: OkPath, id: String) {
}
}

internal suspend fun assertCacheRestored(module: OkPath, id: String) {
val record = assertCacheRecord(module, id)
when (val result = record.result) {
is OkCacheMiss -> fail("Expected CacheHit in module '$module' for '$id'. Dirty: ${result.dirty}")
is OkCacheHit -> {
if (result.restored.none { it.descriptor.id == id }) {
fail("Expected $id to be 'restored'. Found $result")
}
}
}
}


internal suspend fun assertCacheRecord(module: OkPath, id: String): OkTestCoroutineCacheHook.Record {
val matches = cacheRecords()
.filter { it.descriptor.module == module && it.descriptor.id == id }
Expand Down

0 comments on commit 58922b3

Please sign in to comment.