Skip to content

Commit

Permalink
avm: Use rustc 1.79.0 when installing versions older than v0.31 (#3315
Browse files Browse the repository at this point in the history
)
  • Loading branch information
acheroncrypto authored Oct 16, 2024
1 parent d3a51c5 commit 9fe2cb6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- idl: Fix instructions with tuple parameters not producing an error([#3294](https://github.com/coral-xyz/anchor/pull/3294)).
- ts: Update `engines.node` to `>= 17` ([#3301](https://github.com/coral-xyz/anchor/pull/3301)).
- cli: Use OS-agnostic paths ([#3307](https://github.com/coral-xyz/anchor/pull/3307)).
- avm: Use `rustc 1.79.0` when installing versions older than v0.31 ([#3315](https://github.com/coral-xyz/anchor/pull/3315)).

### Breaking

Expand Down
39 changes: 33 additions & 6 deletions avm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{de, Deserialize};
use std::fs;
use std::io::{BufRead, Write};
use std::path::PathBuf;
use std::process::Stdio;
use std::process::{Command, Stdio};

/// Storage directory for AVM, customizable by setting the $AVM_HOME, defaults to ~/.avm
pub static AVM_HOME: Lazy<PathBuf> = Lazy::new(|| {
Expand Down Expand Up @@ -193,16 +193,43 @@ pub fn install_version(install_target: InstallTarget, force: bool) -> Result<()>
return Ok(());
}

let exit = std::process::Command::new("cargo")
// If the version is older than v0.31, install using `rustc 1.79.0` to get around the problem
// explained in https://github.com/coral-xyz/anchor/pull/3143
if version < Version::parse("0.31.0")? {
const REQUIRED_VERSION: &str = "1.79.0";
let is_installed = Command::new("rustup")
.args(["toolchain", "list"])
.output()
.map(|output| String::from_utf8(output.stdout))??
.lines()
.any(|line| line.starts_with(REQUIRED_VERSION));
if !is_installed {
let exit_status = Command::new("rustup")
.args(["toolchain", "install", REQUIRED_VERSION])
.spawn()?
.wait()?;
if !exit_status.success() {
return Err(anyhow!(
"Installation of `rustc {REQUIRED_VERSION}` failed. \
`rustc <1.80` is required to install Anchor v{version} from source. \
See https://github.com/coral-xyz/anchor/pull/3143 for more information."
));
}
}

// Prepend the toolchain to use with the `cargo install` command
args.insert(0, format!("+{REQUIRED_VERSION}"));
}

let output = Command::new("cargo")
.args(args)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.output()
.map_err(|e| anyhow!("Cargo install for {} failed: {}", version, e.to_string()))?;
if !exit.status.success() {
.map_err(|e| anyhow!("Cargo install for {version} failed: {e}"))?;
if !output.status.success() {
return Err(anyhow!(
"Failed to install {}, is it a valid version?",
version
"Failed to install {version}, is it a valid version?"
));
}

Expand Down

0 comments on commit 9fe2cb6

Please sign in to comment.