For cargo_nih_plug workspace root detection
This is a lot simpler now and it simply considers the topmost ancestor directory of either `$CARGO_MANIFEST_DIR` or the current working directory containing a `Cargo.toml` file.
This commit is contained in:
parent
148fc3379b
commit
08ddd9c42a
|
@ -149,32 +149,30 @@ pub fn main_with_args(command_name: &str, args: impl IntoIterator<Item = String>
|
||||||
|
|
||||||
/// Change the current directory into the Cargo workspace's root.
|
/// Change the current directory into the Cargo workspace's root.
|
||||||
///
|
///
|
||||||
/// This is using a heuristic to find the workspace root, walking up directories
|
/// This is using a heuristic to find the workspace root. It considers all ancestor directories of
|
||||||
/// from `CARGO_MANIFEST_DIR` until another `Cargo.toml` is found, and assuming
|
/// either `CARGO_MANIFEST_DIR` or the current directory, and finds the leftmost one containing a
|
||||||
/// that is the active workspace.
|
/// `Cargo.toml` file.
|
||||||
pub fn chdir_workspace_root() -> Result<()> {
|
pub fn chdir_workspace_root() -> Result<()> {
|
||||||
let xtask_project_dir = std::env::var("CARGO_MANIFEST_DIR")
|
// This is either the directory of the xtask binary when using `nih_plug_xtask` normally, or any
|
||||||
.context("'$CARGO_MANIFEST_DIR' was not set, are you running this binary directly?")?;
|
// random project when using it through `cargo nih-plug`.
|
||||||
let workspace_root = Path::new(&xtask_project_dir).parent().context(
|
let project_dir = std::env::var("CARGO_MANIFEST_DIR")
|
||||||
"'$CARGO_MANIFEST_DIR' has an unexpected value, are you running this binary directly?",
|
.map(PathBuf::from)
|
||||||
)?;
|
.or_else(|_| std::env::current_dir())
|
||||||
|
.context(
|
||||||
|
"'$CARGO_MANIFEST_DIR' was not set and the current working directory could not be \
|
||||||
|
found",
|
||||||
|
)?;
|
||||||
|
|
||||||
// If `workspace_root` is not actually the workspace's root because this xtask binary's `Cargo.toml`
|
let workspace_root = project_dir
|
||||||
// file is in a sub-subdirectory, then we'll walk up the directory stack until we hopefully find
|
.ancestors()
|
||||||
// it.
|
.filter(|dir| dir.join("Cargo.toml").exists())
|
||||||
let workspace_root = if workspace_root.join("Cargo.toml").exists() {
|
.last()
|
||||||
workspace_root
|
.with_context(|| {
|
||||||
} else {
|
format!(
|
||||||
let mut workspace_root_candidate = workspace_root;
|
"Could not find a 'Cargo.toml' file in '{}' or any of its parent directories",
|
||||||
loop {
|
project_dir.display()
|
||||||
workspace_root_candidate = workspace_root_candidate
|
)
|
||||||
.parent()
|
})?;
|
||||||
.context("Reached the file system root without finding a parent Cargo.toml file")?;
|
|
||||||
if workspace_root_candidate.join("Cargo.toml").exists() {
|
|
||||||
break workspace_root_candidate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::env::set_current_dir(workspace_root)
|
std::env::set_current_dir(workspace_root)
|
||||||
.context("Could not change to workspace root directory")
|
.context("Could not change to workspace root directory")
|
||||||
|
|
Loading…
Reference in a new issue