sinelaw/fresh · error

Cannot reload: buffer has unsaved modifications

Error message

Cannot reload: buffer has unsaved modifications

What it means

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.

Solutions

  1. Save the buffer (or undo changes) before invoking reload_with_encoding.
  2. Prompt the user to save or discard, then retry the reload.
  3. If intentional data loss is desired, force-write or reset the buffer to unmodified state before reloading.

Example fix

// before
editor.reload_with_encoding(buffer_id, encoding)?;
// after
if editor.buffer_is_modified(buffer_id) {
    editor.save()?; // or prompt the user
}
editor.reload_with_encoding(buffer_id, encoding)?;
Defensive patterns

Strategy: validation

Validate before calling

if editor.buffer_is_modified(buffer_id) { prompt_save_or_discard(buffer_id)?; }

Try / catch

if let Err(e) = editor.reload_with_encoding(id, enc) { if e.to_string().contains("unsaved modifications") { show_save_prompt(id); } }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of sinelaw/fresh@67894ca546 (2026-09-13). Data as JSON: /api/errors/22c076e98b799ab9. Report an issue: GitHub.

Appendix: source

Thrown at crates/fresh-editor/src/app/file_open_orchestrators.rs:474

        let buffer_id = self.active_buffer();

        // Get the file path
        let path = self
            .buffers()
            .get(&buffer_id)
            .and_then(|s| s.buffer.file_path().map(|p| p.to_path_buf()))
            .ok_or_else(|| anyhow::anyhow!("Buffer has no file path"))?;

        // Check for unsaved modifications
        if let Some(state) = self
            .windows
            .get(&self.active_window)
            .map(|w| &w.buffers)
            .expect("active window present")
            .get(&buffer_id)
        {
            if state.buffer.is_modified() {
                anyhow::bail!("Cannot reload: buffer has unsaved modifications");
            }
        }

        // Reload the buffer with the new encoding
        let new_buffer = crate::model::buffer::Buffer::load_from_file_with_encoding(
            &path,
            encoding,
            Arc::clone(&self.authority().filesystem),
            crate::model::buffer::BufferConfig {
                estimated_line_length: self.config.editor.estimated_line_length,
            },
        )?;

        // Update the buffer in the editor state
        if let Some(state) = self
            .windows
            .get_mut(&self.active_window)
            .map(|w| &mut w.buffers)

View on GitHub (pinned to 67894ca546)