Skip to content

Commit

Permalink
Auto merge of #6694 - ordovicia:misspell-env-var, r=dwijnand
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Mar 7, 2019
2 parents 780aec2 + 3ca98ad commit 37a4555
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/cargo/ops/cargo_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,27 @@ pub fn compile_ws<'a>(
ref export_dir,
} = *options;

match build_config.mode {
CompileMode::Test
| CompileMode::Build
| CompileMode::Check { .. }
| CompileMode::Bench
| CompileMode::RunCustomBuild => {
if std::env::var("RUST_FLAGS").is_ok() {
config.shell().warn(
"Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?",
)?;
}
}
CompileMode::Doc { .. } | CompileMode::Doctest => {
if std::env::var("RUSTDOC_FLAGS").is_ok() {
config.shell().warn(
"Cargo does not read `RUSTDOC_FLAGS` environment variable. Did you mean `RUSTDOCFLAGS`?"
)?;
}
}
}

let default_arch_kind = if build_config.requested_target.is_some() {
Kind::Target
} else {
Expand Down
10 changes: 10 additions & 0 deletions tests/testsuite/rustdocflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,13 @@ fn rustdocflags_passed_to_rustdoc_through_cargo_test_only_once() {
.env("RUSTDOCFLAGS", "--markdown-no-toc")
.run();
}

#[test]
fn rustdocflags_misspelled() {
let p = project().file("src/main.rs", "fn main() { }").build();

p.cargo("doc")
.env("RUSTDOC_FLAGS", "foo")
.with_stderr_contains("[WARNING] Cargo does not read `RUSTDOC_FLAGS` environment variable. Did you mean `RUSTDOCFLAGS`?")
.run();
}
34 changes: 34 additions & 0 deletions tests/testsuite/rustflags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1325,3 +1325,37 @@ fn two_matching_in_config() {
p1.cargo("run").run();
p1.cargo("build").with_stderr("[FINISHED] [..]").run();
}

#[test]
fn env_rustflags_misspelled() {
let p = project().file("src/main.rs", "fn main() { }").build();

for cmd in &["check", "build", "run", "test", "bench"] {
p.cargo(cmd)
.env("RUST_FLAGS", "foo")
.with_stderr_contains("[WARNING] Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?")
.run();
}
}

#[test]
fn env_rustflags_misspelled_build_script() {
let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
version = "0.0.1"
build = "build.rs"
"#,
)
.file("src/lib.rs", "")
.file("build.rs", "fn main() { }")
.build();

p.cargo("build")
.env("RUST_FLAGS", "foo")
.with_stderr_contains("[WARNING] Cargo does not read `RUST_FLAGS` environment variable. Did you mean `RUSTFLAGS`?")
.run();
}

0 comments on commit 37a4555

Please sign in to comment.