1
0
Fork 0

Change new workspace root finding error message

Not finding a parent Cargo.toml file should have a different error
message than the environment variable being empty.
This commit is contained in:
Robbert van der Helm 2022-07-18 23:51:22 +02:00
parent 59be30205a
commit ae8c78c5e5

View file

@ -124,18 +124,28 @@ pub fn main_with_args(command_name: &str, args: impl IntoIterator<Item = String>
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(
let project_root = Path::new(&xtask_project_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(());
// 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