Browse Source

idl: Log output with `ANCHOR_LOG` on failure and improve build error message (#3284)

acheron 1 year ago
parent
commit
c960b8c623
2 changed files with 11 additions and 7 deletions
  1. 1 0
      CHANGELOG.md
  2. 10 7
      idl/src/build.rs

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

+ 10 - 7
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<Idl> = 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,