{"record":{"id":"22c076e98b799ab9","repo":"sinelaw/fresh","slug":"cannot-reload-buffer-has-unsaved-modifications","errorCode":null,"errorMessage":"Cannot reload: buffer has unsaved modifications","messagePattern":"Cannot reload: buffer has unsaved modifications","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/fresh-editor/src/app/file_open_orchestrators.rs","lineNumber":474,"sourceCode":"        let buffer_id = self.active_buffer();\n\n        // Get the file path\n        let path = self\n            .buffers()\n            .get(&buffer_id)\n            .and_then(|s| s.buffer.file_path().map(|p| p.to_path_buf()))\n            .ok_or_else(|| anyhow::anyhow!(\"Buffer has no file path\"))?;\n\n        // Check for unsaved modifications\n        if let Some(state) = self\n            .windows\n            .get(&self.active_window)\n            .map(|w| &w.buffers)\n            .expect(\"active window present\")\n            .get(&buffer_id)\n        {\n            if state.buffer.is_modified() {\n                anyhow::bail!(\"Cannot reload: buffer has unsaved modifications\");\n            }\n        }\n\n        // Reload the buffer with the new encoding\n        let new_buffer = crate::model::buffer::Buffer::load_from_file_with_encoding(\n            &path,\n            encoding,\n            Arc::clone(&self.authority().filesystem),\n            crate::model::buffer::BufferConfig {\n                estimated_line_length: self.config.editor.estimated_line_length,\n            },\n        )?;\n\n        // Update the buffer in the editor state\n        if let Some(state) = self\n            .windows\n            .get_mut(&self.active_window)\n            .map(|w| &mut w.buffers)","sourceCodeStart":456,"sourceCodeEnd":492,"githubUrl":"https://github.com/sinelaw/fresh/blob/67894ca5463dbd7a89bb31add4627c27d6b79d83/crates/fresh-editor/src/app/file_open_orchestrators.rs#L456-L492","documentation":"reload_with_encoding refuses to reload a buffer whose on-disk representation is about to be replaced when the in-memory buffer has unsaved edits. Reloading would destroy those modifications, so the operation is aborted before the file is re-read with the new encoding.","triggerScenarios":"Calling reload_with_encoding (via handle_reload_with_encoding) on a buffer where state.buffer.is_modified() is true, i.e. the user edited text without saving.","commonSituations":"User changes the encoding of a file they have already typed into; a script triggers an encoding reload on a dirty buffer; hotkey pressed after making edits.","solutions":["Save the buffer (or undo changes) before invoking reload_with_encoding.","Prompt the user to save or discard, then retry the reload.","If intentional data loss is desired, force-write or reset the buffer to unmodified state before reloading."],"exampleFix":"// before\neditor.reload_with_encoding(buffer_id, encoding)?;\n// after\nif editor.buffer_is_modified(buffer_id) {\n    editor.save()?; // or prompt the user\n}\neditor.reload_with_encoding(buffer_id, encoding)?;","handlingStrategy":"validation","validationCode":"if editor.buffer_is_modified(buffer_id) { prompt_save_or_discard(buffer_id)?; }","typeGuard":null,"tryCatchPattern":"if let Err(e) = editor.reload_with_encoding(id, enc) { if e.to_string().contains(\"unsaved modifications\") { show_save_prompt(id); } }","preventionTips":["Check is_modified() before any reload/reopen operation","Offer save/discard UI before changing encoding","Auto-save or checkpoint dirty buffers before risky operations"],"tags":["editor","buffer","unsaved-changes"],"backgroundTag":"invalid-state-transition","analyzedSha":"67894ca5463dbd7a89bb31add4627c27d6b79d83","analyzedAt":"2026-09-13T15:04:03.701Z","contentChangedAt":"2026-09-13T15:04:03.701Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}