Skip to content

Commit 37a4555

Browse files
committed
Auto merge of #6694 - ordovicia:misspell-env-var, r=dwijnand
Emit warning on misspelled environment variables This PR makes Cargo emit a warning when `RUST_FLAGS` or `RUSTDOC_FLAGS` environment variables are used instead of `RUSTFLAGS` or `RUSTDOCFLAGS`. ```shell $ RUST_FLAGS=foo ./target/debug/cargo build warning: Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`? ``` Fixes #6406
2 parents 780aec2 + 3ca98ad commit 37a4555

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/cargo/ops/cargo_compile.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,27 @@ pub fn compile_ws<'a>(
255255
ref export_dir,
256256
} = *options;
257257

258+
match build_config.mode {
259+
CompileMode::Test
260+
| CompileMode::Build
261+
| CompileMode::Check { .. }
262+
| CompileMode::Bench
263+
| CompileMode::RunCustomBuild => {
264+
if std::env::var("RUST_FLAGS").is_ok() {
265+
config.shell().warn(
266+
"Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?",
267+
)?;
268+
}
269+
}
270+
CompileMode::Doc { .. } | CompileMode::Doctest => {
271+
if std::env::var("RUSTDOC_FLAGS").is_ok() {
272+
config.shell().warn(
273+
"Cargo does not read `RUSTDOC_FLAGS` environment variable. Did you mean `RUSTDOCFLAGS`?"
274+
)?;
275+
}
276+
}
277+
}
278+
258279
let default_arch_kind = if build_config.requested_target.is_some() {
259280
Kind::Target
260281
} else {

tests/testsuite/rustdocflags.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@ fn rustdocflags_passed_to_rustdoc_through_cargo_test_only_once() {
8585
.env("RUSTDOCFLAGS", "--markdown-no-toc")
8686
.run();
8787
}
88+
89+
#[test]
90+
fn rustdocflags_misspelled() {
91+
let p = project().file("src/main.rs", "fn main() { }").build();
92+
93+
p.cargo("doc")
94+
.env("RUSTDOC_FLAGS", "foo")
95+
.with_stderr_contains("[WARNING] Cargo does not read `RUSTDOC_FLAGS` environment variable. Did you mean `RUSTDOCFLAGS`?")
96+
.run();
97+
}

tests/testsuite/rustflags.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1325,3 +1325,37 @@ fn two_matching_in_config() {
13251325
p1.cargo("run").run();
13261326
p1.cargo("build").with_stderr("[FINISHED] [..]").run();
13271327
}
1328+
1329+
#[test]
1330+
fn env_rustflags_misspelled() {
1331+
let p = project().file("src/main.rs", "fn main() { }").build();
1332+
1333+
for cmd in &["check", "build", "run", "test", "bench"] {
1334+
p.cargo(cmd)
1335+
.env("RUST_FLAGS", "foo")
1336+
.with_stderr_contains("[WARNING] Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?")
1337+
.run();
1338+
}
1339+
}
1340+
1341+
#[test]
1342+
fn env_rustflags_misspelled_build_script() {
1343+
let p = project()
1344+
.file(
1345+
"Cargo.toml",
1346+
r#"
1347+
[package]
1348+
name = "foo"
1349+
version = "0.0.1"
1350+
build = "build.rs"
1351+
"#,
1352+
)
1353+
.file("src/lib.rs", "")
1354+
.file("build.rs", "fn main() { }")
1355+
.build();
1356+
1357+
p.cargo("build")
1358+
.env("RUST_FLAGS", "foo")
1359+
.with_stderr_contains("[WARNING] Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?")
1360+
.run();
1361+
}

0 commit comments

Comments
 (0)