Skip to content

Commit

Permalink
build(router_env): obtain workspace member package names from `cargo_…
Browse files Browse the repository at this point in the history
…metadata` more deterministically (#4139)

Co-authored-by: Sampras Lopes <lsampras@pm.me>
  • Loading branch information
SanchithHegde and lsampras committed Mar 20, 2024
1 parent 3653c2c commit 8f7d9fb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 25 deletions.
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.

4 changes: 2 additions & 2 deletions crates/router_env/Cargo.toml
Expand Up @@ -8,7 +8,7 @@ readme = "README.md"
license.workspace = true

[dependencies]
cargo_metadata = "0.15.4"
cargo_metadata = "0.18.1"
config = { version = "0.13.3", features = ["toml"] }
error-stack = "0.3.1"
gethostname = "0.4.3"
Expand All @@ -34,7 +34,7 @@ vergen = { version = "8.2.1", optional = true, features = ["cargo", "git", "git2
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }

[build-dependencies]
cargo_metadata = "0.15.4"
cargo_metadata = "0.18.1"
vergen = { version = "8.2.1", features = ["cargo", "git", "git2", "rustc"], optional = true }

[features]
Expand Down
30 changes: 10 additions & 20 deletions crates/router_env/src/cargo_workspace.rs
Expand Up @@ -14,17 +14,11 @@ pub fn set_cargo_workspace_members_env() {
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 All @@ -35,32 +29,28 @@ pub fn set_cargo_workspace_members_env() {
.expect("Failed to set `CARGO_WORKSPACE_MEMBERS` environment variable");
}

/// 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 Down

0 comments on commit 8f7d9fb

Please sign in to comment.