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(router_env): obtain workspace member package names from cargo_metadata more deterministically #4227

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
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading