Skip to content

Commit

Permalink
feat: warning on compiler version mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
rvcas committed Jun 14, 2024
1 parent 0ebffa2 commit de870e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
25 changes: 20 additions & 5 deletions crates/aiken-project/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,14 +510,17 @@ pub enum Warning {
DependencyAlreadyExists { name: PackageName },
#[error("Ignoring file with invalid module name at: {path:?}")]
InvalidModuleName { path: PathBuf },
#[error("aiken.toml demands compiler version {demanded}, but you are using {current}.")]
CompilerVersionMismatch { demanded: String, current: String },
}

impl ExtraData for Warning {
fn extra_data(&self) -> Option<String> {
match self {
Warning::NoValidators { .. }
| Warning::DependencyAlreadyExists { .. }
| Warning::InvalidModuleName { .. } => None,
| Warning::InvalidModuleName { .. }
| Warning::CompilerVersionMismatch { .. } => None,
Warning::Type { warning, .. } => warning.extra_data(),
}
}
Expand All @@ -527,7 +530,9 @@ impl GetSource for Warning {
fn path(&self) -> Option<PathBuf> {
match self {
Warning::InvalidModuleName { path } | Warning::Type { path, .. } => Some(path.clone()),
Warning::NoValidators | Warning::DependencyAlreadyExists { .. } => None,
Warning::NoValidators
| Warning::DependencyAlreadyExists { .. }
| Warning::CompilerVersionMismatch { .. } => None,
}
}

Expand All @@ -536,7 +541,8 @@ impl GetSource for Warning {
Warning::Type { src, .. } => Some(src.clone()),
Warning::NoValidators
| Warning::InvalidModuleName { .. }
| Warning::DependencyAlreadyExists { .. } => None,
| Warning::DependencyAlreadyExists { .. }
| Warning::CompilerVersionMismatch { .. } => None,
}
}
}
Expand All @@ -551,7 +557,8 @@ impl Diagnostic for Warning {
Warning::Type { named, .. } => Some(named),
Warning::NoValidators
| Warning::InvalidModuleName { .. }
| Warning::DependencyAlreadyExists { .. } => None,
| Warning::DependencyAlreadyExists { .. }
| Warning::CompilerVersionMismatch { .. } => None,
}
}

Expand All @@ -560,7 +567,8 @@ impl Diagnostic for Warning {
Warning::Type { warning, .. } => warning.labels(),
Warning::InvalidModuleName { .. }
| Warning::NoValidators
| Warning::DependencyAlreadyExists { .. } => None,
| Warning::DependencyAlreadyExists { .. }
| Warning::CompilerVersionMismatch { .. } => None,
}
}

Expand All @@ -572,6 +580,9 @@ impl Diagnostic for Warning {
))),
Warning::NoValidators => Some(Box::new("aiken::check")),
Warning::InvalidModuleName { .. } => Some(Box::new("aiken::project::module_name")),
Warning::CompilerVersionMismatch { .. } => {
Some(Box::new("aiken::project::compiler_version_mismatch"))
}
Warning::DependencyAlreadyExists { .. } => {
Some(Box::new("aiken::packages::already_exists"))
}
Expand All @@ -582,6 +593,10 @@ impl Diagnostic for Warning {
match self {
Warning::Type { warning, .. } => warning.help(),
Warning::NoValidators => None,
Warning::CompilerVersionMismatch { demanded, .. } => Some(Box::new(format!(
"You may want to switch to {}",
demanded.if_supports_color(Stdout, |s| s.purple())
))),
Warning::InvalidModuleName { .. } => Some(Box::new(
"Module names are lowercase, (ascii) alpha-numeric and may contain dashes or underscores.",
)),
Expand Down
13 changes: 12 additions & 1 deletion crates/aiken-project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,18 @@ where
pub fn new(root: PathBuf, event_listener: T) -> Result<Project<T>, Error> {
let config = Config::load(&root)?;

let project = Project::new_with_config(config, root, event_listener);
let demanded_compiler_version = format!("v{}", config.compiler);

let mut project = Project::new_with_config(config, root, event_listener);

let current_compiler_version = config::compiler_version(false);

if demanded_compiler_version != current_compiler_version {
project.warnings.push(Warning::CompilerVersionMismatch {
demanded: demanded_compiler_version,
current: current_compiler_version,
})
}

Ok(project)
}
Expand Down

0 comments on commit de870e2

Please sign in to comment.