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

build: obtain workspace member package names from cargo_metadata more deterministically #84

Merged
merged 1 commit into from
Mar 21, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ criterion = "0.5.1"
axum-test = "14.2.2"

[build-dependencies]
cargo_metadata = "0.15.4"
cargo_metadata = "0.18.1"


[[bin]]
Expand Down
30 changes: 10 additions & 20 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
mod cargo_workspace {
/// Verify that the cargo metadata workspace members format matches that expected by
/// Verify that the cargo metadata workspace packages format matches that expected by
/// [`set_cargo_workspace_members_env`] to set the `CARGO_WORKSPACE_MEMBERS` environment variable.
///
/// This function should be typically called within build scripts, before the
/// [`set_cargo_workspace_members_env`] function is called.
///
/// # Panics
///
/// Panics if running the `cargo metadata` command fails, or if the workspace members package ID
/// format cannot be determined.
/// Panics if running the `cargo metadata` command fails, or if the workspace member package names
/// cannot be determined.
pub fn verify_cargo_metadata_format() {
#[allow(clippy::expect_used)]
let metadata = cargo_metadata::MetadataCommand::new()
.exec()
.expect("Failed to obtain cargo metadata");
let workspace_members = metadata.workspace_members;

let package_id_entry_prefix =
format!("{} {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
assert!(
workspace_members
metadata
.workspace_packages()
.iter()
.any(|package_id| package_id.repr.starts_with(&package_id_entry_prefix)),
"Unknown workspace members package ID format. \
Please run `cargo metadata --format-version=1 | jq '.workspace_members'` and update this \
build script to match the updated package ID format."
.any(|package| package.name == env!("CARGO_PKG_NAME")),
"Unable to determine workspace member package names from `cargo metadata`"
);
}

Expand All @@ -44,17 +40,11 @@ mod cargo_workspace {
let metadata = cargo_metadata::MetadataCommand::new()
.exec()
.expect("Failed to obtain cargo metadata");
let workspace_members = metadata.workspace_members;

let workspace_members = workspace_members
let workspace_members = metadata
.workspace_packages()
.iter()
.map(|package_id| {
package_id
.repr
.split_once(' ')
.expect("Unknown cargo metadata package ID format")
.0
})
.map(|package| package.name.as_str())
.collect::<Vec<_>>()
.join(",");

Expand Down