From 82b5f6c8221ef670422fe6688317cb94876f68a9 Mon Sep 17 00:00:00 2001 From: Michael-F-Bryan Date: Tue, 19 Jul 2022 00:13:12 +0800 Subject: [PATCH] Provide a more useful specific error message when we can't determine a model's directory --- crates/fj-host/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/crates/fj-host/src/lib.rs b/crates/fj-host/src/lib.rs index c6a5c62e4..0d094ca5b 100644 --- a/crates/fj-host/src/lib.rs +++ b/crates/fj-host/src/lib.rs @@ -209,7 +209,7 @@ impl Model { fn package_associated_with_directory<'m>( metadata: &'m cargo_metadata::Metadata, dir: &Path, -) -> Result<&'m cargo_metadata::Package, io::Error> { +) -> Result<&'m cargo_metadata::Package, Error> { for pkg in metadata.workspace_packages() { let crate_dir = pkg .manifest_path @@ -221,9 +221,33 @@ fn package_associated_with_directory<'m>( } } - let msg = format!("\"{}\" doesn't point to a crate", dir.display()); + Err(ambiguous_path_error(metadata, dir)) +} - Err(io::Error::new(io::ErrorKind::Other, msg)) +fn ambiguous_path_error( + metadata: &cargo_metadata::Metadata, + dir: &Path, +) -> Error { + let possible_paths = metadata + .workspace_members + .iter() + .map(|id| PathBuf::from(&metadata[id].manifest_path)) + .map(|mut cargo_toml_path| { + let _ = cargo_toml_path.pop(); + cargo_toml_path + }) + .map(|crate_dir| { + crate_dir + .strip_prefix(&metadata.workspace_root) + .unwrap_or(&crate_dir) + .to_path_buf() + }) + .collect(); + + Error::AmbiguousPath { + dir: dir.to_path_buf(), + possible_paths, + } } /// Watches a model for changes, reloading it continually @@ -331,6 +355,22 @@ pub enum Error { /// [`cargo_metadata::MetadataCommand`]. #[error("Unable to determine the crate's metadata")] CargoMetadata(#[from] cargo_metadata::Error), + + /// The user pointed us to a directory, but it doesn't look like that was + /// a crate root (i.e. the folder containing `Cargo.toml`). + #[error( + "It doesn't look like \"{}\" is a crate directory. Did you mean one of {}?", + dir.display(), + possible_paths.iter().map(|p| p.display().to_string()) + .collect::>() + .join(", ") + )] + AmbiguousPath { + /// The model directory supplied by the user. + dir: PathBuf, + /// The directories for each crate in the workspace. + possible_paths: Vec, + }, } type ModelFn = unsafe extern "C" fn(args: &Parameters) -> fj::Shape;