Skip to content

Commit 9f1392a

Browse files
committed
Auto merge of #2896 - RalfJung:cargo-miri-args, r=RalfJung
cargo-miri: fix forwarding arguments to cargo Fixes #2829
2 parents b3ed052 + cba8cd8 commit 9f1392a

File tree

3 files changed

+33
-30
lines changed

3 files changed

+33
-30
lines changed

cargo-miri/src/arg.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ impl<'s, I: Iterator<Item = Cow<'s, str>>> Iterator for ArgSplitFlagValue<'_, I>
4040
if arg == "--" {
4141
// Stop searching at `--`.
4242
self.args = None;
43-
return None;
43+
// But yield the `--` so that it does not get lost!
44+
return Some(Err(Cow::Borrowed("--")));
4445
}
4546
// These branches cannot be merged if we want to avoid the allocation in the `Borrowed` branch.
4647
match &arg {
@@ -79,9 +80,8 @@ impl<'a, I: Iterator<Item = String> + 'a> ArgSplitFlagValue<'a, I> {
7980
) -> impl Iterator<Item = Result<String, String>> + 'a {
8081
ArgSplitFlagValue::new(args.map(Cow::Owned), name).map(|x| {
8182
match x {
82-
Ok(Cow::Owned(s)) => Ok(s),
83-
Err(Cow::Owned(s)) => Err(s),
84-
_ => panic!("iterator converted owned to borrowed"),
83+
Ok(s) => Ok(s.into_owned()),
84+
Err(s) => Err(s.into_owned()),
8585
}
8686
})
8787
}

cargo-miri/src/phases.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,30 +113,17 @@ pub fn phase_cargo_miri(mut args: impl Iterator<Item = String>) {
113113
};
114114
let metadata = get_cargo_metadata();
115115
let mut cmd = cargo();
116-
cmd.arg(cargo_cmd);
117-
118-
// Forward all arguments before `--` other than `--target-dir` and its value to Cargo.
119-
// (We want to *change* the target-dir value, so we must not forward it.)
120-
let mut target_dir = None;
121-
for arg in ArgSplitFlagValue::from_string_iter(&mut args, "--target-dir") {
122-
match arg {
123-
Ok(value) => {
124-
if target_dir.is_some() {
125-
show_error!("`--target-dir` is provided more than once");
126-
}
127-
target_dir = Some(value.into());
128-
}
129-
Err(arg) => {
130-
cmd.arg(arg);
131-
}
132-
}
116+
cmd.arg(&cargo_cmd);
117+
// In nextest we have to also forward the main `verb`.
118+
if cargo_cmd == "nextest" {
119+
cmd.arg(
120+
args.next()
121+
.unwrap_or_else(|| show_error!("`cargo miri nextest` expects a verb (e.g. `run`)")),
122+
);
133123
}
134-
// Detect the target directory if it's not specified via `--target-dir`.
135-
// (`cargo metadata` does not support `--target-dir`, that's why we have to handle this ourselves.)
136-
let target_dir = target_dir.get_or_insert_with(|| metadata.target_directory.clone());
137-
// Set `--target-dir` to `miri` inside the original target directory.
138-
target_dir.push("miri");
139-
cmd.arg("--target-dir").arg(target_dir);
124+
// We set the following flags *before* forwarding more arguments.
125+
// This is needed to fix <https://github.com/rust-lang/miri/issues/2829>: cargo will stop
126+
// interpreting things as flags when it sees the first positional argument.
140127

141128
// Make sure the build target is explicitly set.
142129
// 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>) {
154141
cmd.arg("--config")
155142
.arg(format!("target.'cfg(all())'.runner=[{cargo_miri_path_for_toml}, 'runner']"));
156143

157-
// Forward all further arguments after `--` to cargo.
158-
cmd.arg("--").args(args);
144+
// Set `--target-dir` to `miri` inside the original target directory.
145+
let mut target_dir = match get_arg_flag_value("--target-dir") {
146+
Some(dir) => PathBuf::from(dir),
147+
None => metadata.target_directory.clone().into_std_path_buf(),
148+
};
149+
target_dir.push("miri");
150+
cmd.arg("--target-dir").arg(target_dir);
151+
152+
// *After* we set all the flags that need setting, forward everything else. Make sure to skip
153+
// `--target-dir` (which would otherwise be set twice).
154+
for arg in
155+
ArgSplitFlagValue::from_string_iter(&mut args, "--target-dir").filter_map(Result::err)
156+
{
157+
cmd.arg(arg);
158+
}
159+
// Forward all further arguments (not consumed by `ArgSplitFlagValue`) to cargo.
160+
cmd.args(args);
159161

160162
// Set `RUSTC_WRAPPER` to ourselves. Cargo will prepend that binary to its usual invocation,
161163
// i.e., the first argument is `rustc` -- which is what we use in `main` to distinguish

test-cargo-miri/run-test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ def test_cargo_miri_run():
108108
env={'MIRITESTVAR': "wrongval"}, # changing the env var causes a rebuild (re-runs build.rs),
109109
# so keep it set
110110
)
111+
# This also covers passing arguments without `--`: Cargo will forward unused positional arguments to the program.
111112
test("`cargo miri run` (with arguments and target)",
112-
cargo_miri("run") + ["--bin", "cargo-miri-test", "--", "hello world", '"hello world"', r'he\\llo\"world'],
113+
cargo_miri("run") + ["--bin", "cargo-miri-test", "hello world", '"hello world"', r'he\\llo\"world'],
113114
"run.args.stdout.ref", "run.args.stderr.ref",
114115
)
115116
test("`cargo miri r` (subcrate, no isolation)",

0 commit comments

Comments
 (0)