{"id":"6be69f8283e5a471","repo":"rust-lang/rust","slug":"file-locks-not-supported-on-this-platform","errorCode":null,"errorMessage":"file locks not supported on this platform","messagePattern":"file locks not supported on this platform","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler/rustc_data_structures/src/flock/unsupported.rs","lineNumber":10,"sourceCode":"use std::io;\nuse std::path::Path;\n\n#[derive(Debug)]\npub struct Lock(());\n\nimpl Lock {\n    pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool) -> io::Result<Lock> {\n        let msg = \"file locks not supported on this platform\";\n        Err(io::Error::new(io::ErrorKind::Other, msg))\n    }\n\n    pub fn error_unsupported(_err: &io::Error) -> bool {\n        true\n    }\n}\n","sourceCodeStart":1,"sourceCodeEnd":17,"githubUrl":"https://github.com/rust-lang/rust/blob/22057b88b091743bc0fd8d592a9264f0a6951403/compiler/rustc_data_structures/src/flock/unsupported.rs#L1-L17","documentation":"Returned by Lock::new on platforms for which rustc_data_structures has no native file-locking backend. The cfg_select! in flock.rs only wires linux/redox/unix/windows to a real lock implementation; every other target falls through to the unsupported module, whose new() unconditionally returns Err(io::Error::new(ErrorKind::Other, msg)). The platform simply cannot honor the file-locking contract, so callers must treat any lock attempt as a hard failure.","triggerScenarios":"Any code path that calls rustc_data_structures::flock::Lock::new on a target_os that is not linux, redox, another unix, or windows (e.g. wasm32-wasi, wasm32-unknown-unknown, certain bare-metal or hermetic targets). The function does not consult its arguments and always returns the Err.","commonSituations":"Building or running rustc/rustdoc-tier tooling on wasm or an unsupported tier-3 target; compiling a tool that depends on rustc_data_structures for a target without OS file locks; attempting to use incremental compilation locking on such a target.","solutions":["Run the tooling on a supported target (any unix, windows, or redox) where Lock has a real impl","If you only need the crate as a library dependency, avoid code paths that take a Lock","For wasm tooling, replace the file-locking requirement with a higher-level coordination mechanism","Confirm the target triple is what you expect (rustc -vV) before assuming support"],"exampleFix":"// before\nuse rustc_data_structures::flock::Lock;\nlet _l = Lock::new(&p, true, false, true)?;  // Err on wasm\n// after: gate or skip locking on unsupported targets\n#[cfg(any(unix, windows))]\nlet _l = Lock::new(&p, true, false, true)?;","handlingStrategy":"fallback","validationCode":"// Detect platforms where rustc's flock is the `unsupported` impl, and avoid\n// relying on exclusive file locking there.\ncfg_if::cfg_if! {\n    if #[cfg(any(windows, unix))] {\n        const FLOCK_SUPPORTED: bool = true;\n    } else {\n        const FLOCK_SUPPORTED: bool = false;\n    }\n}\n\nfn can_use_file_lock() -> bool { FLOCK_SUPPORTED }\n\n// before calling any API that internally locks (e.g. incremental cache, dep graph):\n// if !can_use_file_lock() { switch_to_lock_free_mode(); }","typeGuard":"// Capability probe: try a real flock on a temp file; if it returns the\n// \"unsupported\" path it will error immediately.\nuse std::fs::File;\nuse fs2::FileExt;\n\nfn flock_supported() -> bool {\n    let tmp = std::env::temp_dir().join(\".flock_probe\");\n    match File::create(&tmp) {\n        Ok(f) => match f.try_lock_exclusive() {\n            Ok(()) => { let _ = f.unlock(); true }\n            Err(_) => false,\n        },\n        Err(_) => false,\n    }\n}","tryCatchPattern":"use rustc_data_structures::flock;\n\nfn acquire_or_fallback(path: &Path) -> LockHandle {\n    if flock_supported() {\n        match flock::Lock::new(path) {\n            Ok(g) => LockHandle::Exclusive(g),\n            Err(_) => LockHandle::Fallback,\n        }\n    } else {\n        // platform has no flock; fall back to a process-level guard so the\n        // rest of the pipeline still runs single-writer.\n        LockHandle::Fallback\n    }\n}\n\nenum LockHandle {\n    Exclusive(rustc_data_structures::flock::Lock),\n    Fallback,\n}","preventionTips":["Target only tier-1 platforms (unix, windows) if you depend on incremental compilation or other file-lock-backed features.","On unsupported platforms (e.g. wasm, some embedded/bare-metal, certain cloud runtimes) disable features that need locking rather than letting them panic.","Gate the feature behind a cfg attribute so unsupported targets compile without it instead of failing at runtime.","Provide a single-writer/process-wide guard as a fallback when OS flock is unavailable.","Document in your build setup that concurrent rustc invocations writing to the same incremental cache are unsafe without flock."],"tags":["platform","file-lock","portability","unsupported"],"analyzedSha":"22057b88b091743bc0fd8d592a9264f0a6951403","analyzedAt":"2026-08-03T08:09:25.915Z","schemaVersion":2}