zed-industries/zed · error · anyhow::Error

fcntl returned -1

Error message

fcntl returned -1

What it means

On macOS, the libc fcntl(F_GETPATH) call used to recover a file's current path from its descriptor returned -1. The kernel could not produce a path for this fd — possible right after the file is unlinked, for certain virtual files, or on an invalid fd; errno holds the precise reason but is not captured in this message.

Source

Thrown at crates/fs/src/fs.rs:478

}

pub trait FileHandle: Send + Sync + std::fmt::Debug {
    fn current_path(&self, fs: &Arc<dyn Fs>) -> Result<PathBuf>;
}

impl FileHandle for std::fs::File {
    #[cfg(target_os = "macos")]
    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
        use std::{
            ffi::{CStr, OsStr},
            os::unix::ffi::OsStrExt,
        };

        let fd = self.as_fd();
        let mut path_buf = MaybeUninit::<[u8; libc::PATH_MAX as usize]>::uninit();

        let result = unsafe { libc::fcntl(fd.as_raw_fd(), libc::F_GETPATH, path_buf.as_mut_ptr()) };
        anyhow::ensure!(result != -1, "fcntl returned -1");

        // SAFETY: `fcntl` will initialize the path buffer.
        let c_str = unsafe { CStr::from_ptr(path_buf.as_ptr().cast()) };
        anyhow::ensure!(!c_str.is_empty(), "Could find a path for the file handle");
        let path = PathBuf::from(OsStr::from_bytes(c_str.to_bytes()));
        Ok(path)
    }

    #[cfg(target_os = "linux")]
    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
        let fd = self.as_fd();
        let fd_path = format!("/proc/self/fd/{}", fd.as_raw_fd());
        let new_path = std::fs::read_link(fd_path)?;
        if new_path
            .file_name()
            .is_some_and(|f| f.to_string_lossy().ends_with(" (deleted)"))
        {
            anyhow::bail!("file was deleted")

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Check errno (via std::io::Error::last_os_error) to distinguish ENOENT (deleted file) from EBADF
  2. Fall back to remembering the path at open time instead of recovering it from the fd
  3. Treat unlinked files as unsaved/anonymous — no path recovery is possible for them
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at crates/fs/src/fs.rs:442 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of zed-industries/zed@9d272b0363 (2026-08-20). Data as JSON: /api/errors/6523854fc0792614. Report an issue: GitHub.