Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix config args for cargo pgo run #54

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Dev
## Fixes

- Fix passing of `--config` Cargo argument when using `cargo pgo run`.

# 0.2.7 (28. 3. 2024)
## Fixes

Expand Down
15 changes: 11 additions & 4 deletions src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl RunningCargo {
pub fn cargo_command_with_rustflags(
command: CargoCommand,
rustflags: Vec<String>,
mut cargo_args: Vec<String>,
cargo_args: Vec<String>,
) -> anyhow::Result<RunningCargo> {
let mut env = HashMap::default();

Expand All @@ -75,6 +75,8 @@ pub fn cargo_command_with_rustflags(
let supports_config_flag = version_check::is_min_version("1.63.0").unwrap_or(false);
let serialized_rustflags = rustflags.join(" ");

let mut final_cargo_args = vec![];

match (supports_config_flag, std::env::var("RUSTFLAGS")) {
(_, Ok(mut existing_rustflags)) => {
// If RUSTFLAGS was defined explicitly, we want to append to it. These RUSTFLAGS will
Expand All @@ -92,7 +94,7 @@ pub fn cargo_command_with_rustflags(
// If there's no RUSTFLAGS, and Cargo supports `--config`, use it.
// The user might have some flags in their config.toml file(s), and
// `--config build.rustflags` will append to it instead of overwriting it.
cargo_args.push("--config".to_string());
final_cargo_args.push("--config".to_string());

let mut flags = String::from("build.rustflags=[");
for (index, flag) in rustflags.into_iter().enumerate() {
Expand All @@ -102,7 +104,7 @@ pub fn cargo_command_with_rustflags(
flags.push_str(&format!("'{}'", flag));
}
flags.push(']');
cargo_args.push(flags);
final_cargo_args.push(flags);
}
}

Expand All @@ -111,7 +113,12 @@ pub fn cargo_command_with_rustflags(
_ => ReleaseMode::AddRelease,
};

let mut child = cargo_command(command, cargo_args, env, release_mode)?;
// We need to add any new Cargo arguments to the beginning of `cargo_args`, because it could
// end with `--`, in which case the added arguments would be sent to e.g. the executed binary
// instead.
final_cargo_args.extend(cargo_args);

let mut child = cargo_command(command, final_cargo_args, env, release_mode)?;
let stdout = child.stdout.take().unwrap();
Ok(RunningCargo {
child,
Expand Down
21 changes: 21 additions & 0 deletions tests/integration/pgo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,24 @@ rustflags = ["-Ctarget-cpu=native"]

Ok(())
}

#[test]
fn test_run_pass_args_to_cargo() -> anyhow::Result<()> {
let mut project = init_cargo_project()?;
project.file(
"src/main.rs",
r#"
fn main() {
let args = std::env::args().collect::<Vec<_>>();
assert_eq!(args.len(), 3);
assert_eq!(args[1], "arg-for-binary");
assert_eq!(args[2], "foo");
}
"#,
);
project
.run(&["run", "--", "-v", "--", "arg-for-binary", "foo"])?
.assert_ok();

Ok(())
}
Loading