Unverified Commit fbde9d87 authored by david-perez's avatar david-perez Committed by GitHub
Browse files

Do not set `RUSTFLAGS` environment variable when invoking Cargo commands via Gradle tasks (#3678)

Otherwise, if you generate a crate and compile it using Gradle, like for
example using the invocation:

```
./gradlew -P modules='simple' -P cargoCommands='test' codegen-server-test:build
```

And then manually run a `cargo` command within the generated crate
directory, or open the project using `rust-analyzer`, Cargo will
re-compile the project from scratch, with
`CARGO_LOG=cargo::core::compiler::fingerprint=trace` reporting that
flags have changed since the last compilation.

Instead, it's best if we persist these flags to `.cargo/config.toml`, so
all `cargo` invocations, either through Gradle, manually, or through
`rust-analyzer`, use the same set. This way, if no files were changed,
subsequent compilations since code generation will truly be no-ops, with
Cargo reusing all artifacts.

Note this commit fixes a regression that was introduced when `--cfg
aws_sdk_unstable` was introduced in #2614, since I fixed this the first
time back in #1422.

----

_By submitting this pull request, I confirm that you can use, modify,
copy, and redistribute this contribution, under the terms of your
choice._
parent 99e1e208
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -20,7 +20,6 @@ java {
}
}


val smithyVersion: String by project
val smithyVersion: String by project
val defaultRustDocFlags: String by project
val properties = PropertyRetriever(rootProject, project)
val properties = PropertyRetriever(rootProject, project)


val pluginName = "rust-client-codegen"
val pluginName = "rust-client-codegen"
@@ -89,7 +88,7 @@ tasks["smithyBuild"].dependsOn("generateSmithyBuild")
tasks["assemble"].finalizedBy("generateCargoWorkspace")
tasks["assemble"].finalizedBy("generateCargoWorkspace")


project.registerModifyMtimeTask()
project.registerModifyMtimeTask()
project.registerCargoCommandsTasks(layout.buildDirectory.dir(workingDirUnderBuildDir).get().asFile, defaultRustDocFlags)
project.registerCargoCommandsTasks(layout.buildDirectory.dir(workingDirUnderBuildDir).get().asFile)


tasks["test"].finalizedBy(cargoCommands(properties).map { it.toString })
tasks["test"].finalizedBy(cargoCommands(properties).map { it.toString })


+1 −2
Original line number Original line Diff line number Diff line
@@ -31,7 +31,6 @@ configure<software.amazon.smithy.gradle.SmithyExtension> {
}
}


val smithyVersion: String by project
val smithyVersion: String by project
val defaultRustDocFlags: String by project
val properties = PropertyRetriever(rootProject, project)
val properties = PropertyRetriever(rootProject, project)


val crateHasherToolPath = rootProject.projectDir.resolve("tools/ci-build/crate-hasher")
val crateHasherToolPath = rootProject.projectDir.resolve("tools/ci-build/crate-hasher")
@@ -461,7 +460,7 @@ tasks.register<Copy>("copyCheckedInCargoLock") {
    into(outputDir)
    into(outputDir)
}
}


project.registerCargoCommandsTasks(outputDir.asFile, defaultRustDocFlags)
project.registerCargoCommandsTasks(outputDir.asFile)
project.registerGenerateCargoConfigTomlTask(outputDir.asFile)
project.registerGenerateCargoConfigTomlTask(outputDir.asFile)


