dbt-labs/dbt-core · error

failed to write compiled code to stdout

Error message

failed to write compiled code to stdout

What it means

`handle_compiled_code_inline` formats compiled inline SQL (via `format_compiled_inline_code`) and writes it to a locked stdout with `.expect("failed to write compiled code to stdout")`. The panic means the byte write failed, almost always a broken pipe because the process reading stdout exited or was closed before consuming the compiled code output.

Source

Thrown at crates/dbt-common/src/tracing/layers/tui_layer.rs:1468

    }

    fn handle_compiled_code_inline(&self, compiled_code: &CompiledCodeInline) {
        // Only show if any Progress*, Completed or All option is enabled
        let should_show = self.show_options.contains(&ShowOptions::Progress)
            || self.show_options.contains(&ShowOptions::ProgressRender)
            || self.show_options.contains(&ShowOptions::Completed)
            || self.show_options.contains(&ShowOptions::All);

        if !should_show {
            return;
        }

        let formatted = format_compiled_inline_code(compiled_code, true);
        self.write_suspended(|| {
            io::stdout()
                .lock()
                .write_all(format!("{}\n", formatted).as_bytes())
                .expect("failed to write compiled code to stdout");
        });
    }

    fn handle_compiled_code(&self, compiled_code: &CompiledCode) {
        if self.command != FsCommand::Compile {
            return;
        }

        if !should_show_progress_message(ExecutionPhase::Render, &self.show_options) {
            return;
        }

        let formatted = format_compiled_code(compiled_code, true);
        self.write_suspended(|| {
            io::stdout()
                .lock()
                .write_all(format!("{}\n", formatted).as_bytes())
                .expect("failed to write compiled code to stdout");

View on GitHub (pinned to 0267ce9170)

Solutions

  1. Pipe to a consumer that reads everything, or redirect to a file: `dbt compile > compiled.sql`.
  2. Check `echo $?` and RUST_BACKTRACE to confirm broken-pipe (typically exit code 101 with this panic).
  3. Ensure the terminal/PTY stays open for the whole run.
  4. Upgrade dbt for non-panicking stdout write handling.

Example fix

// before
io::stdout()
    .lock()
    .write_all(format!("{}\n", formatted).as_bytes())
    .expect("failed to write compiled code to stdout");
// after
let _ = io::stdout()
    .lock()
    .write_all(format!("{}\n", formatted).as_bytes());
Defensive patterns

Strategy: try-catch

Validate before calling

// bash: capture instead of truncating pipe
dbt compile > compiled.out 2> compile.err; echo $?

Try / catch

if let Err(e) = io::stdout().lock().write_all(bytes) {
    eprintln!("compiled code not written: {e}");
}

Prevention

When it happens

Trigger: Running dbt (e.g. `dbt compile`/`dbt run` with inline compiled-code display enabled) while stdout is piped to an early-exiting consumer, or stdout is an invalid/closed descriptor, during `on_log_record` handling of a CompiledCodeInline event.

Common situations: `dbt compile | head -20`; log forwards that close the pipe; CI systems killing the reader while dbt still streams output.

Related errors


AI-assisted analysis of dbt-labs/dbt-core@0267ce9170 (2026-09-07). Data as JSON: /api/errors/ba89263958971979. Report an issue: GitHub.