Auto merge of #2896 - RalfJung:cargo-miri-args, r=RalfJung

cargo-miri: fix forwarding arguments to cargo

Fixes https://github.com/rust-lang/miri/issues/2829
This commit is contained in:
bors 2023-05-13 17:04:41 +00:00
commit 68941d4f68
3 changed files with 33 additions and 30 deletions

View File

@ -40,7 +40,8 @@ impl<'s, I: Iterator<Item = Cow<'s, str>>> Iterator for ArgSplitFlagValue<'_, I>
if arg == "--" { if arg == "--" {
// Stop searching at `--`. // Stop searching at `--`.
self.args = None; self.args = None;
return None; // But yield the `--` so that it does not get lost!
return Some(Err(Cow::Borrowed("--")));
} }
// These branches cannot be merged if we want to avoid the allocation in the `Borrowed` branch. // These branches cannot be merged if we want to avoid the allocation in the `Borrowed` branch.
match &arg { match &arg {
@ -79,9 +80,8 @@ impl<'a, I: Iterator<Item = String> + 'a> ArgSplitFlagValue<'a, I> {
) -> impl Iterator<Item = Result<String, String>> + 'a { ) -> impl Iterator<Item = Result<String, String>> + 'a {
ArgSplitFlagValue::new(args.map(Cow::Owned), name).map(|x| { ArgSplitFlagValue::new(args.map(Cow::Owned), name).map(|x| {
match x { match x {
Ok(Cow::Owned(s)) => Ok(s), Ok(s) => Ok(s.into_owned()),
Err(Cow::Owned(s)) => Err(s), Err(s) => Err(s.into_owned()),
_ => panic!("iterator converted owned to borrowed"),
} }
}) })
} }

View File

@ -113,30 +113,17 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
}; };
let metadata = get_cargo_metadata(); let metadata = get_cargo_metadata();
let mut cmd = cargo(); let mut cmd = cargo();
cmd.arg(cargo_cmd); cmd.arg(&cargo_cmd);
// In nextest we have to also forward the main `verb`.
// Forward all arguments before `--` other than `--target-dir` and its value to Cargo. if cargo_cmd == "nextest" {
// (We want to *change* the target-dir value, so we must not forward it.) cmd.arg(
let mut target_dir = None; args.next()
for arg in ArgSplitFlagValue::from_string_iter(&mut args, "--target-dir") { .unwrap_or_else(|| show_error!("`cargo miri nextest` expects a verb (e.g. `run`)")),
match arg { );
Ok(value) => {
if target_dir.is_some() {
show_error!("`--target-dir` is provided more than once");
}
target_dir = Some(value.into());
}
Err(arg) => {
cmd.arg(arg);
}
}
} }
// Detect the target directory if it's not specified via `--target-dir`. // We set the following flags *before* forwarding more arguments.
// (`cargo metadata` does not support `--target-dir`, that's why we have to handle this ourselves.) // This is needed to fix <https://github.com/rust-lang/miri/issues/2829>: cargo will stop
let target_dir = target_dir.get_or_insert_with(|| metadata.target_directory.clone()); // interpreting things as flags when it sees the first positional argument.
// Set `--target-dir` to `miri` inside the original target directory.
target_dir.push("miri");
cmd.arg("--target-dir").arg(target_dir);
// Make sure the build target is explicitly set. // Make sure the build target is explicitly set.
// This is needed to make the `target.runner` settings do something, // This is needed to make the `target.runner` settings do something,
@ -154,8 +141,23 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
cmd.arg("--config") cmd.arg("--config")
.arg(format!("target.'cfg(all())'.runner=[{cargo_miri_path_for_toml}, 'runner']")); .arg(format!("target.'cfg(all())'.runner=[{cargo_miri_path_for_toml}, 'runner']"));
// Forward all further arguments after `--` to cargo. // Set `--target-dir` to `miri` inside the original target directory.
cmd.arg("--").args(args); let mut target_dir = match get_arg_flag_value("--target-dir") {
Some(dir) => PathBuf::from(dir),
None => metadata.target_directory.clone().into_std_path_buf(),
};
target_dir.push("miri");
cmd.arg("--target-dir").arg(target_dir);
// *After* we set all the flags that need setting, forward everything else. Make sure to skip
// `--target-dir` (which would otherwise be set twice).
for arg in
ArgSplitFlagValue::from_string_iter(&mut args, "--target-dir").filter_map(Result::err)
{
cmd.arg(arg);
}
// Forward all further arguments (not consumed by `ArgSplitFlagValue`) to cargo.
cmd.args(args);
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation, // Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish // i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish

View File

@ -108,8 +108,9 @@ def test_cargo_miri_run():
env={'MIRITESTVAR': "wrongval"}, # changing the env var causes a rebuild (re-runs build.rs), env={'MIRITESTVAR': "wrongval"}, # changing the env var causes a rebuild (re-runs build.rs),
# so keep it set # so keep it set
) )
# This also covers passing arguments without `--`: Cargo will forward unused positional arguments to the program.
test("`cargo miri run` (with arguments and target)", test("`cargo miri run` (with arguments and target)",
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"', r'he\\llo\"world'], cargo_miri("run") + ["--bin", "cargo-miri-test", "hello world", '"hello world"', r'he\\llo\"world'],
"run.args.stdout.ref", "run.args.stderr.ref", "run.args.stdout.ref", "run.args.stderr.ref",
) )
test("`cargo miri r` (subcrate, no isolation)", test("`cargo miri r` (subcrate, no isolation)",