Skip to content

Commit

Permalink
cli: Fix commit based anchor_version override (#2704)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Nov 17, 2023
1 parent a9c423e commit d28414e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Fix using user specific path for `provider.wallet` in `Anchor.toml` ([#2696](https://github.com/coral-xyz/anchor/pull/2696)).
- syn: Fix IDL constant seeds parsing ([#2699](https://github.com/coral-xyz/anchor/pull/2699)).
- cli: Display errors if toolchain override restoration fails ([#2700](https://github.com/coral-xyz/anchor/pull/2700)).
- cli: Fix commit based `anchor_version` override ([#2704](https://github.com/coral-xyz/anchor/pull/2704)).

### Breaking

Expand Down
27 changes: 21 additions & 6 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC
let cfg = Config::discover(cfg_override)?;
if let Some(cfg) = cfg {
fn get_current_version(cmd_name: &str) -> Result<String> {
let output: std::process::Output = std::process::Command::new(cmd_name)
let output = std::process::Command::new(cmd_name)
.arg("--version")
.output()?;
let output_version = std::str::from_utf8(&output.stdout)?;
Expand Down Expand Up @@ -540,16 +540,31 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC

// Anchor version override should be handled last
if let Some(anchor_version) = &cfg.toolchain.anchor_version {
let current_version = VERSION;
if anchor_version != current_version {
// Anchor binary name prefix(applies to binaries that are installed via `avm`)
const ANCHOR_BINARY_PREFIX: &str = "anchor-";

// Get the current version from the executing binary name if possible because commit
// based toolchain overrides do not have version information.
let current_version = std::env::args()
.next()
.expect("First arg should exist")
.parse::<PathBuf>()?
.file_name()
.and_then(|name| name.to_str())
.expect("File name should be valid Unicode")
.split_once(ANCHOR_BINARY_PREFIX)
.map(|(_, version)| version)
.unwrap_or(VERSION)
.to_owned();
if anchor_version != &current_version {
let binary_path = home_dir()
.unwrap()
.join(".avm")
.join("bin")
.join(format!("anchor-{anchor_version}"));
.join(format!("{ANCHOR_BINARY_PREFIX}{anchor_version}"));

if !binary_path.exists() {
println!(
eprintln!(
"`anchor` {anchor_version} is not installed with `avm`. Installing...\n"
);

Expand All @@ -559,7 +574,7 @@ fn override_toolchain(cfg_override: &ConfigOverride) -> Result<RestoreToolchainC
.spawn()?
.wait()?;
if !exit_status.success() {
println!(
eprintln!(
"Failed to install `anchor` {anchor_version}, \
using {current_version} instead"
);
Expand Down

1 comment on commit d28414e

@vercel
Copy link

@vercel vercel bot commented on d28414e Nov 17, 2023

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-200ms.vercel.app
anchor-docs-git-master-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com

Please sign in to comment.