From 594169efd093c2afe00406c4922d34aed71fdf67 Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Wed, 26 Jun 2024 23:31:17 +0200 Subject: [PATCH] idl: Make safety comment checks fail silently when program path env is not set (#3045) --- CHANGELOG.md | 4 +++- lang/syn/src/idl/common.rs | 6 ++++++ lang/syn/src/idl/external.rs | 5 ++--- lang/syn/src/idl/program.rs | 26 ++++++++++++++++++++------ 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 131e7201c2..0e63bfcc89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,9 +12,11 @@ The minor version will be incremented upon a breaking change and the patch versi ### Features +- ts: Add optional `commitment` parameter to `Program.addEventListener` ([#3052](https://github.com/coral-xyz/anchor/pull/3052)). + ### Fixes -- ts: add optional `commitment` parameter to `Program.addEventListener` ([#3052](https://github.com/coral-xyz/anchor/pull/3052)) +- idl: Make safety comment checks fail silently when program path env is not set ([#3045](https://github.com/coral-xyz/anchor/pull/3045])). ### Breaking diff --git a/lang/syn/src/idl/common.rs b/lang/syn/src/idl/common.rs index 23dd27bed0..c8e95059a0 100644 --- a/lang/syn/src/idl/common.rs +++ b/lang/syn/src/idl/common.rs @@ -22,6 +22,12 @@ pub fn get_no_docs() -> bool { .unwrap_or_default() } +pub fn get_program_path() -> Result { + std::env::var("ANCHOR_IDL_BUILD_PROGRAM_PATH") + .map(PathBuf::from) + .map_err(|_| anyhow!("Failed to get program path")) +} + pub fn get_idl_module_path() -> TokenStream { quote!(anchor_lang::idl::types) } diff --git a/lang/syn/src/idl/external.rs b/lang/syn/src/idl/external.rs index 414b6fe537..e6e5f58093 100644 --- a/lang/syn/src/idl/external.rs +++ b/lang/syn/src/idl/external.rs @@ -7,7 +7,7 @@ use anyhow::{anyhow, Result}; use cargo_toml::Manifest; use quote::ToTokens; -use super::common::find_path; +use super::common::{find_path, get_program_path}; use crate::parser::context::CrateContext; pub fn get_external_type(name: &str, path: impl AsRef) -> Result> { @@ -17,8 +17,7 @@ pub fn get_external_type(name: &str, path: impl AsRef) -> Result Result<()> { return Ok(()); } - std::env::var("ANCHOR_IDL_BUILD_PROGRAM_PATH") - .map(PathBuf::from) + let program_path = get_program_path(); + if program_path.is_err() { + // Getting the program path can fail in the following scenarios: + // + // - Anchor CLI version is incompatible with the current version + // - The error is coming from Rust Analyzer when the user has `idl-build` feature enabled, + // likely due to enabling all features (https://github.com/coral-xyz/anchor/issues/3042) + // + // For the first case, we have a warning when the user is using different versions of the + // lang and CLI crate. For the second case, users would either have to disable the + // `idl-build` feature, or define the program path environment variable in Rust Analyzer + // settings. + // + // Given this feature is not a critical one, and it works by default with `anchor build`, + // we can fail silently in the case of an error rather than panicking. + return Ok(()); + } + + program_path .map(|path| path.join("src").join("lib.rs")) - .map_err(|_| anyhow!("Failed to get program path")) .map(CrateContext::parse)? .map_err(|e| anyhow!("Failed to parse crate: {e}"))? .safety_checks()