diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a6b442d36..7543ce7e61 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,6 +107,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 diff --git a/lang/attribute/program/src/declare_program/mod.rs b/lang/attribute/program/src/declare_program/mod.rs index 2b96995ee9..81acfd5695 100644 --- a/lang/attribute/program/src/declare_program/mod.rs +++ b/lang/attribute/program/src/declare_program/mod.rs @@ -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}; @@ -34,8 +36,9 @@ impl ToTokens for DeclareProgram { } fn get_idl(name: &syn::Ident) -> anyhow::Result { - 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"); @@ -43,7 +46,7 @@ fn get_idl(name: &syn::Ident) -> anyhow::Result { }) .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))? }