{"record":{"id":"2f2e01232c6278a7","repo":"jdx/mise","slug":"refusing-to-remove-non-link","errorCode":null,"errorMessage":"refusing to remove non-link: {}","messagePattern":"refusing to remove non-link: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/file.rs","lineNumber":1336,"sourceCode":"    };\n    Ok(Some(resolve_relative_link_target(link, target)))\n}\n\nfn resolve_relative_link_target(link: &Path, target: PathBuf) -> PathBuf {\n    if target.is_absolute() {\n        target\n    } else {\n        link.parent().unwrap_or(link).join(target)\n    }\n}\n\n#[cfg(unix)]\npub(crate) fn remove_symlink_or_junction(link: &Path) -> Result<()> {\n    // POSIX has no standard unlink-by-handle operation, so a concurrent replacement can still\n    // occur between this check and remove_file. The latter cannot remove directories; callers\n    // must keep the parent directory protected from untrusted writers.\n    if dir_link_target(link)?.is_none() {\n        bail!(\"refusing to remove non-link: {}\", display_path(link));\n    }\n    fs::remove_file(link)\n        .wrap_err_with(|| format!(\"failed to remove symlink: {}\", display_path(link)))\n}\n\n#[cfg(windows)]\npub(crate) fn remove_symlink_or_junction(link: &Path) -> Result<()> {\n    let link_handle = open_link_for_removal(link)?;\n    remove_open_link(link_handle)\n        .wrap_err_with(|| format!(\"failed to remove link or junction: {}\", display_path(link)))\n}\n\n#[cfg(windows)]\nfn open_link_for_removal(link: &Path) -> Result<File> {\n    use std::os::windows::fs::OpenOptionsExt;\n    use std::os::windows::io::AsRawHandle;\n    use windows_sys::Win32::Storage::FileSystem::{\n        DELETE, FILE_ATTRIBUTE_TAG_INFO, FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,","sourceCodeStart":1318,"sourceCodeEnd":1354,"githubUrl":"https://github.com/jdx/mise/blob/533346cc374382b41ec5ff70536252b2e96e725c/src/file.rs#L1318-L1354","documentation":"remove_symlink_or_junction refuses to unlink a path that is not a symlink (or junction on Windows). Before calling fs::remove_file, it checks whether the path resolves as a directory link; if it is a regular file, real directory, or missing, it bails with \"refusing to remove non-link\". This is a safety guard so a mistyped path (e.g. a real directory) is never destroyed.","triggerScenarios":"Calling remove_symlink_or_junction (directly or via remove_all / make_symlink replacement logic) on a path where dir_link_target(link) returns None: the path is a plain file, a real directory, or does not exist at the moment of the check.","commonSituations":"A tool install directory was replaced by a real directory (e.g. manually copied files instead of symlinked); a symlink was already removed by a concurrent process; or cleanup code runs on a stale path where the link no longer exists.","solutions":["Verify the path with ls -la (or fs::symlink_metadata) and confirm it is actually a symlink before removal","If the target is a real directory, use a directory-removal routine (remove_dir_all) instead of remove_symlink_or_junction","Handle the already-gone case: check existence first and treat non-link-but-missing as success","Ensure no concurrent process is replacing the path between check and unlink; keep the parent dir protected from untrusted writers"],"exampleFix":"// before\nremove_symlink_or_junction(&install_dir)?;\n// after\nif dir_link_target(&install_dir)?.is_some() {\n    remove_symlink_or_junction(&install_dir)?;\n} else if install_dir.exists() {\n    fs::remove_dir_all(&install_dir)?;\n}","handlingStrategy":"validation","validationCode":"fn is_link(p: &Path) -> bool {\n    std::fs::symlink_metadata(p)\n        .map(|m| m.file_type().is_symlink())\n        .unwrap_or(false)\n}\nif !is_link(link) { /* skip or use remove_dir_all */ }","typeGuard":"fn as_symlink(p: &Path) -> Option<std::fs::Metadata> {\n    std::fs::symlink_metadata(p).ok().filter(|m| m.file_type().is_symlink())\n}","tryCatchPattern":"match remove_symlink_or_junction(&link) {\n    Ok(()) => {}\n    Err(e) if e.to_string().contains(\"refusing to remove non-link\") => {\n        // path is a real dir/file or already gone: fall back to remove_dir_all if dir\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Check symlink_metadata().file_type().is_symlink() before link removal","Treat 'already removed' races as success in cleanup code","Never point link-removal at directories created by extraction","Keep install parent dirs free of untrusted writers to avoid TOCTOU"],"tags":["filesystem","symlink","safety-guard"],"backgroundTag":"invalid-argument-value","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"}