From 59be30205a7e4c44cec2b092ed88e72e4228cf05 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Sat, 16 Jul 2022 23:16:48 -0600 Subject: [PATCH] Make xtask search for workspace more robust --- nih_plug_xtask/src/lib.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/nih_plug_xtask/src/lib.rs b/nih_plug_xtask/src/lib.rs index 726801ec..c01a69d2 100644 --- a/nih_plug_xtask/src/lib.rs +++ b/nih_plug_xtask/src/lib.rs @@ -117,13 +117,25 @@ pub fn main_with_args(command_name: &str, args: impl IntoIterator } /// Change the current directory into the Cargo workspace's root. +/// +/// This is using a heuristic to find the workspace root, walking up directories +/// from `CARGO_MANIFEST_DIR` until another `Cargo.toml` is found, and assuming +/// that is the active workspace. pub fn chdir_workspace_root() -> Result<()> { let xtask_project_dir = std::env::var("CARGO_MANIFEST_DIR") .context("'$CARGO_MANIFEST_DIR' was not set, are you running this binary directly?")?; - let project_root = Path::new(&xtask_project_dir).parent().context( - "'$CARGO_MANIFEST_DIR' has an unexpected value, are you running this binary directly?", - )?; - std::env::set_current_dir(project_root).context("Could not change to project root directory") + let mut current_dir = Path::new(&xtask_project_dir); + loop { + let maybe_project_root = current_dir.parent().context( + "'$CARGO_MANIFEST_DIR' has an unexpected value, are you running this binary directly?", + )?; + if maybe_project_root.join("Cargo.toml").exists() { + std::env::set_current_dir(maybe_project_root) + .context("Could not change to project root directory")?; + return Ok(()); + } + current_dir = maybe_project_root; + } } /// Build one or more packages using the provided `cargo build` arguments. This should be caleld