Skip to content

Commit

Permalink
cli: Check whether the idl-build feature exists when using the `idl…
Browse files Browse the repository at this point in the history
… build` command (#3273)
  • Loading branch information
acheroncrypto authored Sep 24, 2024
1 parent 385c36c commit 37e0cd2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
26 changes: 25 additions & 1 deletion cli/src/checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 4 additions & 28 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit 37e0cd2

Please sign in to comment.