{"record":{"id":"f1413f5886f863c3","repo":"rust-lang/cargo","slug":"the-path-was-not-found","errorCode":null,"errorMessage":"the path was not found","messagePattern":"the path was not found","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src/util/mod.rs","lineNumber":160,"sourceCode":"}\n\n#[cfg(not(windows))]\n#[inline]\npub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {\n    std::fs::canonicalize(&path)\n}\n\n#[cfg(windows)]\n#[inline]\npub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {\n    use std::io::Error;\n    use std::io::ErrorKind;\n\n    // On Windows `canonicalize` may fail, so we fall back to getting an absolute path.\n    std::fs::canonicalize(&path).or_else(|_| {\n        // Return an error if a file does not exist for better compatibility with `canonicalize`\n        if !path.as_ref().try_exists()? {\n            return Err(Error::new(ErrorKind::NotFound, \"the path was not found\"));\n        }\n        std::path::absolute(&path)\n    })\n}\n\n/// Get the current [`umask`] value.\n///\n/// [`umask`]: https://man7.org/linux/man-pages/man2/umask.2.html\n#[cfg(unix)]\npub fn get_umask() -> u32 {\n    use std::sync::OnceLock;\n    static UMASK: OnceLock<libc::mode_t> = OnceLock::new();\n    // SAFETY: Syscalls are unsafe. Calling `umask` twice is even unsafer for\n    // multithreading program, since it doesn't provide a way to retrieve the\n    // value without modifications. We use a static `OnceLock` here to ensure\n    // it only gets call once during the entire program lifetime.\n    *UMASK.get_or_init(|| unsafe {\n        let umask = libc::umask(0o022);","sourceCodeStart":142,"sourceCodeEnd":178,"githubUrl":"https://github.com/rust-lang/cargo/blob/98a09e7e7d62850f14e5b6132101fc1edd19a16f/src/util/mod.rs#L142-L178","documentation":"`try_canonicalize` is a Windows-only helper (`#[cfg(windows)]`). `std::fs::canonicalize` on Windows can fail for various path forms, so the function falls back: first it re-checks existence with `try_exists()`; if the path does not exist it returns `io::Error::new(ErrorKind::NotFound, \"the path was not found\")`; otherwise it falls through to `std::path::absolute(&path)`. This preserves `canonicalize`'s behavior of erroring on non-existent paths while still returning an absolute path when canonicalization itself is the problem.","triggerScenarios":"Calling `try_canonicalize` on a Windows path where `std::fs::canonicalize` fails AND `path.as_ref().try_exists()?` returns `false` (the file genuinely does not exist). The explicit `NotFound` error is returned.","commonSituations":"Passing a non-existent path on Windows (typo, deleted file, not-yet-created build artifact, wrong working directory), or a path on a network/UNC location that cannot be resolved and does not exist.","solutions":["Verify the path exists (create the file/directory, or fix the typo) before canonicalizing.","Check the working directory and that build artifacts have been generated prior to this call.","For UNC/network paths, confirm the share is reachable and the path is correct."],"exampleFix":"// before (Windows)\nlet p = try_canonicalize(\"C:\\\\build\\\\missing.exe\")?; // NotFound\n\n// after\nstd::fs::create_dir_all(\"C:\\\\build\")?;\n// ensure the file exists first, then canonicalize\nlet p = try_canonicalize(\"C:\\\\build\\\\real.exe\")?;","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn exists_before_canon(p: &Path) -> std::io::Result<bool> { p.try_exists() }\n// on Windows, call this before try_canonicalize to surface a clear NotFound up front","typeGuard":null,"tryCatchPattern":"match try_canonicalize(path) {\n    Ok(p) => { /* use p */ }\n    Err(e) if e.kind() == std::io::ErrorKind::NotFound => {\n        // create the file/dir, or correct the path, then retry\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Ensure files/dirs exist before canonicalizing on Windows.","Generate build artifacts before resolving their absolute paths.","Validate working directory and share reachability for UNC paths."],"tags":["windows","path","canonicalize","not-found","cargo"],"backgroundTag":null,"analyzedSha":"98a09e7e7d62850f14e5b6132101fc1edd19a16f","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}