jdx/mise · error
refusing to remove non-link
Error message
refusing to remove non-link: {} What it means
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.
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
Example fix
// before
remove_symlink_or_junction(&install_dir)?;
// after
if dir_link_target(&install_dir)?.is_some() {
remove_symlink_or_junction(&install_dir)?;
} else if install_dir.exists() {
fs::remove_dir_all(&install_dir)?;
} Defensive patterns
Strategy: validation
Validate before calling
fn is_link(p: &Path) -> bool {
std::fs::symlink_metadata(p)
.map(|m| m.file_type().is_symlink())
.unwrap_or(false)
}
if !is_link(link) { /* skip or use remove_dir_all */ } Type guard
fn as_symlink(p: &Path) -> Option<std::fs::Metadata> {
std::fs::symlink_metadata(p).ok().filter(|m| m.file_type().is_symlink())
} Try / catch
match remove_symlink_or_junction(&link) {
Ok(()) => {}
Err(e) if e.to_string().contains("refusing to remove non-link") => {
// path is a real dir/file or already gone: fall back to remove_dir_all if dir
}
Err(e) => return Err(e),
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- a parent directory is now a symlink; left untouched
- binary target is not an owned Caskroom symlink
- brew-cask: refusing to replace structured symlink directory
- . Creating directory symlinks on Windows may require…
- failed to create file symlink
AI-assisted analysis of jdx/mise@533346cc37 (2026-09-17).
Data as JSON: /api/errors/2f2e01232c6278a7.
Report an issue: GitHub.
Appendix: source
Thrown at src/file.rs:1336
};
Ok(Some(resolve_relative_link_target(link, target)))
}
fn resolve_relative_link_target(link: &Path, target: PathBuf) -> PathBuf {
if target.is_absolute() {
target
} else {
link.parent().unwrap_or(link).join(target)
}
}
#[cfg(unix)]
pub(crate) fn remove_symlink_or_junction(link: &Path) -> Result<()> {
// POSIX has no standard unlink-by-handle operation, so a concurrent replacement can still
// occur between this check and remove_file. The latter cannot remove directories; callers
// must keep the parent directory protected from untrusted writers.
if dir_link_target(link)?.is_none() {
bail!("refusing to remove non-link: {}", display_path(link));
}
fs::remove_file(link)
.wrap_err_with(|| format!("failed to remove symlink: {}", display_path(link)))
}
#[cfg(windows)]
pub(crate) fn remove_symlink_or_junction(link: &Path) -> Result<()> {
let link_handle = open_link_for_removal(link)?;
remove_open_link(link_handle)
.wrap_err_with(|| format!("failed to remove link or junction: {}", display_path(link)))
}
#[cfg(windows)]
fn open_link_for_removal(link: &Path) -> Result<File> {
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::AsRawHandle;
use windows_sys::Win32::Storage::FileSystem::{
DELETE, FILE_ATTRIBUTE_TAG_INFO, FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT,View on GitHub (pinned to 533346cc37)