Skip to content

Commit

Permalink
Fix modifyMtime task when generateSmithyBuild task is up to date (#…
Browse files Browse the repository at this point in the history
…1471)

When `generateSmithyBuild` task is up to date, the hashes of the build
are not calculated, so `modifyMtime` fails, because it expects them to
be registered in Gradle's project properties. This can happen if you run
a command like `./gradlew codegen-server-test:test` twice consecutively.

In this case, we should skip the `modifyMtime` task altogether.
Everything is up to date, so no codegen will run, and no artifacts will
change.
  • Loading branch information
david-perez authored Jun 17, 2022
1 parent 5af3b10 commit a91b813
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions buildSrc/src/main/kotlin/CodegenTestCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ fun Project.registerGenerateSmithyBuildTask(

// If this is a rebuild, cache all the hashes of the generated Rust files. These are later used by the
// `modifyMtime` task.
project.extra["previousBuildHashes"] = project.buildDir.walk()
project.extra[previousBuildHashesKey] = project.buildDir.walk()
.filter { it.isFile }
.map {
getChecksumForFile(it) to it.lastModified()
Expand Down Expand Up @@ -174,6 +174,8 @@ fun Project.registerGenerateCargoConfigTomlTask(
}
}

const val previousBuildHashesKey = "previousBuildHashes"

fun Project.registerModifyMtimeTask() {
// Cargo uses `mtime` (among other factors) to determine whether a compilation unit needs a rebuild. While developing,
// it is likely that only a small number of the generated crate files are modified across rebuilds. This task compares
Expand All @@ -187,19 +189,23 @@ fun Project.registerModifyMtimeTask() {
dependsOn("generateSmithyBuild")

doFirst {
@Suppress("UNCHECKED_CAST") val previousBuildHashes: Map<String, Long> = project.extra["previousBuildHashes"] as Map<String, Long>

project.buildDir.walk()
.filter { it.isFile }
.map {
getChecksumForFile(it) to it
}
.forEach { (currentHash, currentFile) ->
previousBuildHashes[currentHash]?.also { oldMtime ->
println("Setting `mtime` of $currentFile back to `$oldMtime` because its hash `$currentHash` remained unchanged after a rebuild.")
currentFile.setLastModified(oldMtime)
if (!project.extra.has(previousBuildHashesKey)) {
println("No hashes from a previous build exist because `generateSmithyBuild` is up to date, skipping `mtime` fixups")
} else {
@Suppress("UNCHECKED_CAST") val previousBuildHashes: Map<String, Long> = project.extra[previousBuildHashesKey] as Map<String, Long>

project.buildDir.walk()
.filter { it.isFile }
.map {
getChecksumForFile(it) to it
}
}
.forEach { (currentHash, currentFile) ->
previousBuildHashes[currentHash]?.also { oldMtime ->
println("Setting `mtime` of $currentFile back to `$oldMtime` because its hash `$currentHash` remained unchanged after a rebuild.")
currentFile.setLastModified(oldMtime)
}
}
}
}
}
}
Expand Down

0 comments on commit a91b813

Please sign in to comment.