Skip to content

Commit

Permalink
Clear RUSTFLAGS when building workspace metadata entries
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Jan 3, 2023
1 parent c8d0256 commit a5f0d4f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
47 changes: 46 additions & 1 deletion cargo-dylint/tests/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use assert_cmd::prelude::*;
use dylint_internal::packaging::isolate;
use dylint_internal::{env, packaging::isolate};
use predicates::prelude::*;
use std::{fs::OpenOptions, io::Write};
use tempfile::{tempdir, tempdir_in};
Expand Down Expand Up @@ -107,3 +107,48 @@ path = "../../examples/general/nonexistent_library"
.failure()
.stderr(predicate::str::contains("No paths matched"));
}

#[test]
fn rustflags_change() {
let tempdir = tempdir_in(".").unwrap();

dylint_internal::cargo::init("package `rustflags_change_test`", false)
.current_dir(&tempdir)
.args(["--name", "rustflags_change_test"])
.success()
.unwrap();

isolate(tempdir.path()).unwrap();

let mut file = OpenOptions::new()
.write(true)
.append(true)
.open(tempdir.path().join("Cargo.toml"))
.unwrap();

write!(
file,
r#"
[[workspace.metadata.dylint.libraries]]
path = "../../examples/general/crate_wide_allow"
"#
)
.unwrap();

std::process::Command::cargo_bin("cargo-dylint")
.unwrap()
.current_dir(&tempdir)
.args(["dylint", "--all"])
.assert()
.success()
.stderr(predicate::str::contains("Compiling"));

std::process::Command::cargo_bin("cargo-dylint")
.unwrap()
.current_dir(&tempdir)
.env(env::RUSTFLAGS, "--verbose")
.args(["dylint", "--all"])
.assert()
.success()
.stderr(predicate::str::contains("Compiling").not());
}
5 changes: 4 additions & 1 deletion dylint/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use cargo::{
util::Config,
};
use cargo_metadata::{Error, Metadata, MetadataCommand};
use dylint_internal::rustup::SanitizeEnvironment;
use dylint_internal::{env, rustup::SanitizeEnvironment};
use glob::glob;
use if_chain::if_chain;
use serde::Deserialize;
Expand Down Expand Up @@ -314,11 +314,14 @@ fn package_library_path(
let target_dir = target_dir(metadata, package_root, package_id)?;

if !opts.no_build {
// smoelius: Clear `RUSTFLAGS` so that changes to it do not cause workspace metadata entries
// to be rebuilt.
dylint_internal::cargo::build(
&format!("workspace metadata entry `{}`", package_id.name()),
opts.quiet,
)
.sanitize_environment()
.env_remove(env::RUSTFLAGS)
.current_dir(package_root)
.args(["--release", "--target-dir", &target_dir.to_string_lossy()])
.success()?;
Expand Down
1 change: 1 addition & 0 deletions examples/general/crate_wide_allow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ mod test {
// smoelius: The real reason this test is slow is that setting `RUSTFLAGS` causes the metadata
// entries to be rebuilt. Running `clippy` once and passing `--no-build` thereafter avoids this
// problem.
// smoelius: Metadata entries are no longer rebuilt when `RUSTFLAGS` changes.

fn test(rustflags: &str, assert: impl Fn(Assert) -> Assert) {
let _lock = MUTEX.lock().unwrap();
Expand Down

0 comments on commit a5f0d4f

Please sign in to comment.