Skip to content

Commit

Permalink
Do not set RUSTFLAGS environment variable when invoking Cargo comma…
Browse files Browse the repository at this point in the history
…nds 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._
  • Loading branch information
david-perez authored Jun 21, 2024
1 parent 99e1e20 commit fbde9d8
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 30 deletions.
3 changes: 1 addition & 2 deletions aws/sdk-adhoc-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ java {
}

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

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

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 })

Expand Down
3 changes: 1 addition & 2 deletions aws/sdk/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ configure<software.amazon.smithy.gradle.SmithyExtension> {
}

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

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

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

//The task name "test" is already registered by one of our plugins
Expand Down
21 changes: 10 additions & 11 deletions buildSrc/src/main/kotlin/CodegenTestCommon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,15 @@ fun Project.registerGenerateCargoWorkspaceTask(
fun Project.registerGenerateCargoConfigTomlTask(outputDir: File) {
this.tasks.register("generateCargoConfigToml") {
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 {
outputDir.resolve(".cargo").mkdirs()
outputDir.resolve(".cargo/config.toml")
.writeText(
"""
[build]
rustflags = ["--deny", "warnings"]
rustflags = ["--deny", "warnings", "--cfg", "aws_sdk_unstable"]
""".trimIndent(),
)
}
Expand Down Expand Up @@ -256,10 +258,7 @@ fun Project.registerModifyMtimeTask() {
}
}

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

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

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

this.tasks.register<Exec>(Cargo.CLIPPY.toString) {
dependsOn(dependentTasks)
workingDir(outputDir)
environment("RUSTFLAGS", "--cfg aws_sdk_unstable")
commandLine("cargo", "clippy")
}
}
3 changes: 1 addition & 2 deletions codegen-client-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ plugins {
}

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

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

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 })

Expand Down
3 changes: 1 addition & 2 deletions codegen-server-test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ plugins {
}

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

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

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 })

Expand Down
3 changes: 1 addition & 2 deletions codegen-server-test/python/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ plugins {
}

val smithyVersion: String by project
val defaultRustDocFlags: String by project
val properties = PropertyRetriever(rootProject, project)
val buildDir = layout.buildDirectory.get().asFile

Expand Down Expand Up @@ -128,7 +127,7 @@ tasks["smithyBuild"].dependsOn("generateSmithyBuild")
tasks["assemble"].finalizedBy("generateCargoWorkspace")

project.registerModifyMtimeTask()
project.registerCargoCommandsTasks(buildDir.resolve(workingDirUnderBuildDir), defaultRustDocFlags)
project.registerCargoCommandsTasks(buildDir.resolve(workingDirUnderBuildDir))

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

Expand Down
3 changes: 1 addition & 2 deletions codegen-server-test/typescript/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ plugins {
}

val smithyVersion: String by project
val defaultRustDocFlags: String by project
val properties = PropertyRetriever(rootProject, project)
val buildDir = layout.buildDirectory.get().asFile

Expand Down Expand Up @@ -49,7 +48,7 @@ tasks["smithyBuild"].dependsOn("generateSmithyBuild")
tasks["assemble"].finalizedBy("generateCargoWorkspace")

project.registerModifyMtimeTask()
project.registerCargoCommandsTasks(buildDir.resolve(workingDirUnderBuildDir), defaultRustDocFlags)
project.registerCargoCommandsTasks(buildDir.resolve(workingDirUnderBuildDir))

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class PythonServerUnionGenerator(
) {
if (member.isTargetUnit()) {
writer.rust(
"/// Tries to convert the union instance into [`$variantName`].",
"/// Tries to convert the enum instance into [`$variantName`](#T::$variantName), extracting the inner `()`.",
unionSymbol,
)
writer.rust("/// :rtype None:")
writer.rustBlockTemplate("pub fn as_$funcNamePart(&self) -> #{pyo3}::PyResult<()>", "pyo3" to pyo3) {
Expand Down
6 changes: 0 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,3 @@ ktlintVersion=1.0.1
kotestVersion=5.8.0
# Avoid registering dependencies/plugins/tasks that are only used for testing purposes
isTestingEnabled=true

# TODO(https://github.com/smithy-lang/smithy-rs/issues/1068): Once doc normalization
# is completed, warnings can be prohibited in rustdoc.
#
# defaultRustDocFlags=-D warnings
defaultRustDocFlags=

0 comments on commit fbde9d8

Please sign in to comment.