idl: Remove `anchor-syn` dependency (#3030)

This commit is contained in:
acheron 2024-06-16 23:54:40 +02:00 committed by GitHub
parent 1c2aabe315
commit d4318cc7cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 35 additions and 16 deletions

View File

@ -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

1
Cargo.lock generated
View File

@ -291,7 +291,6 @@ dependencies = [
name = "anchor-lang-idl"
version = "0.1.0"
dependencies = [
"anchor-syn",
"anyhow",
"heck 0.3.3",
"regex",

View File

@ -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

View File

@ -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<Idl> {
// 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<Idl> {
fn build(program_path: &Path, resolution: bool, skip_lint: bool, no_docs: bool) -> Result<Idl> {
// `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<Idl> {
"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)

View File

@ -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()
}