//The task name "test" is already registered by one of our plugins
//The task name "test" is already registered by one of our plugins
+10 −11
Original line number Original line Diff line number Diff line
@@ -205,13 +205,15 @@ fun Project.registerGenerateCargoWorkspaceTask(
fun Project.registerGenerateCargoConfigTomlTask(outputDir: File) {
fun Project.registerGenerateCargoConfigTomlTask(outputDir: File) {
    this.tasks.register("generateCargoConfigToml") {
    this.tasks.register("generateCargoConfigToml") {
        description = "generate `.cargo/config.toml`"
        description = "generate `.cargo/config.toml`"
        // TODO(https://github.com/smithy-lang/smithy-rs/issues/1068): Once doc normalization
        // is completed, warnings can be prohibited in rustdoc by setting `rustdocflags` to `-D warnings`.
        doFirst {
        doFirst {
            outputDir.resolve(".cargo").mkdirs()
            outputDir.resolve(".cargo").mkdirs()
            outputDir.resolve(".cargo/config.toml")
            outputDir.resolve(".cargo/config.toml")
                .writeText(
                .writeText(
                    """
                    """
                    [build]
                    [build]
                    rustflags = ["--deny", "warnings"]
                    rustflags = ["--deny", "warnings", "--cfg", "aws_sdk_unstable"]
                    """.trimIndent(),
                    """.trimIndent(),
                )
                )
        }
        }
@@ -256,10 +258,7 @@ fun Project.registerModifyMtimeTask() {
    }
    }
}
}


fun Project.registerCargoCommandsTasks(
fun Project.registerCargoCommandsTasks(outputDir: File) {
    outputDir: File,
    defaultRustDocFlags: String,
) {
    val dependentTasks =
    val dependentTasks =
        listOfNotNull(
        listOfNotNull(
            "assemble",
            "assemble",
@@ -270,29 +269,29 @@ fun Project.registerCargoCommandsTasks(
    this.tasks.register<Exec>(Cargo.CHECK.toString) {
    this.tasks.register<Exec>(Cargo.CHECK.toString) {
        dependsOn(dependentTasks)
        dependsOn(dependentTasks)
        workingDir(outputDir)
        workingDir(outputDir)
        environment("RUSTFLAGS", "--cfg aws_sdk_unstable")
        commandLine("cargo", "check", "--lib", "--tests", "--benches", "--all-features")
        commandLine("cargo", "check", "--lib", "--tests", "--benches", "--all-features")
    }
    }


    this.tasks.register<Exec>(Cargo.TEST.toString) {
    this.tasks.register<Exec>(Cargo.TEST.toString) {
        dependsOn(dependentTasks)
        dependsOn(dependentTasks)
        workingDir(outputDir)
        workingDir(outputDir)
        environment("RUSTFLAGS", "--cfg aws_sdk_unstable")
        commandLine("cargo", "test", "--all-features", "--no-fail-fast")
        commandLine("cargo", "test", "--all-features", "--no-fail-fast")
    }
    }


    this.tasks.register<Exec>(Cargo.DOCS.toString) {
    this.tasks.register<Exec>(Cargo.DOCS.toString) {
        dependsOn(dependentTasks)
        dependsOn(dependentTasks)
        workingDir(outputDir)
        workingDir(outputDir)
        environment("RUSTDOCFLAGS", defaultRustDocFlags)
        val args =
        environment("RUSTFLAGS", "--cfg aws_sdk_unstable")
            mutableListOf(
        commandLine("cargo", "doc", "--no-deps", "--document-private-items")
                "--no-deps",
                "--document-private-items",
            )
        commandLine("cargo", "doc", *args.toTypedArray())
    }
    }


    this.tasks.register<Exec>(Cargo.CLIPPY.toString) {
    this.tasks.register<Exec>(Cargo.CLIPPY.toString) {
        dependsOn(dependentTasks)
        dependsOn(dependentTasks)
        workingDir(outputDir)
        workingDir(outputDir)
        environment("RUSTFLAGS", "--cfg aws_sdk_unstable")
        commandLine("cargo", "clippy")
        commandLine("cargo", "clippy")
    }
    }
}
}
+1 −2
Original line number Original line Diff line number Diff line
@@ -15,7 +15,6 @@ plugins {
}
}


val smithyVersion: String by project
val smithyVersion: String by project
val defaultRustDocFlags: String by project
val properties = PropertyRetriever(rootProject, project)
val properties = PropertyRetriever(rootProject, project)
fun getSmithyRuntimeMode(): String = properties.get("smithy.runtime.mode") ?: "orchestrator"
fun getSmithyRuntimeMode(): String = properties.get("smithy.runtime.mode") ?: "orchestrator"


@@ -134,7 +133,7 @@ tasks["smithyBuild"].dependsOn("generateSmithyBuild")
tasks["assemble"].finalizedBy("generateCargoWorkspace")
tasks["assemble"].finalizedBy("generateCargoWorkspace")


project.registerModifyMtimeTask()
project.registerModifyMtimeTask()
project.registerCargoCommandsTasks(layout.buildDirectory.dir(workingDirUnderBuildDir).get().asFile, defaultRustDocFlags)
project.registerCargoCommandsTasks(layout.buildDirectory.dir(workingDirUnderBuildDir).get().asFile)


tasks["test"].finalizedBy(cargoCommands(properties).map { it.toString })
tasks["test"].finalizedBy(cargoCommands(properties).map { it.toString })


+1 −2
Original line number Original line Diff line number Diff line
@@ -16,7 +16,6 @@ plugins {
}
}


val smithyVersion: String by project
val smithyVersion: String by project
val defaultRustDocFlags: String by project
val properties = PropertyRetriever(rootProject, project)
val properties = PropertyRetriever(rootProject, project)


val pluginName = "rust-server-codegen"
val pluginName = "rust-server-codegen"
@@ -112,7 +111,7 @@ tasks["smithyBuild"].dependsOn("generateSmithyBuild")
tasks["assemble"].finalizedBy("generateCargoWorkspace", "generateCargoConfigToml")
tasks["assemble"].finalizedBy("generateCargoWorkspace", "generateCargoConfigToml")


project.registerModifyMtimeTask()
project.registerModifyMtimeTask()
project.registerCargoCommandsTasks(layout.buildDirectory.dir(workingDirUnderBuildDir).get().asFile, defaultRustDocFlags)
project.registerCargoCommandsTasks(layout.buildDirectory.dir(workingDirUnderBuildDir).get().asFile)


tasks["test"].finalizedBy(cargoCommands(properties).map { it.toString })
tasks["test"].finalizedBy(cargoCommands(properties).map { it.toString })


Loading