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

This commit is contained in:
acheron 2024-09-25 00:54:17 +02:00 committed by GitHub
parent 385c36cd7f
commit 37e0cd2b64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 30 additions and 29 deletions

View File

@ -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: 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: 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: 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 ### Fixes

View File

@ -115,7 +115,31 @@ pub fn check_deps(cfg: &WithPath<Config>) -> Result<()> {
/// ///
/// **Note:** The check expects the current directory to be a program directory. /// **Note:** The check expects the current directory to be a program directory.
pub fn check_idl_build_feature() -> Result<()> { 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 // Check if `idl-build` is enabled by default
manifest manifest

View File

@ -2735,9 +2735,10 @@ fn idl_build(
.path .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() let idl = anchor_lang_idl::build::IdlBuilder::new()
.program_path(program_path)
.resolution(cfg.features.resolution) .resolution(cfg.features.resolution)
.skip_lint(cfg.features.skip_lint || skip_lint) .skip_lint(cfg.features.skip_lint || skip_lint)
.no_docs(no_docs) .no_docs(no_docs)
@ -2763,32 +2764,7 @@ fn generate_idl(
no_docs: bool, no_docs: bool,
cargo_args: &[String], cargo_args: &[String],
) -> Result<Idl> { ) -> Result<Idl> {
// Check whether the manifest has `idl-build` feature check_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();
anchor_lang_idl::build::IdlBuilder::new() anchor_lang_idl::build::IdlBuilder::new()
.resolution(cfg.features.resolution) .resolution(cfg.features.resolution)