{"record":{"id":"7e8060cb965f6616","repo":"dbt-labs/dbt-core","slug":"failed-to-remove-stale-file","errorCode":null,"errorMessage":"Failed to remove stale file {}: {}","messagePattern":"Failed to remove stale file (.+?): (.+?)","errorType":"exception","errorClass":"minijinja::Error (InvalidOperation)","httpStatus":null,"severity":"error","filePath":"crates/dbt-jinja-utils/src/phases/run/run_node_context.rs","lineNumber":749,"sourceCode":"    }\n\n    // Reconcile a target/ left behind by a different artifact layout — e.g.\n    // dbt Core v1, or a pre-#14125 Fusion that wrote snapshot run artifacts as\n    // a flat file where we now write a nested directory (or vice versa). Without\n    // this, a stale file sitting where we now need a directory fails the write\n    // below with ENOTDIR (\"Not a directory\"), and a stale directory sitting\n    // where we now write a flat file fails with EISDIR (\"Is a directory\").\n    // See https://github.com/dbt-labs/dbt-core/issues/15692.\n    if let Some(parent) = full_path.parent()\n        && !parent.is_dir()\n    {\n        // Parent is missing, or a stale file on the parent chain (from an older\n        // layout) sits where we now need a directory. Remove the deepest such\n        // file so create_dir_all can rebuild the tree, then create it.\n        for ancestor in parent.ancestors() {\n            if ancestor.is_file() {\n                if let Err(e) = fs::remove_file(ancestor) {\n                    return Err(Error::new(\n                        ErrorKind::InvalidOperation,\n                        format!(\"Failed to remove stale file {}: {}\", ancestor.display(), e),\n                    ));\n                }\n                break;\n            }\n        }\n        if let Err(e) = fs::create_dir_all(parent) {\n            return Err(Error::new(\n                ErrorKind::InvalidOperation,\n                format!(\"Failed to create directory {}: {}\", parent.display(), e),\n            ));\n        }\n    }\n\n    // A stale directory sitting where we now write a flat file fails with EISDIR.\n    if full_path.is_dir()\n        && let Err(e) = fs::remove_dir_all(full_path)","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/dbt-labs/dbt-core/blob/0267ce9170576975b76b64ce856b2e5848e96617/crates/dbt-jinja-utils/src/phases/run/run_node_context.rs#L731-L767","documentation":"Before creating the destination directory, write_file walks the parent chain and deletes stale files left by an older artifact layout; if fs::remove_file on such a stale ancestor file fails (permissions, file in use), this error reports the path and the OS error.","triggerScenarios":"A target/ directory contains a file (e.g. target/run or an old flat artifact) exactly where write_file now needs a directory, and removal fails due to permission denied or the file being locked/held open by another process.","commonSituations":"Layout migration between dbt versions leaving flat files on the parent chain; running as a different user than the one that created target/; Windows file locks from an open editor or antivirus.","solutions":["Delete the stale file manually: remove the path named in the error message.","Fix ownership/permissions: chown/chmod the target/ directory for the running user.","Close processes holding the file open (editors, watchers, antivirus) and retry.","As a last resort, remove the whole target/ directory and rerun."],"exampleFix":"// before\nrm target/run   # Permission denied\n// after\nsudo chown -R $(whoami) target/ && rm target/run","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if let Err(e) = fs::remove_file(ancestor) {\n    if e.kind() == std::io::ErrorKind::PermissionDenied {\n        // surface actionable hint: chown/chmod target/ or remove the file manually\n    }\n    return Err(wrap(e));\n}","preventionTips":["Run all dbt invocations as the same OS user so target/ files stay writable.","Wipe target/ when migrating across dbt versions with changed artifact layouts.","On Windows, exclude target/ from antivirus/indexing that holds locks."],"tags":["filesystem","permissions","stale-artifacts","dbt"],"backgroundTag":"file-write-failed","analyzedSha":"0267ce9170576975b76b64ce856b2e5848e96617","analyzedAt":"2026-09-07T21:53:39.732Z","contentChangedAt":"2026-09-07T21:53:39.732Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}