Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lang: Remove a potential panic while getting the IDL in declare_program! #3458

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Fix custom `provider.cluster` ([#3428](https://github.com/coral-xyz/anchor/pull/3428)).
- cli: Ignore non semver solana/agave releases to avoid panic ([#3432](https://github.com/coral-xyz/anchor/pull/3432)).
- ts: Fix loading programs with numbers in their names using `workspace` ([#3450](https://github.com/coral-xyz/anchor/pull/3450)).
- lang: Remove a potential panic while getting the IDL in `declare_program!` ([#3458](https://github.com/coral-xyz/anchor/pull/3458)).

### Breaking

Expand Down
9 changes: 6 additions & 3 deletions lang/attribute/program/src/declare_program/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod common;
mod mods;

use std::{env, fs, path::PathBuf};

use anchor_lang_idl::{convert::convert_idl, types::Idl};
use anyhow::anyhow;
use quote::{quote, ToTokens};
Expand Down Expand Up @@ -34,16 +36,17 @@ impl ToTokens for DeclareProgram {
}

fn get_idl(name: &syn::Ident) -> anyhow::Result<Idl> {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("Failed to get manifest dir");
std::path::Path::new(&manifest_dir)
env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.map_err(|e| anyhow!("Failed to get environment variable `CARGO_MANIFEST_DIR`: {e}"))?
.ancestors()
.find_map(|ancestor| {
let idl_dir = ancestor.join("idls");
idl_dir.exists().then_some(idl_dir)
})
.ok_or_else(|| anyhow!("`idls` directory not found"))
.map(|idl_dir| idl_dir.join(name.to_string()).with_extension("json"))
.map(std::fs::read)?
.map(fs::read)?
.map_err(|e| anyhow!("Failed to read IDL `{name}`: {e}"))
.map(|buf| convert_idl(&buf))?
}
Expand Down
Loading