diff --git a/CHANGELOG.md b/CHANGELOG.md index ab67f89b95..0606b11c6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,7 @@ The minor version will be incremented upon a breaking change and the patch versi - idl: Fix using full path types with `Program` ([#3228](https://github.com/coral-xyz/anchor/pull/3228)). - lang: Use closures for `init` constraints to reduce the stack usage of `try_accounts` ([#2939](https://github.com/coral-xyz/anchor/pull/2939)). - lang: Allow the `cfg` attribute above the instructions ([#2339](https://github.com/coral-xyz/anchor/pull/2339)). +- idl: Log output with `ANCHOR_LOG` on failure and improve build error message ([#3284](https://github.com/coral-xyz/anchor/pull/3284)). ### Breaking diff --git a/idl/src/build.rs b/idl/src/build.rs index 72af71476a..fe706bac8d 100644 --- a/idl/src/build.rs +++ b/idl/src/build.rs @@ -171,8 +171,16 @@ fn build( .current_dir(program_path) .stderr(Stdio::inherit()) .output()?; + + let stdout = String::from_utf8_lossy(&output.stdout); + if env::var("ANCHOR_LOG").is_ok() { + eprintln!("{}", stdout); + } + if !output.status.success() { - return Err(anyhow!("Building IDL failed")); + return Err(anyhow!( + "Building IDL failed. Run `ANCHOR_LOG=true anchor idl build` to see the logs." + )); } enum State { @@ -191,13 +199,8 @@ fn build( let mut types = BTreeMap::new(); let mut idl: Option = None; - let output = String::from_utf8_lossy(&output.stdout); - if env::var("ANCHOR_LOG").is_ok() { - println!("{}", output); - } - let mut state = State::Pass; - for line in output.lines() { + for line in stdout.lines() { match &mut state { State::Pass => match line { "--- IDL begin address ---" => state = State::Address,