{"record":{"id":"1394bc36f31e5170","repo":"jdx/mise","slug":"refusing-to-remove-directory-as-a-file","errorCode":null,"errorMessage":"refusing to remove directory as a file: {}","messagePattern":"refusing to remove directory as a file: (.+?)","errorType":"error_code","errorClass":"eyre::Report","httpStatus":null,"severity":"error","filePath":"src/system/managed_files.rs","lineNumber":1578,"sourceCode":"                )\n            })?\n        }\n        Ok(_) => fs::remove_file(path)?,\n    }\n    temporary\n        .persist(path)\n        .map_err(|error| error.error)\n        .wrap_err_with(|| format!(\"failed to atomically replace {}\", path.display()))?;\n    fs::File::open(parent)?.sync_all()?;\n    Ok(())\n}\n\nfn remove_file(path: &Path) -> Result<()> {\n    match fs::symlink_metadata(path) {\n        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),\n        Err(err) => Err(err.into()),\n        Ok(metadata) if metadata.file_type().is_dir() => {\n            bail!(\"refusing to remove directory as a file: {}\", path.display())\n        }\n        Ok(_) => fs::remove_file(path).map_err(Into::into),\n    }\n}\n\nfn create_directory(\n    path: &Path,\n    owner: Option<&str>,\n    group: Option<&str>,\n    mode: u32,\n    replace: bool,\n) -> Result<()> {\n    match fs::symlink_metadata(path) {\n        Err(error)\n            if matches!(\n                error.kind(),\n                std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory\n            ) => {}","sourceCodeStart":1560,"sourceCodeEnd":1596,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/system/managed_files.rs#L1560-L1596","documentation":"`remove_file` in the managed-files module checks the entry's type with `fs::symlink_metadata` before deleting. If the path is actually a directory, it refuses with this error instead of calling `fs::remove_file`, which would fail confusingly on most platforms. This prevents accidentally treating a directory as a deletable file.","triggerScenarios":"Calling the managed-file removal API on a path whose symlink metadata reports a directory — e.g. the manifest previously declared a directory at that path, or the user replaced the file with a directory between runs.","commonSituations":"State drift: a managed path changed kind on disk; a user replaced a managed config file with a directory; manifest corrections that repurpose a path.","solutions":["Use the directory-removal API (managed directory removal) instead of the file-removal API","Manually remove the directory (`rm -r <path>`) if it is no longer needed","Verify the path in the manifest matches the intended kind on disk"],"exampleFix":"// before\nremove_managed_file(Path::new(\"/etc/app/old.d\"))?; // it's a directory\n// after\nremove_managed_directory(Path::new(\"/etc/app/old.d\"))?;","handlingStrategy":"validation","validationCode":"fn safe_remove(path: &std::path::Path) -> std::io::Result<()> {\n    match std::fs::symlink_metadata(path) {\n        Ok(m) if m.is_dir() => std::fs::remove_dir_all(path),\n        Ok(_) => std::fs::remove_file(path),\n        Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),\n        Err(e) => Err(e),\n    }\n}","typeGuard":"fn is_dir_path(path: &std::path::Path) -> bool {\n    std::fs::symlink_metadata(path).map_or(false, |m| m.is_dir())\n}","tryCatchPattern":"match result {\n    Err(e) if e.to_string().contains(\"refusing to remove directory as a file\") => {\n        remove_dir_all(path)?; // only if the contents are safe to delete\n    }\n    other => other?,\n}","preventionTips":["Check the path kind before choosing file vs directory removal","Keep manifests in sync with on-disk path kinds","Never replace a managed file with a directory without updating the manifest"],"tags":["filesystem","type-mismatch","managed-files"],"backgroundTag":"path-is-not-a-directory","analyzedSha":"533346cc374382b41ec5ff70536252b2e96e725c","analyzedAt":"2026-09-17T13:35:38.149Z","contentChangedAt":"2026-09-17T13:35:38.149Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}