helix-editor/helix · error · anyhow::Error

unknown encoding

Error message

unknown encoding

What it means

Document::set_encoding resolves the given label with encoding_rs::Encoding::for_label, which only accepts labels from the WHATWG Encoding Standard (e.g. "utf-8", "utf-16le", "windows-1252"). If the label matches none of them, for_label returns None and the method bails with "unknown encoding". This is the path behind Helix's :set-encoding command and encoding-related reloads.

Source

Thrown at helix-view/src/document.rs:1326

        self.append_changes_to_history(view);
        self.reset_modified();
        self.pickup_last_saved_time();
        self.detect_indent_and_line_ending();

        match provider_registry.get_diff_base(&path, trust_full) {
            Some(diff_base) => self.set_diff_base(diff_base),
            None => self.diff_handle = None,
        }

        self.version_control_head = provider_registry.get_current_head_name(&path, trust_full);

        Ok(())
    }

    /// Sets the [`Document`]'s encoding with the encoding correspondent to `label`.
    pub fn set_encoding(&mut self, label: &str) -> Result<(), Error> {
        let encoding =
            Encoding::for_label(label.as_bytes()).ok_or_else(|| anyhow!("unknown encoding"))?;

        self.encoding = encoding;

        Ok(())
    }

    /// Returns the [`Document`]'s current encoding.
    pub fn encoding(&self) -> &'static Encoding {
        self.encoding
    }

    /// sets the document path without sending events to various
    /// observers (like LSP), in most cases `Editor::set_doc_path`
    /// should be used instead
    pub fn set_path(&mut self, path: Option<&Path>) {
        let path = path.map(helix_stdx::path::canonicalize);

        // `take` to remove any prior relative path that may have existed.

View on GitHub (pinned to 079a789e8c)

Solutions

  1. Use a WHATWG-recognized label: "utf-8", "utf-16le", "utf-16be", "latin1" (windows-1252), "gbk", "shift_jis", etc.
  2. If unsure, validate the label first with encoding_rs::Encoding::for_label(label.as_bytes()); None means Helix will also reject it.
  3. Check the WHATWG Encoding Standard label table for the exact alias list before hardcoding a name.

Example fix

// before
 doc.set_encoding("unicode")?; // Err: unknown encoding

// after
 let label = "utf-8";
 debug_assert!(encoding_rs::Encoding::for_label(label.as_bytes()).is_some());
 doc.set_encoding(label)?;
Defensive patterns

Strategy: validation

Validate before calling

fn is_known_encoding(label: &str) -> bool {
    encoding_rs::Encoding::for_label(label.as_bytes()).is_some()
}

// before saving:
if !is_known_encoding(label) {
    return Err(anyhow!("unsupported encoding label '{label}'"));
}
doc.set_encoding(label)?;

Type guard

fn resolve_encoding(label: &str) -> Option<&'static encoding_rs::Encoding> {
    encoding_rs::Encoding::for_label(label.as_bytes())
}

Try / catch

if let Err(err) = doc.set_encoding(label) {
    // error is context-free ("unknown encoding"), so re-wrap with the label
    log::warn!("set_encoding('{label}') failed: {err:#}");
    editor.set_error(format!("unknown encoding '{label}'"));
}

Prevention

When it happens

Trigger: Calling doc.set_encoding(label) with a string that is not a WHATWG label: typos ("utf-88"), colloquial names ("unicode", "ansi"), or an encoding name that exists under IANA but not under the WHATWG label table. Also triggered by config or scripted callers that pass user input straight through without validation.

Common situations: A user types :set-encoding unicode or :set-encoding ascii expecting them to work; a config or script hardcodes an encoding name from a different spec; case or dash variants are fine for many labels but a genuinely unknown name reaches this branch.

Related errors


AI-assisted analysis of helix-editor/helix@079a789e8c (2026-08-16). Data as JSON: /api/errors/e6e07d487c52030c. Report an issue: GitHub.