diff --git a/nih_plug_xtask/src/lib.rs b/nih_plug_xtask/src/lib.rs index c01a69d2..50158a10 100644 --- a/nih_plug_xtask/src/lib.rs +++ b/nih_plug_xtask/src/lib.rs @@ -124,18 +124,28 @@ pub fn main_with_args(command_name: &str, args: impl IntoIterator 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 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(()); + let project_root = Path::new(&xtask_project_dir).parent().context( + "'$CARGO_MANIFEST_DIR' has an unexpected value, are you running this binary directly?", + )?; + + // If `project_root` is not actually the project's root because this xtask binary's `Cargo.toml` + // file is in a sub-subdirectory, then we'll walk up the directory stack until we hopefully find + // it. + let project_root = if project_root.join("Cargo.toml").exists() { + project_root + } else { + let mut project_root_candidate = project_root; + loop { + project_root_candidate = project_root_candidate + .parent() + .context("Reached the file system root without finding a parent Cargo.toml file")?; + if project_root_candidate.join("Cargo.toml").exists() { + break project_root_candidate; + } } - current_dir = maybe_project_root; - } + }; + + std::env::set_current_dir(project_root).context("Could not change to project root directory") } /// Build one or more packages using the provided `cargo build` arguments. This should be caleld