Skip to content

Add support for cargo-args in package metadata #1374

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

Merged
merged 1 commit into from
Apr 22, 2021
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
25 changes: 25 additions & 0 deletions crates/metadata/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ pub struct Metadata {
/// List of command line arguments for `rustdoc`.
#[serde(default)]
rustdoc_args: Vec<String>,

/// List of command line arguments for `cargo`.
///
/// These cannot be a subcommand, they may only be options.
#[serde(default)]
cargo_args: Vec<String>,
}

/// The targets that should be built for a crate.
Expand Down Expand Up @@ -264,6 +270,7 @@ impl Metadata {
}

cargo_args.extend(additional_args.iter().map(|s| s.to_owned()));
cargo_args.extend_from_slice(&self.cargo_args);
cargo_args.push("--".into());
cargo_args.extend_from_slice(&self.rustdoc_args);
cargo_args.extend(rustdoc_args.iter().map(|s| s.to_owned()));
Expand Down Expand Up @@ -336,6 +343,7 @@ mod test_parsing {
targets = [ "x86_64-apple-darwin", "x86_64-pc-windows-msvc" ]
rustc-args = [ "--example-rustc-arg" ]
rustdoc-args = [ "--example-rustdoc-arg" ]
cargo-args = [ "-Zbuild-std" ]
"#;

let metadata = Metadata::from_str(manifest).unwrap();
Expand Down Expand Up @@ -369,6 +377,9 @@ mod test_parsing {
assert_eq!(rustdoc_args[0], "--example-rustdoc-arg".to_owned());
assert_eq!(rustdoc_args[1], "-Z".to_owned());
assert_eq!(rustdoc_args[2], "unstable-options".to_owned());

let cargo_args = metadata.cargo_args;
assert_eq!(cargo_args.as_slice(), &["-Zbuild-std"]);
}

#[test]
Expand Down Expand Up @@ -678,5 +689,19 @@ mod test_calculations {
"--".into(),
];
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);

// cargo flags
let metadata = Metadata {
cargo_args: vec!["-Zbuild-std".into()],
..Metadata::default()
};
let expected_args = vec![
String::from("rustdoc"),
"--lib".into(),
"-Zrustdoc-map".into(),
"-Zbuild-std".into(),
"--".into(),
];
assert_eq!(metadata.cargo_args(&[], &[]), expected_args);
}
}
5 changes: 5 additions & 0 deletions templates/core/Cargo.toml.example
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ rustc-args = ["--example-rustc-arg"]

# Additional `RUSTDOCFLAGS` to set (default: [])
rustdoc-args = ["--example-rustdoc-arg"]

# List of command line arguments for `cargo`.
#
# These cannot be a subcommand, they may only be options.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention that these will be passed to multiple subcommands so need to be generally applicable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that true? I thought we only ever use the rustdoc subcommand.

I can add a disclaimer that we don't guarantee the subcommand and you should make these independent, but I don't know how useful that is in practice - they can't truly be independent because e.g. cargo new won't have any useful options, and I think almost everything else is shared between rustdoc and doc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use cargo metadata too I think.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but we don't pass user-defined args there:

let res = Command::new(workspace, toolchain.cargo())
.args(&["metadata", "--format-version", "1"])
.cd(source_dir)
.log_output(false)
.run_capture()?;

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it helps, the only place we call cargo_args() is for prepare_command:

let cargo_args = metadata.cargo_args(&cargo_args, &rustdoc_flags_extras);

which is only used for doc coverage and actually generating the documentation.

cargo-args = ["-Z", "build-std"]