diff --git a/CHANGELOG.md b/CHANGELOG.md index 85e2f7272..b3be8b8c8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -27,6 +27,7 @@ The minor version will be incremented upon a breaking change and the patch versi - cli: Add `idl type` command ([#3017](https://github.com/coral-xyz/anchor/pull/3017)). - lang: Add `anchor_lang::pubkey` macro for declaring `Pubkey` const values ([#3021](https://github.com/coral-xyz/anchor/pull/3021)). - cli: Sync program ids on the initial build ([#3023](https://github.com/coral-xyz/anchor/pull/3023)). +- idl: Remove `anchor-syn` dependency ([#3030](https://github.com/coral-xyz/anchor/pull/3030)). ### Fixes diff --git a/Cargo.lock b/Cargo.lock index 73ae58d5f..267c0c4a1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -291,7 +291,6 @@ dependencies = [ name = "anchor-lang-idl" version = "0.1.0" dependencies = [ - "anchor-syn", "anyhow", "heck 0.3.3", "regex", diff --git a/idl/Cargo.toml b/idl/Cargo.toml index 6925e7d1b..5d0421a4a 100644 --- a/idl/Cargo.toml +++ b/idl/Cargo.toml @@ -12,7 +12,7 @@ all-features = true rustdoc-args = ["--cfg", "docsrs"] [features] -build = ["anchor-syn", "regex"] +build = ["regex"] convert = ["heck", "sha2"] [dependencies] @@ -21,7 +21,6 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" # `build` feature only -anchor-syn = { path = "../lang/syn", version = "0.30.0", optional = true } regex = { version = "1", optional = true } # `convert` feature only diff --git a/idl/src/build.rs b/idl/src/build.rs index a67e8d7a3..96dc3db2c 100644 --- a/idl/src/build.rs +++ b/idl/src/build.rs @@ -5,7 +5,6 @@ use std::{ process::{Command, Stdio}, }; -use anchor_syn::parser::context::CrateContext; use anyhow::{anyhow, Result}; use regex::Regex; use serde::Deserialize; @@ -51,15 +50,7 @@ pub fn build_idl( skip_lint: bool, no_docs: bool, ) -> Result { - // Check safety comments - let program_path = program_path.as_ref(); - let lib_path = program_path.join("src").join("lib.rs"); - let ctx = CrateContext::parse(lib_path)?; - if !skip_lint { - ctx.safety_checks()?; - } - - let idl = build(program_path, resolution, no_docs)?; + let idl = build(program_path.as_ref(), resolution, skip_lint, no_docs)?; let idl = convert_module_paths(idl); let idl = sort(idl); verify(&idl)?; @@ -68,7 +59,7 @@ pub fn build_idl( } /// Build IDL. -fn build(program_path: &Path, resolution: bool, no_docs: bool) -> Result { +fn build(program_path: &Path, resolution: bool, skip_lint: bool, no_docs: bool) -> Result { // `nightly` toolchain is currently required for building the IDL. let toolchain = std::env::var("RUSTUP_TOOLCHAIN") .map(|toolchain| format!("+{}", toolchain)) @@ -95,6 +86,10 @@ fn build(program_path: &Path, resolution: bool, no_docs: bool) -> Result { "ANCHOR_IDL_BUILD_RESOLUTION", if resolution { "TRUE" } else { "FALSE" }, ) + .env( + "ANCHOR_IDL_BUILD_SKIP_LINT", + if skip_lint { "TRUE" } else { "FALSE" }, + ) .env("ANCHOR_IDL_BUILD_PROGRAM_PATH", program_path) .env("RUSTFLAGS", "--cfg procmacro2_semver_exempt") .current_dir(program_path) diff --git a/lang/syn/src/idl/program.rs b/lang/syn/src/idl/program.rs index f4268da85..043f34312 100644 --- a/lang/syn/src/idl/program.rs +++ b/lang/syn/src/idl/program.rs @@ -1,4 +1,6 @@ -use anyhow::Result; +use std::path::PathBuf; + +use anyhow::{anyhow, Result}; use heck::CamelCase; use proc_macro2::TokenStream; use quote::{format_ident, quote}; @@ -7,10 +9,15 @@ use super::{ common::{gen_print_section, get_idl_module_path, get_no_docs}, defined::gen_idl_type, }; -use crate::{parser::docs, Program}; +use crate::{ + parser::{context::CrateContext, docs}, + Program, +}; /// Generate the IDL build print function for the program module. pub fn gen_idl_print_fn_program(program: &Program) -> TokenStream { + check_safety_comments().unwrap_or_else(|e| panic!("Safety checks failed: {e}")); + let idl = get_idl_module_path(); let no_docs = get_no_docs(); @@ -139,3 +146,21 @@ pub fn gen_idl_print_fn_program(program: &Program) -> TokenStream { } } } + +/// Check safety comments. +fn check_safety_comments() -> Result<()> { + let skip_lint = option_env!("ANCHOR_IDL_BUILD_SKIP_LINT") + .map(|val| val == "TRUE") + .unwrap_or_default(); + if skip_lint { + return Ok(()); + } + + std::env::var("ANCHOR_IDL_BUILD_PROGRAM_PATH") + .map(PathBuf::from) + .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() +}