{"record":{"id":"918b93438bc16278","repo":"linera-io/linera-protocol","slug":"file-is-empty-or-does-not-exist","errorCode":null,"errorMessage":"file is empty or does not exist: {}","messagePattern":"file is empty or does not exist: (.+?)","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"linera-persistent/src/file.rs","lineNumber":155,"sourceCode":"                    .write(true)\n                    .create(true)\n                    .open(path)?,\n            )\n            .map_err(|source| Error::Lock {\n                path: path.into(),\n                source,\n            })?,\n            path: path.into(),\n            value,\n        };\n        this.save()?;\n        Ok(this)\n    }\n\n    /// Reads the value from a file at `path`, returning an error if it does not exist.\n    pub fn read(path: &Path) -> Result<Self, Error> {\n        Self::read_or_create(path, || {\n            Err(std::io::Error::new(\n                std::io::ErrorKind::NotFound,\n                format!(\"file is empty or does not exist: {}\", path.display()),\n            )\n            .into())\n        })\n    }\n\n    /// Reads the value from a file at `path`, calling the `value` function to create it\n    /// if it does not exist. If it does exist, `value` will not be called.\n    pub fn read_or_create(\n        path: &Path,\n        value: impl FnOnce() -> Result<T, Error>,\n    ) -> Result<Self, Error> {\n        let lock = Lock::new(open_options().read(true).open(path)?)?;\n        let mut reader = io::BufReader::new(&lock.0);\n        let file_is_empty = reader.fill_buf()?.is_empty();\n\n        let me = Self {","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/linera-io/linera-protocol/blob/6c226ddcb332ef55118dc8d0aafbd093d5420899/linera-persistent/src/file.rs#L137-L173","documentation":"`linera_persistent::file::File::read` loads a JSON-persisted value (wallets, configs) from a path and is the strict variant of `read_or_create`: if the file is missing or zero bytes, the value-creation closure itself returns `std::io::ErrorKind::NotFound` with this message. The file is flock-locked and atomically rewritten through a `<path>.json.new` staging file, so an empty main file usually means it was never written or was truncated externally.","triggerScenarios":"Passing a wallet/config path that does not exist (typo, wrong `--wallet` argument); pointing at a fresh directory without running the creation step; a zero-byte file left by manual truncation or an interrupted external process.","commonSituations":"First run of a tool that expects an existing wallet instead of creating one; scripts referencing `$HOME` under a different user; renamed or moved data directories; CI starting with a clean environment.","solutions":["Verify the path exists and is non-empty before reading.","Create the file first with the tool's creation command, or use `File::read_or_create(path, || Ok(default_value))` instead of `File::read`.","If the file was expected to exist, restore it from backup — an empty file means there is no state to recover via `read`.","Check for a leftover `<path>.json.new` staging file if a crash interrupted a save."],"exampleFix":"// before\nlet wallet = File::<Wallet>::read(&path)?; // NotFound if path missing/empty\n\n// after\nlet wallet = File::<Wallet>::read_or_create(&path, || Ok(Wallet::default()))?;","handlingStrategy":"validation","validationCode":"use std::path::Path;\nfn readable(p: &Path) -> bool {\n    std::fs::metadata(p).map(|m| m.len() > 0).unwrap_or(false)\n}","typeGuard":null,"tryCatchPattern":"match File::<T>::read(&path) { Ok(f) => f, Err(e) if e.to_string().contains(\"file is empty or does not exist\") => File::read_or_create(&path, T::default)?, Err(e) => return Err(e.into()) }","preventionTips":["Run the wallet-creation step before any command that reads an existing wallet.","Default to read_or_create with an explicit default value for first-run paths.","Check for leftover .json.new staging files after crashes.","Guard scripts against empty-string path variables."],"tags":["persistence","file-io","wallet","file-not-found","json"],"backgroundTag":"file-not-found","analyzedSha":"6c226ddcb332ef55118dc8d0aafbd093d5420899","analyzedAt":"2026-08-22T22:49:09.787Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}