cli: Build IDL if there is only one program when using the `idl build` command (#3275)

This commit is contained in:
acheron 2024-09-26 01:18:37 +02:00 committed by GitHub
parent 0c306c2f13
commit ef542f5679
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 5 deletions

View File

@ -51,6 +51,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- 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)).
- cli: Build IDL if there is only one program when using the `idl build` command ([#3275](https://github.com/coral-xyz/anchor/pull/3275)).
### Fixes

View File

@ -2728,11 +2728,16 @@ fn idl_build(
Some(name) => cfg.get_program(&name)?.path,
None => {
let current_dir = std::env::current_dir()?;
cfg.read_all_programs()?
.into_iter()
.find(|program| program.path == current_dir)
.ok_or_else(|| anyhow!("Not in a program directory"))?
.path
let programs = cfg.read_all_programs()?;
if programs.len() == 1 {
programs.into_iter().next().unwrap().path
} else {
programs
.into_iter()
.find(|program| program.path == current_dir)
.ok_or_else(|| anyhow!("Not in a program directory"))?
.path
}
}
};
std::env::set_current_dir(program_path)?;