Browse Source

cli: Check whether the `idl-build` feature exists when using the `idl build` command (#3273)

acheron 1 year ago
parent
commit
37e0cd2b64
3 changed files with 30 additions and 29 deletions
  1. 1 0
      CHANGELOG.md
  2. 25 1
      cli/src/checks.rs
  3. 4 28
      cli/src/lib.rs

+ 1 - 0
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
 

+ 25 - 1
cli/src/checks.rs

@@ -115,7 +115,31 @@ pub fn check_deps(cfg: &WithPath<Config>) -> 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

+ 4 - 28
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<Idl> {
-    // 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)