From a91b813e43c01606d6ba5c01353adea6a1fe417d Mon Sep 17 00:00:00 2001 From: david-perez Date: Fri, 17 Jun 2022 18:12:56 +0200 Subject: [PATCH] Fix `modifyMtime` task when `generateSmithyBuild` task is up to date (#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. --- buildSrc/src/main/kotlin/CodegenTestCommon.kt | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/buildSrc/src/main/kotlin/CodegenTestCommon.kt b/buildSrc/src/main/kotlin/CodegenTestCommon.kt index aa187751a..74b9b313a 100644 --- a/buildSrc/src/main/kotlin/CodegenTestCommon.kt +++ b/buildSrc/src/main/kotlin/CodegenTestCommon.kt @@ -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() @@ -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 @@ -187,19 +189,23 @@ fun Project.registerModifyMtimeTask() { dependsOn("generateSmithyBuild") doFirst { - @Suppress("UNCHECKED_CAST") val previousBuildHashes: Map = project.extra["previousBuildHashes"] as Map - - 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 = project.extra[previousBuildHashesKey] as Map + + 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) + } + } + } } } } -- GitLab