diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a7f3184b..2607fadbd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -50,6 +50,7 @@ The minor version will be incremented upon a breaking change and the patch versi - cli: Warn if a manifest has `solana-program` dependency ([#3250](https://github.com/coral-xyz/anchor/pull/3250)). - cli: Add completions command to generate shell completions via the clap_complete crate ([#3251](https://github.com/coral-xyz/anchor/pull/3251)). - cli: Always convert IDLs ([#3265](https://github.com/coral-xyz/anchor/pull/3265)). +- cli: Check whether the `idl-build` feature exists when using the `idl build` command ([#3273](https://github.com/coral-xyz/anchor/pull/3273)). ### Fixes diff --git a/cli/src/checks.rs b/cli/src/checks.rs index b4c188e93..46072eec9 100644 --- a/cli/src/checks.rs +++ b/cli/src/checks.rs @@ -115,7 +115,31 @@ pub fn check_deps(cfg: &WithPath) -> Result<()> { /// /// **Note:** The check expects the current directory to be a program directory. pub fn check_idl_build_feature() -> Result<()> { - let manifest = Manifest::from_path("Cargo.toml")?; + let manifest_path = Path::new("Cargo.toml").canonicalize()?; + let manifest = Manifest::from_path(&manifest_path)?; + + // Check whether the manifest has `idl-build` feature + let has_idl_build_feature = manifest + .features + .iter() + .any(|(feature, _)| feature == "idl-build"); + if !has_idl_build_feature { + let anchor_spl_idl_build = manifest + .dependencies + .iter() + .any(|dep| dep.0 == "anchor-spl") + .then_some(r#", "anchor-spl/idl-build""#) + .unwrap_or_default(); + + return Err(anyhow!( + r#"`idl-build` feature is missing. To solve, add + +[features] +idl-build = ["anchor-lang/idl-build"{anchor_spl_idl_build}] + +in `{manifest_path:?}`."# + )); + } // Check if `idl-build` is enabled by default manifest diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 821fcefe8..ce1a93c8a 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -2735,9 +2735,10 @@ fn idl_build( .path } }; - check_idl_build_feature().ok(); + std::env::set_current_dir(program_path)?; + + check_idl_build_feature()?; let idl = anchor_lang_idl::build::IdlBuilder::new() - .program_path(program_path) .resolution(cfg.features.resolution) .skip_lint(cfg.features.skip_lint || skip_lint) .no_docs(no_docs) @@ -2763,32 +2764,7 @@ fn generate_idl( no_docs: bool, cargo_args: &[String], ) -> Result { - // Check whether the manifest has `idl-build` feature - let manifest = Manifest::discover()?.ok_or_else(|| anyhow!("Cargo.toml not found"))?; - let is_idl_build = manifest - .features - .iter() - .any(|(feature, _)| feature == "idl-build"); - if !is_idl_build { - let path = manifest.path().display(); - let anchor_spl_idl_build = manifest - .dependencies - .iter() - .any(|dep| dep.0 == "anchor-spl") - .then_some(r#", "anchor-spl/idl-build""#) - .unwrap_or_default(); - - return Err(anyhow!( - r#"`idl-build` feature is missing. To solve, add - -[features] -idl-build = ["anchor-lang/idl-build"{anchor_spl_idl_build}] - -in `{path}`."# - )); - } - - check_idl_build_feature().ok(); + check_idl_build_feature()?; anchor_lang_idl::build::IdlBuilder::new() .resolution(cfg.features.resolution)