From bd816615d7eed8ea7445df58819345dc8000fc31 Mon Sep 17 00:00:00 2001 From: Micah Kendall Date: Sun, 27 Nov 2022 08:52:20 +1100 Subject: [PATCH] Display more descriptive error on missing manifest MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: ``` ❯ aiken check Error: × No such file or directory (os error 2) ``` After: ``` ❯ aiken check Error: × Missing 'aiken.toml' manifest in /Users/ktorz/Documents/Projects/aiken-lang/aiken help: Try running `aiken new ` to initialise a project with an example manifest. ``` Co-authored-by: KtorZ --- crates/cli/src/lib.rs | 4 +--- crates/project/src/config.rs | 9 ++++----- crates/project/src/error.rs | 9 +++++++++ 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/crates/cli/src/lib.rs b/crates/cli/src/lib.rs index d7daba18..044d238a 100644 --- a/crates/cli/src/lib.rs +++ b/crates/cli/src/lib.rs @@ -1,9 +1,7 @@ -use std::collections::BTreeMap; -use std::{env, path::PathBuf, process}; - use aiken_project::{pretty, script::EvalInfo, telemetry, Project}; use miette::IntoDiagnostic; use owo_colors::OwoColorize; +use std::{collections::BTreeMap, env, path::PathBuf, process}; use uplc::machine::cost_model::ExBudget; pub mod cmd; diff --git a/crates/project/src/config.rs b/crates/project/src/config.rs index 7ea850b8..c8c09dc4 100644 --- a/crates/project/src/config.rs +++ b/crates/project/src/config.rs @@ -1,10 +1,8 @@ -use std::{fmt::Display, fs, path::PathBuf}; - +use crate::error::Error; use aiken_lang::ast::Span; use miette::NamedSource; use serde::{de::Visitor, Deserialize, Serialize}; - -use crate::error::Error; +use std::{fmt::Display, fs, path::PathBuf}; #[derive(Deserialize)] pub struct Config { @@ -112,7 +110,8 @@ impl Display for Platform { impl Config { pub fn load(dir: PathBuf) -> Result { let config_path = dir.join("aiken.toml"); - let raw_config = fs::read_to_string(&config_path)?; + let raw_config = fs::read_to_string(&config_path) + .map_err(|_| Error::MissingManifest { path: dir.clone() })?; let result: Self = toml::from_str(&raw_config).map_err(|e| Error::TomlLoading { path: config_path.clone(), diff --git a/crates/project/src/error.rs b/crates/project/src/error.rs index 5d2663a3..8bdae4ed 100644 --- a/crates/project/src/error.rs +++ b/crates/project/src/error.rs @@ -52,6 +52,9 @@ pub enum Error { help: String, }, + #[error("Missing 'aiken.toml' manifest in {path}")] + MissingManifest { path: PathBuf }, + #[error("Cyclical module imports")] ImportCycle { modules: Vec }, @@ -175,6 +178,7 @@ impl Error { Error::FileIo { .. } => None, Error::Format { .. } => None, Error::StandardIo(_) => None, + Error::MissingManifest { path } => Some(path.to_path_buf()), Error::TomlLoading { path, .. } => Some(path.to_path_buf()), Error::ImportCycle { .. } => None, Error::List(_) => None, @@ -195,6 +199,7 @@ impl Error { Error::FileIo { .. } => None, Error::Format { .. } => None, Error::StandardIo(_) => None, + Error::MissingManifest { .. } => None, Error::TomlLoading { src, .. } => Some(src.to_string()), Error::ImportCycle { .. } => None, Error::List(_) => None, @@ -243,6 +248,7 @@ impl Diagnostic for Error { Error::Parse { .. } => Some(Box::new("aiken::parser")), Error::Type { .. } => Some(Box::new("aiken::check")), Error::StandardIo(_) => None, + Error::MissingManifest { .. } => None, Error::TomlLoading { .. } => Some(Box::new("aiken::loading::toml")), Error::Format { .. } => None, Error::ValidatorMustReturnBool { .. } => Some(Box::new("aiken::scripts")), @@ -270,6 +276,7 @@ impl Diagnostic for Error { Error::Parse { error, .. } => error.kind.help(), Error::Type { error, .. } => error.help(), Error::StandardIo(_) => None, + Error::MissingManifest { .. } => Some(Box::new("Try running `aiken new ` to initialise a project with an example manifest.")), Error::TomlLoading { .. } => None, Error::Format { .. } => None, Error::ValidatorMustReturnBool { .. } => Some(Box::new("Try annotating the validator's return type with Bool")), @@ -315,6 +322,7 @@ impl Diagnostic for Error { Error::ImportCycle { .. } => None, Error::List(_) => None, Error::Parse { error, .. } => error.labels(), + Error::MissingManifest { .. } => None, Error::Type { error, .. } => error.labels(), Error::StandardIo(_) => None, Error::TomlLoading { location, .. } => { @@ -349,6 +357,7 @@ impl Diagnostic for Error { Error::Parse { named, .. } => Some(named), Error::Type { named, .. } => Some(named), Error::StandardIo(_) => None, + Error::MissingManifest { .. } => None, Error::TomlLoading { named, .. } => Some(named), Error::Format { .. } => None, Error::ValidatorMustReturnBool { named, .. } => Some(named),