Skip to content

Commit

Permalink
idl: Make safety comment checks fail silently when program path env i…
Browse files Browse the repository at this point in the history
…s not set (coral-xyz#3045)
  • Loading branch information
acheroncrypto authored Jun 26, 2024
1 parent 18cc94b commit 594169e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lang/syn/src/idl/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ pub fn get_no_docs() -> bool {
.unwrap_or_default()
}

pub fn get_program_path() -> Result<PathBuf> {
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)
}
Expand Down
5 changes: 2 additions & 3 deletions lang/syn/src/idl/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Path>) -> Result<Option<syn::Type>> {
Expand All @@ -17,8 +17,7 @@ pub fn get_external_type(name: &str, path: impl AsRef<Path>) -> Result<Option<sy
.ok_or_else(|| anyhow!("`{name}` not found in use statements"))?;

// Get crate name and version from lock file
let program_path =
std::env::var("ANCHOR_IDL_BUILD_PROGRAM_PATH").expect("Failed to get program path");
let program_path = get_program_path()?;
let lock_path = find_path("Cargo.lock", program_path)?;
let lock_file = parse_lock_file(lock_path)?;
let registry_path = get_registry_path()?;
Expand Down
26 changes: 20 additions & 6 deletions lang/syn/src/idl/program.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use heck::CamelCase;
use proc_macro2::TokenStream;
use quote::{format_ident, quote};

use super::{
common::{gen_print_section, get_idl_module_path, get_no_docs},
common::{gen_print_section, get_idl_module_path, get_no_docs, get_program_path},
defined::gen_idl_type,
};
use crate::{
Expand Down Expand Up @@ -156,10 +154,26 @@ fn check_safety_comments() -> 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()
Expand Down

0 comments on commit 594169e

Please sign in to comment.