Skip to content

Commit 10eb698

Browse files
avm: Add support for the .anchorversion file (solana-foundation#2553)
Co-authored-by: acheron <[email protected]>
1 parent 8309bb3 commit 10eb698

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi
1616
- client: Add a helper struct `DynSigner` to simplify use of `Client<C> where <C: Clone + Deref<Target = impl Signer>>` with Solana clap CLI utils that loads `Signer` as `Box<dyn Signer>` ([#2550](https://github.com/coral-xyz/anchor/pull/2550)).
1717
- lang: Allow CPI calls matching an interface without pinning program ID ([#2559](https://github.com/coral-xyz/anchor/pull/2559)).
1818
- cli, lang: Add IDL generation through compilation. `anchor build` still uses parsing method to generate IDLs, use `anchor idl build` to generate IDLs with the build method ([#2011](https://github.com/coral-xyz/anchor/pull/2011)).
19+
- avm: Add support for the `.anchorversion` file to facilitate switching between different versions of the `anchor-cli` ([#2553](https://github.com/coral-xyz/anchor/pull/2553)).
1920

2021
### Fixes
2122

avm/src/lib.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,15 @@ pub fn version_binary_path(version: &Version) -> PathBuf {
4545
}
4646

4747
/// Update the current version to a new version
48-
pub fn use_version(version: &Version) -> Result<()> {
48+
pub fn use_version(opt_version: Option<Version>) -> Result<()> {
49+
let version = match opt_version {
50+
Some(version) => version,
51+
None => read_anchorversion_file()?,
52+
};
53+
4954
let installed_versions = read_installed_versions();
5055
// Make sure the requested version is installed
51-
if !installed_versions.contains(version) {
56+
if !installed_versions.contains(&version) {
5257
if let Ok(current) = current_version() {
5358
println!("Version {version} is not installed, staying on version {current}.");
5459
} else {
@@ -118,7 +123,7 @@ pub fn install_version(version: &Version, force: bool) -> Result<()> {
118123
current_version_file.write_all(version.to_string().as_bytes())?;
119124
}
120125

121-
use_version(version)
126+
use_version(Some(version.clone()))
122127
}
123128

124129
/// Remove an installed version of anchor-cli
@@ -134,6 +139,14 @@ pub fn uninstall_version(version: &Version) -> Result<()> {
134139
Ok(())
135140
}
136141

142+
/// Read version from .anchorversion
143+
pub fn read_anchorversion_file() -> Result<Version> {
144+
fs::read_to_string(".anchorversion")
145+
.map_err(|e| anyhow!(".anchorversion file not found: {e}"))
146+
.map(|content| Version::parse(content.trim()))?
147+
.map_err(|e| anyhow!("Unable to parse version: {e}"))
148+
}
149+
137150
/// Ensure the users home directory is setup with the paths required by AVM.
138151
pub fn ensure_paths() {
139152
let home_dir = AVM_HOME.to_path_buf();
@@ -237,9 +250,31 @@ pub fn read_installed_versions() -> Vec<semver::Version> {
237250
mod tests {
238251
use crate::*;
239252
use semver::Version;
253+
use std::env;
240254
use std::fs;
241255
use std::io::Write;
242256

257+
#[test]
258+
fn test_read_anchorversion() {
259+
ensure_paths();
260+
let mut dir = env::current_dir().unwrap();
261+
dir.push(".anchorversion");
262+
let mut file_created = fs::File::create(&dir).unwrap();
263+
let test_version = "0.26.0";
264+
file_created.write(test_version.as_bytes()).unwrap();
265+
266+
let version = read_anchorversion_file();
267+
match version {
268+
Ok(v) => {
269+
assert_eq!(v.to_string(), test_version);
270+
}
271+
Err(_e) => {
272+
assert!(false);
273+
}
274+
}
275+
fs::remove_file(&dir).unwrap();
276+
}
277+
243278
#[test]
244279
fn test_ensure_paths() {
245280
ensure_paths();

avm/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ pub struct Cli {
1515
pub enum Commands {
1616
#[clap(about = "Use a specific version of Anchor")]
1717
Use {
18-
#[clap(value_parser = parse_version)]
19-
version: Version,
18+
#[clap(value_parser = parse_version, required = false)]
19+
version: Option<Version>,
2020
},
2121
#[clap(about = "Install a version of Anchor")]
2222
Install {
@@ -48,7 +48,7 @@ fn parse_version(version: &str) -> Result<Version, Error> {
4848
}
4949
pub fn entry(opts: Cli) -> Result<()> {
5050
match opts.command {
51-
Commands::Use { version } => avm::use_version(&version),
51+
Commands::Use { version } => avm::use_version(version),
5252
Commands::Install { version, force } => avm::install_version(&version, force),
5353
Commands::Uninstall { version } => avm::uninstall_version(&version),
5454
Commands::List {} => avm::list_versions(),

0 commit comments

Comments
 (0)