From 821459d337259c418f2ce815c5b9a89519b75c8e Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Mon, 9 Sep 2024 15:30:39 -0700 Subject: [PATCH 01/11] Fetch miri flags from cargo config --- cargo-miri/src/phases.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 3743446e27..6449d20bf1 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -650,6 +650,15 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner eprintln!("Trying seed: {seed}"); cmd.arg(format!("-Zmiri-seed={seed}")); } +<<<<<<< HEAD +======= + } + + // Respect flags. + if let Some(flags) = get_miriflags() { + cmd.args(flags); + } +>>>>>>> 7e32c0f90 (Fetch miri flags from cargo config) // Then pass binary arguments. cmd.arg("--"); @@ -691,6 +700,27 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner }); } +fn get_miriflags() -> Option> { + // Fetch miri flags from cargo config. + let mut cmd = cargo(); + cmd.args(["-Zunstable-options", "config", "get", "miri.flags", "--format=json-value"]); + let output = cmd.output().expect("failed to run `cargo config`"); + let config_miriflags = + str::from_utf8(&output.stdout).expect("failed to get `cargo config` output"); + + // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. + // If MIRIFLAGS is present, flags from cargo config are ignored. + // This matches cargo behavior for RUSTFLAGS. + if let Ok(a) = env::var("MIRIFLAGS") { + // This code is taken from `RUSTFLAGS` handling in cargo. + Some(a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()) + } else if let Ok(args) = serde_json::from_str::>(config_miriflags) { + Some(args) + } else { + None + } +} + pub fn phase_rustdoc(mut args: impl Iterator) { let verbose = env::var("MIRI_VERBOSE") .map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer")); From 8273896667953db48b0661a5b684c7a894512dd6 Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Mon, 9 Sep 2024 15:31:02 -0700 Subject: [PATCH 02/11] Add tests for fetching flags from from cargo config --- test-cargo-miri/run-test.py | 44 +++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/test-cargo-miri/run-test.py b/test-cargo-miri/run-test.py index 5b77092979..161aa5d746 100755 --- a/test-cargo-miri/run-test.py +++ b/test-cargo-miri/run-test.py @@ -106,6 +106,12 @@ def test_no_rebuild(name, cmd, env=None): fail("Something was being rebuilt when it should not be (or we got no output)") def test_cargo_miri_run(): + # Try to remove cargo config to avoid unexpected settings + try: + os.remove('.cargo/config.toml') + except OSError: + pass + test("`cargo miri run` (no isolation)", cargo_miri("run"), "run.default.stdout.ref", "run.default.stderr.ref", @@ -115,6 +121,44 @@ def test_cargo_miri_run(): 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence }, ) + + # Create a config with isolation disabled + try: + os.mkdir('.cargo') + except OSError: + pass + + with open(".cargo/config.toml", "w") as f: + f.write('[miri]\nflags = ["-Zmiri-disable-isolation"]') + + # Testing miri.flags are taken in account + test("`cargo miri run` (no isolation, set from cargo config)", + cargo_miri("run"), + "run.default.stdout.ref", "run.default.stderr.ref", + stdin=b'12\n21\n', + env={ + 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence + }, + ) + + # Create an empty config + with open(".cargo/config.toml", "w") as f: + f.write('[miri]\nflags = [""]') + + # Check that MIRIFLAGS envar has higher precedence tha cargo config + test("`cargo miri run` (no isolation, ignoring cargo config)", + cargo_miri("run"), + "run.default.stdout.ref", "run.default.stderr.ref", + stdin=b'12\n21\n', + env={ + 'MIRIFLAGS': "-Zmiri-disable-isolation", + 'MIRITESTVAR': "wrongval", # make sure the build.rs value takes precedence + }, + ) + + # Cleaning up config after all config-related tests + os.remove('.cargo/config.toml') + # Special test: run it again *without* `-q` to make sure nothing is being rebuilt (Miri issue #1722) test_no_rebuild("`cargo miri run` (no rebuild)", cargo_miri("run", quiet=False) + ["--", ""], From 8959e9f49f33208f41dc0dbbd8494262af1b4d1d Mon Sep 17 00:00:00 2001 From: Aleksei Trifonov Date: Mon, 9 Sep 2024 15:31:22 -0700 Subject: [PATCH 03/11] Add info on setting miriflags in cargo config --- README.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 72555e8c40..e9fad25e00 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,8 @@ their name. You can pass [flags][miri-flags] to Miri via `MIRIFLAGS`. For example, `MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run` runs the program -without checking the aliasing of references. +without checking the aliasing of references. Also, you can set the `miri.flags` +setting in [cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html). When compiling code via `cargo miri`, the `cfg(miri)` config flag is set for code that will be interpreted under Miri. You can use this to ignore test cases that fail @@ -275,7 +276,22 @@ Try running `cargo miri clean`. [miri-flags]: #miri--z-flags-and-environment-variables Miri adds its own set of `-Z` flags, which are usually set via the `MIRIFLAGS` -environment variable. We first document the most relevant and most commonly used flags: +environment variable or using the `miri.flags` setting in +[cargo configuration](https://doc.rust-lang.org/cargo/reference/config.html), +they are mutually exclusive with priority of `MIRIFLAGS` environment variable: + +```bash +MIRIFLAGS="-Zmiri-disable-stacked-borrows" cargo miri run +``` + +```toml +# .cargo/config.toml + +[miri] +flags = ["-Zmiri-disable-isolation", "-Zmiri-report-progress"] +``` + +We first document the most relevant and most commonly used flags: * `-Zmiri-address-reuse-rate=` changes the probability that a freed *non-stack* allocation will be added to the pool for address reuse, and the probability that a new *non-stack* allocation From d0f39d2ece6931bb2cf11124febde3556dff579c Mon Sep 17 00:00:00 2001 From: badumbatish Date: Mon, 9 Sep 2024 15:31:31 -0700 Subject: [PATCH 04/11] Move get_miri_flags to util.rs --- cargo-miri/src/phases.rs | 37 +++---------------------------------- cargo-miri/src/util.rs | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 34 deletions(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 6449d20bf1..53aa6caef1 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -640,25 +640,15 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner cmd.arg(arg); } } - // Respect `MIRIFLAGS`. - if let Ok(a) = env::var("MIRIFLAGS") { - let args = flagsplit(&a); - cmd.args(args); + // Respect miriflags. + if let Some(flags) = get_miriflags() { + cmd.args(flags); } // Set the current seed. if let Some(seed) = seed { eprintln!("Trying seed: {seed}"); cmd.arg(format!("-Zmiri-seed={seed}")); } -<<<<<<< HEAD -======= - } - - // Respect flags. - if let Some(flags) = get_miriflags() { - cmd.args(flags); - } ->>>>>>> 7e32c0f90 (Fetch miri flags from cargo config) // Then pass binary arguments. cmd.arg("--"); @@ -700,27 +690,6 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner }); } -fn get_miriflags() -> Option> { - // Fetch miri flags from cargo config. - let mut cmd = cargo(); - cmd.args(["-Zunstable-options", "config", "get", "miri.flags", "--format=json-value"]); - let output = cmd.output().expect("failed to run `cargo config`"); - let config_miriflags = - str::from_utf8(&output.stdout).expect("failed to get `cargo config` output"); - - // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. - // If MIRIFLAGS is present, flags from cargo config are ignored. - // This matches cargo behavior for RUSTFLAGS. - if let Ok(a) = env::var("MIRIFLAGS") { - // This code is taken from `RUSTFLAGS` handling in cargo. - Some(a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect()) - } else if let Ok(args) = serde_json::from_str::>(config_miriflags) { - Some(args) - } else { - None - } -} - pub fn phase_rustdoc(mut args: impl Iterator) { let verbose = env::var("MIRI_VERBOSE") .map_or(0, |verbose| verbose.parse().expect("verbosity flag must be an integer")); diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index 56f38de8de..52467ff210 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -88,6 +88,29 @@ pub fn escape_for_toml(s: &str) -> String { format!("\"{s}\"") } +pub fn get_miriflags() -> Vec { + // TODO: I quite not understand what Carl Jung means by Oh and please add a link to https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags. + // I guess we don't support the target.rustflags part yet? (That's okay but should be mentioned in a comment.) + // + // Fetch miri flags from cargo config. + let mut cmd = cargo(); + cmd.args(["-Zunstable-options", "config", "get", "miri.flags", "--format=json-value"]); + let output = cmd.output().expect("failed to run `cargo config`"); + let config_miriflags = + std::str::from_utf8(&output.stdout).expect("failed to get `cargo config` output"); + + // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. + // If MIRIFLAGS is present, flags from cargo config are ignored. + // This matches cargo behavior for RUSTFLAGS. + if let Ok(a) = env::var("MIRIFLAGS") { + // This code is taken from `RUSTFLAGS` handling in cargo. + a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() + } else if let Ok(args) = serde_json::from_str::>(config_miriflags) { + args + } else { + Vec::default() + } +} /// Returns the path to the `miri` binary pub fn find_miri() -> PathBuf { if let Some(path) = env::var_os("MIRI") { From a8b44f3a4d963c4f6889246ccb6f80a63d64ce80 Mon Sep 17 00:00:00 2001 From: badumbatish Date: Mon, 9 Sep 2024 15:31:48 -0700 Subject: [PATCH 05/11] Update pseudo-flag and better comments --- test-cargo-miri/run-test.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-cargo-miri/run-test.py b/test-cargo-miri/run-test.py index 161aa5d746..d4db70f3b8 100755 --- a/test-cargo-miri/run-test.py +++ b/test-cargo-miri/run-test.py @@ -141,9 +141,9 @@ def test_cargo_miri_run(): }, ) - # Create an empty config + # Create an invalid config with open(".cargo/config.toml", "w") as f: - f.write('[miri]\nflags = [""]') + f.write('[miri]\nflags = ["-Zmiri-there-is-no-such-flag"]') # Check that MIRIFLAGS envar has higher precedence tha cargo config test("`cargo miri run` (no isolation, ignoring cargo config)", From 27347455b6cb25c8f6ee7cbe05c6b4334e5beac0 Mon Sep 17 00:00:00 2001 From: badumbatish Date: Mon, 9 Sep 2024 15:32:02 -0700 Subject: [PATCH 06/11] Fix build warnings of unused --- cargo-miri/src/phases.rs | 4 +--- cargo-miri/src/util.rs | 5 ----- 2 files changed, 1 insertion(+), 8 deletions(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 53aa6caef1..3694e715fd 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -641,9 +641,7 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner } } // Respect miriflags. - if let Some(flags) = get_miriflags() { - cmd.args(flags); - } + cmd.args(get_miriflags()); // Set the current seed. if let Some(seed) = seed { eprintln!("Trying seed: {seed}"); diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index 52467ff210..90a9cf1cdc 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -141,11 +141,6 @@ pub fn cargo() -> Command { Command::new(env::var_os("CARGO").unwrap_or_else(|| OsString::from("cargo"))) } -pub fn flagsplit(flags: &str) -> Vec { - // This code is taken from `RUSTFLAGS` handling in cargo. - flags.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() -} - /// Execute the `Command`, where possible by replacing the current process with a new process /// described by the `Command`. Then exit this process with the exit code of the new process. pub fn exec(mut cmd: Command) -> ! { From f69a6512833ba44a15edbb2b0487d50f68dcaa47 Mon Sep 17 00:00:00 2001 From: badumbatish Date: Mon, 9 Sep 2024 15:48:23 -0700 Subject: [PATCH 07/11] FIX: Fix clippy manual_unwrap_or_default --- cargo-miri/src/util.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index 90a9cf1cdc..a23d7ab80f 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -105,10 +105,8 @@ pub fn get_miriflags() -> Vec { if let Ok(a) = env::var("MIRIFLAGS") { // This code is taken from `RUSTFLAGS` handling in cargo. a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() - } else if let Ok(args) = serde_json::from_str::>(config_miriflags) { - args } else { - Vec::default() + serde_json::from_str::>(config_miriflags).unwrap_or_default() } } /// Returns the path to the `miri` binary From 2920ebae564b67a578fe7867f58b8b91e3c1000a Mon Sep 17 00:00:00 2001 From: badumbatish Date: Mon, 9 Sep 2024 23:45:24 -0700 Subject: [PATCH 08/11] FEAT: Add --config option for miri flags --- cargo-miri/src/phases.rs | 1 + cargo-miri/src/util.rs | 31 ++++++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 3694e715fd..72d28cee81 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -181,6 +181,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator) { let target_dir = get_target_dir(&metadata); cmd.arg("--target-dir").arg(target_dir); + cmd.args(get_miriflags()); // Store many-seeds argument. let mut many_seeds = None; // *After* we set all the flags that need setting, forward everything else. Make sure to skip diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index a23d7ab80f..84120b01e2 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -88,6 +88,12 @@ pub fn escape_for_toml(s: &str) -> String { format!("\"{s}\"") } +pub fn flagsplit(flags: &str) -> Vec { + // This code is taken from `RUSTFLAGS` handling in cargo. + // Taken from miri-script util.rs + flags.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() +} + pub fn get_miriflags() -> Vec { // TODO: I quite not understand what Carl Jung means by Oh and please add a link to https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags. // I guess we don't support the target.rustflags part yet? (That's okay but should be mentioned in a comment.) @@ -102,10 +108,33 @@ pub fn get_miriflags() -> Vec { // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. // If MIRIFLAGS is present, flags from cargo config are ignored. // This matches cargo behavior for RUSTFLAGS. - if let Ok(a) = env::var("MIRIFLAGS") { + // + // Strategy: (1) check pseudo var CARGO_ENCODED_MIRIFLAGS first (this is only set after we check for --config + // in the cargo_dash_dash in the if else) + // + // if CARGO_ENCODED_MIRIFLAGS doesn't exist, we check in --config (2) + // if --config doesn't exist, we check offical env var MIRIFLAGS (3) + // + // if MIRIFLAGS is non-existent, we then check for toml (4) + let cargo_dash_dash_config = cargo_extra_flags(); + if let Ok(cargo_encoded_miri_flags) = env::var("CARGO_ENCODED_MIRIFLAGS") { + // (1) + flagsplit(cargo_encoded_miri_flags.as_str()) + } else if cargo_dash_dash_config.contains(&"miri".to_string()) { + // (2) + let miri_flags_vec = cargo_dash_dash_config + .into_iter() + .filter(|arg| arg.contains(&"miri".to_string())) + .collect::>(); + let miri_flags_string = miri_flags_vec.join(" "); + env::set_var("CARGO_ENCODED_MIRIFLAGS", miri_flags_string); + miri_flags_vec + } else if let Ok(a) = env::var("MIRIFLAGS") { + // (3) // This code is taken from `RUSTFLAGS` handling in cargo. a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() } else { + // (4) serde_json::from_str::>(config_miriflags).unwrap_or_default() } } From 08668c72d7fc141339b9975104503783d69d10f0 Mon Sep 17 00:00:00 2001 From: badumbatish Date: Tue, 10 Sep 2024 01:51:22 -0700 Subject: [PATCH 09/11] FIX: Add debug info, ready to debug --- cargo-miri/src/phases.rs | 2 ++ cargo-miri/src/util.rs | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 72d28cee81..83ea01750f 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -181,6 +181,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator) { let target_dir = get_target_dir(&metadata); cmd.arg("--target-dir").arg(target_dir); + eprintln!("Getting miri flags in phase_cargo_miri"); cmd.args(get_miriflags()); // Store many-seeds argument. let mut many_seeds = None; @@ -642,6 +643,7 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner } } // Respect miriflags. + eprintln!("Get miri flags in phase_runner"); cmd.args(get_miriflags()); // Set the current seed. if let Some(seed) = seed { diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index 84120b01e2..1c0b140b49 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -116,15 +116,17 @@ pub fn get_miriflags() -> Vec { // if --config doesn't exist, we check offical env var MIRIFLAGS (3) // // if MIRIFLAGS is non-existent, we then check for toml (4) - let cargo_dash_dash_config = cargo_extra_flags(); if let Ok(cargo_encoded_miri_flags) = env::var("CARGO_ENCODED_MIRIFLAGS") { // (1) + eprintln!("Choice 1"); flagsplit(cargo_encoded_miri_flags.as_str()) - } else if cargo_dash_dash_config.contains(&"miri".to_string()) { + } else if cargo_extra_flags().iter().any(|s| s.contains(&"-Zmiri".to_string())) { // (2) + eprintln!("Choice 2"); + let cargo_dash_dash_config = cargo_extra_flags(); let miri_flags_vec = cargo_dash_dash_config .into_iter() - .filter(|arg| arg.contains(&"miri".to_string())) + .filter(|arg| arg.contains(&"-Zmiri".to_string())) .collect::>(); let miri_flags_string = miri_flags_vec.join(" "); env::set_var("CARGO_ENCODED_MIRIFLAGS", miri_flags_string); @@ -132,9 +134,12 @@ pub fn get_miriflags() -> Vec { } else if let Ok(a) = env::var("MIRIFLAGS") { // (3) // This code is taken from `RUSTFLAGS` handling in cargo. + eprintln!("Choice 3"); + eprintln!("{}", a); a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() } else { // (4) + eprintln!("Choice 4"); serde_json::from_str::>(config_miriflags).unwrap_or_default() } } From 3502a944b4abf43e34f00dbc4854587c70c3ea87 Mon Sep 17 00:00:00 2001 From: badumbatish Date: Tue, 10 Sep 2024 02:07:50 -0700 Subject: [PATCH 10/11] Fix bug in cargo miri phase related to choice 3 --- cargo-miri/src/phases.rs | 4 ++-- cargo-miri/src/util.rs | 40 ++++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index 83ea01750f..ca5ffaf8f5 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -182,7 +182,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator) { cmd.arg("--target-dir").arg(target_dir); eprintln!("Getting miri flags in phase_cargo_miri"); - cmd.args(get_miriflags()); + cmd.args(get_miriflags_cargo_mini()); // Store many-seeds argument. let mut many_seeds = None; // *After* we set all the flags that need setting, forward everything else. Make sure to skip @@ -644,7 +644,7 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner } // Respect miriflags. eprintln!("Get miri flags in phase_runner"); - cmd.args(get_miriflags()); + cmd.args(get_miriflags_runner()); // Set the current seed. if let Some(seed) = seed { eprintln!("Trying seed: {seed}"); diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index 1c0b140b49..5987f587ea 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -94,17 +94,7 @@ pub fn flagsplit(flags: &str) -> Vec { flags.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() } -pub fn get_miriflags() -> Vec { - // TODO: I quite not understand what Carl Jung means by Oh and please add a link to https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags. - // I guess we don't support the target.rustflags part yet? (That's okay but should be mentioned in a comment.) - // - // Fetch miri flags from cargo config. - let mut cmd = cargo(); - cmd.args(["-Zunstable-options", "config", "get", "miri.flags", "--format=json-value"]); - let output = cmd.output().expect("failed to run `cargo config`"); - let config_miriflags = - std::str::from_utf8(&output.stdout).expect("failed to get `cargo config` output"); - +pub fn get_miriflags_cargo_mini() -> Vec { // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. // If MIRIFLAGS is present, flags from cargo config are ignored. // This matches cargo behavior for RUSTFLAGS. @@ -131,7 +121,33 @@ pub fn get_miriflags() -> Vec { let miri_flags_string = miri_flags_vec.join(" "); env::set_var("CARGO_ENCODED_MIRIFLAGS", miri_flags_string); miri_flags_vec - } else if let Ok(a) = env::var("MIRIFLAGS") { + } else { + Vec::default() + } +} +pub fn get_miriflags_runner() -> Vec { + // TODO: I quite not understand what Carl Jung means by Oh and please add a link to https://doc.rust-lang.org/cargo/reference/config.html#buildrustflags. + // I guess we don't support the target.rustflags part yet? (That's okay but should be mentioned in a comment.) + // + // Fetch miri flags from cargo config. + let mut cmd = cargo(); + cmd.args(["-Zunstable-options", "config", "get", "miri.flags", "--format=json-value"]); + let output = cmd.output().expect("failed to run `cargo config`"); + let config_miriflags = + std::str::from_utf8(&output.stdout).expect("failed to get `cargo config` output"); + + // Respect `MIRIFLAGS` and `miri.flags` setting in cargo config. + // If MIRIFLAGS is present, flags from cargo config are ignored. + // This matches cargo behavior for RUSTFLAGS. + // + // Strategy: (1) check pseudo var CARGO_ENCODED_MIRIFLAGS first (this is only set after we check for --config + // in the cargo_dash_dash in the if else) + // + // if CARGO_ENCODED_MIRIFLAGS doesn't exist, we check in --config (2) + // if --config doesn't exist, we check offical env var MIRIFLAGS (3) + // + // if MIRIFLAGS is non-existent, we then check for toml (4) + if let Ok(a) = env::var("MIRIFLAGS") { // (3) // This code is taken from `RUSTFLAGS` handling in cargo. eprintln!("Choice 3"); From 914bda3d31fa1cc39ef809dff639e46fe1001ed8 Mon Sep 17 00:00:00 2001 From: badumbatish Date: Wed, 11 Sep 2024 00:50:13 -0700 Subject: [PATCH 11/11] DOCS: Comment out eprintln! after debug --- cargo-miri/src/phases.rs | 4 ++-- cargo-miri/src/util.rs | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/cargo-miri/src/phases.rs b/cargo-miri/src/phases.rs index ca5ffaf8f5..f72e7313f0 100644 --- a/cargo-miri/src/phases.rs +++ b/cargo-miri/src/phases.rs @@ -181,7 +181,7 @@ pub fn phase_cargo_miri(mut args: impl Iterator) { let target_dir = get_target_dir(&metadata); cmd.arg("--target-dir").arg(target_dir); - eprintln!("Getting miri flags in phase_cargo_miri"); + // eprintln!("Getting miri flags in phase_cargo_miri"); cmd.args(get_miriflags_cargo_mini()); // Store many-seeds argument. let mut many_seeds = None; @@ -643,7 +643,7 @@ pub fn phase_runner(mut binary_args: impl Iterator, phase: Runner } } // Respect miriflags. - eprintln!("Get miri flags in phase_runner"); + // eprintln!("Get miri flags in phase_runner"); cmd.args(get_miriflags_runner()); // Set the current seed. if let Some(seed) = seed { diff --git a/cargo-miri/src/util.rs b/cargo-miri/src/util.rs index 5987f587ea..6ded21b30e 100644 --- a/cargo-miri/src/util.rs +++ b/cargo-miri/src/util.rs @@ -108,11 +108,11 @@ pub fn get_miriflags_cargo_mini() -> Vec { // if MIRIFLAGS is non-existent, we then check for toml (4) if let Ok(cargo_encoded_miri_flags) = env::var("CARGO_ENCODED_MIRIFLAGS") { // (1) - eprintln!("Choice 1"); + // eprintln!("Choice 1"); flagsplit(cargo_encoded_miri_flags.as_str()) } else if cargo_extra_flags().iter().any(|s| s.contains(&"-Zmiri".to_string())) { // (2) - eprintln!("Choice 2"); + // eprintln!("Choice 2"); let cargo_dash_dash_config = cargo_extra_flags(); let miri_flags_vec = cargo_dash_dash_config .into_iter() @@ -150,12 +150,12 @@ pub fn get_miriflags_runner() -> Vec { if let Ok(a) = env::var("MIRIFLAGS") { // (3) // This code is taken from `RUSTFLAGS` handling in cargo. - eprintln!("Choice 3"); - eprintln!("{}", a); + // eprintln!("Choice 3"); + // eprintln!("{}", a); a.split(' ').map(str::trim).filter(|s| !s.is_empty()).map(str::to_string).collect() } else { // (4) - eprintln!("Choice 4"); + // eprintln!("Choice 4"); serde_json::from_str::>(config_miriflags).unwrap_or_default() } }