Skip to content

Commit

Permalink
cli: Check anchor-lang and CLI version compatibility (#2753)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Jan 2, 2024
1 parent 1fc92ab commit 47ff77f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- lang: Add `InstructionData::write_to` implementation ([#2733](https://github.com/coral-xyz/anchor/pull/2733)).
- lang: Add `#[interface(..)]` attribute for instruction discriminator overrides ([#2728](https://github.com/coral-xyz/anchor/pull/2728)).
- ts: Add `.interface(..)` method for instruction discriminator overrides ([#2728](https://github.com/coral-xyz/anchor/pull/2728)).
- cli: Check `anchor-lang` and CLI version compatibility ([#2753](https://github.com/coral-xyz/anchor/pull/2753)).

### Fixes

Expand Down
38 changes: 36 additions & 2 deletions cli/src/checks.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use std::path::Path;

use anyhow::{anyhow, Result};
use semver::Version;

use crate::config::Manifest;
use crate::{
config::{Config, Manifest, WithPath},
VERSION,
};

/// Check whether `overflow-checks` codegen option is enabled.
///
Expand All @@ -17,6 +21,36 @@ pub fn check_overflow(cargo_toml_path: impl AsRef<Path>) -> Result<bool> {
"`overflow-checks` is not enabled. To enable, add:\n\n\
[profile.release]\n\
overflow-checks = true\n\n\
in workspace root Cargo.toml.",
in workspace root Cargo.toml",
))
}

/// Check whether there is a mismatch between the current CLI version and the `anchor-lang` crate
/// version.
///
/// This function logs warnings in the case of a mismatch.
pub fn check_anchor_version(cfg: &WithPath<Config>) -> Result<()> {
let cli_version = Version::parse(VERSION)?;
let mismatched_lang_version = cfg
.get_rust_program_list()?
.into_iter()
.map(|path| path.join("Cargo.toml"))
.map(cargo_toml::Manifest::from_path)
.filter_map(|man| man.ok())
.filter_map(|man| man.dependencies.get("anchor-lang").map(|d| d.to_owned()))
.filter_map(|dep| Version::parse(dep.req()).ok())
.find(|ver| ver != &cli_version); // Only log the warning once

if let Some(ver) = mismatched_lang_version {
eprintln!(
"WARNING: `anchor-lang` version({ver}) and the current CLI version({cli_version}) \
don't match.\n\n\t\
This can lead to unwanted behavior. To use the same CLI version, add:\n\n\t\
[toolchain]\n\t\
anchor_version = \"{ver}\"\n\n\t\
to Anchor.toml\n"
);
}

Ok(())
}
5 changes: 4 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use anchor_syn::idl::types::{
IdlTypeDefinitionTy,
};
use anyhow::{anyhow, Context, Result};
use checks::check_overflow;
use checks::{check_anchor_version, check_overflow};
use clap::Parser;
use dirs::home_dir;
use flate2::read::GzDecoder;
Expand Down Expand Up @@ -1194,6 +1194,9 @@ pub fn build(
check_overflow(workspace_cargo_toml_path)?;
}

// Check whether there is a mismatch between CLI and lang crate versions
check_anchor_version(&cfg).ok();

let idl_out = match idl {
Some(idl) => Some(PathBuf::from(idl)),
None => Some(cfg_parent.join("target/idl")),
Expand Down

1 comment on commit 47ff77f

@vercel
Copy link

@vercel vercel bot commented on 47ff77f Jan 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com

Please sign in to comment.