dbt-labs/dbt-core · error · minijinja::Error (InvalidOperation)

Failed to write file: {e}

Error message

Failed to write file: {e}

What it means

write() wraps the underlying filesystem write in write_file(); if that helper returns an OS-level error (permissions, invalid path, disk full), the io error is wrapped into this message including the path context and original error text.

Source

Thrown at crates/dbt-jinja-utils/src/phases/run/run_node_context.rs:711

            ));
        }

        // Extract payload from args
        let payload = match args[0].as_str() {
            Some(s) => s,
            None => {
                return Err(Error::new(
                    ErrorKind::InvalidOperation,
                    "Failed to convert payload to string".to_string(),
                ));
            }
        };

        // Write the file
        match write_file(&self.run_file_path, &self.resource_type, payload) {
            Ok(_) => {}
            Err(e) => {
                return Err(Error::new(
                    ErrorKind::InvalidOperation,
                    format!("Failed to write file: {e}"),
                ));
            }
        }

        // Return empty string on success
        Ok(MinijinjaValue::from(""))
    }
}

/// Write a file to disk
fn write_file(full_path: &Path, resource_type: &str, payload: &str) -> Result<(), Error> {
    // Check if model is a Macro or SourceDefinition
    if resource_type == "macro" || resource_type == "source" {
        return Err(Error::new(
            ErrorKind::InvalidOperation,
            "Macros and sources cannot be written to disk",

View on GitHub (pinned to 0267ce9170)

Solutions

  1. Read the wrapped OS error in the message and fix the underlying cause (permissions, disk space, read-only mount).
  2. Ensure the process user has write access to the target/ directory tree.
  3. Clear or recreate the target/ directory if it contains files owned by another user.
  4. Check available disk space with df and clean up old target artifacts.

Example fix

// before
# running as user without write access to target/
// after
chmod -R u+w target/ || chown -R dbtuser:dbtgroup target/
Defensive patterns

Strategy: try-catch

Try / catch

match write_file(&path, &rt, payload) {
    Ok(_) => (),
    Err(e) if e.to_string().contains("Failed to write file") => {
        // inspect wrapped io error; check permissions/disk space before failing the run
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: write() targets a path in the target/ directory that cannot be written: read-only filesystem, permission denied, path too long, or the write_file helper itself returned an error not otherwise specialized.

Common situations: Running in containers where target/ is mounted read-only; disk quota exceeded; target path colliding with a file owned by another user after switching run users.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


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