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

GetFinalPathNameByHandleW returned 0 length

Error message

GetFinalPathNameByHandleW returned 0 length

What it means

On Windows, the first GetFinalPathNameByHandleW call — used to query the buffer size needed for the final path — returned 0. A zero length means the API failed (the return is the required length on success), typically because the handle is not a file handle, the file was deleted, or access rights are insufficient; GetLastError holds the reason.

Source

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

    }

    #[cfg(target_os = "windows")]
    fn current_path(&self, _: &Arc<dyn Fs>) -> Result<PathBuf> {
        use std::ffi::OsString;
        use std::os::windows::ffi::OsStringExt;
        use std::os::windows::io::AsRawHandle;

        use windows::Win32::Foundation::HANDLE;
        use windows::Win32::Storage::FileSystem::{
            FILE_NAME_NORMALIZED, GetFinalPathNameByHandleW,
        };

        let handle = HANDLE(self.as_raw_handle() as _);

        // Query required buffer size (in wide chars)
        let required_len =
            unsafe { GetFinalPathNameByHandleW(handle, &mut [], FILE_NAME_NORMALIZED) };
        anyhow::ensure!(
            required_len != 0,
            "GetFinalPathNameByHandleW returned 0 length"
        );

        // Allocate buffer and retrieve the path
        let mut buf: Vec<u16> = vec![0u16; required_len as usize + 1];
        let written = unsafe { GetFinalPathNameByHandleW(handle, &mut buf, FILE_NAME_NORMALIZED) };
        anyhow::ensure!(
            written != 0,
            "GetFinalPathNameByHandleW failed to write path"
        );

        let os_str: OsString = OsString::from_wide(&buf[..written as usize]);
        anyhow::ensure!(!os_str.is_empty(), "Could find a path for the file handle");
        Ok(PathBuf::from(os_str))
    }
}

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Inspect GetLastError to identify the cause (ERROR_INVALID_HANDLE, ERROR_ACCESS_DENIED, path deleted)
  2. Fall back to the path captured when the file was opened, as the final path cannot be queried for this handle
Defensive patterns

Strategy: fallback

When it happens

Trigger: Thrown at crates/fs/src/fs.rs:503 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/fa97cb42a0ad66ab. Report an issue: GitHub.