slint-ui/slint · error · std::io::Error

Formatting failed

Error message

Formatting {} failed

What it means

In tools/lsp/fmt/tool.rs (process_file, called from run), after formatting (or copying) a file, any write error with kind InvalidData is replaced by a generic io::Error labeled "Formatting {path} failed". It signals that the formatted output could not be written — typically because the source was not valid UTF-8 (InvalidData is what std::fs::write returns for non-UTF-8 content on text conversion) or the formatted result was rejected. The original error's message is discarded, so the true cause is only available via the error kind before wrapping.

Solutions

  1. Re-save/convert the file to UTF-8 (e.g. in the editor: 'Save with Encoding > UTF-8', or iconv -f WINDOWS-1252 -t UTF-8 file.slint > file.slint).
  2. Verify the file is not binary and has the expected text extension so it goes through the formatter path.
  3. If you control the code, include the underlying error in the wrapped message for diagnosability: format!("Formatting {} failed: {e}", path.display()).

Example fix

// before
Err(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(std::io::Error::new(
    std::io::ErrorKind::InvalidData,
    format!("Formatting {} failed", path.display()),
)),
// after
Err(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(std::io::Error::new(
    std::io::ErrorKind::InvalidData,
    format!("Formatting {} failed: {e} (is the file valid UTF-8?)", path.display()),
)),
Defensive patterns

Strategy: validation

Validate before calling

// before running the formatter
let bytes = std::fs::read(path)?;
if std::str::from_utf8(&bytes).is_err() {
    eprintln!("{} is not valid UTF-8; convert before formatting", path.display());
    return Ok(()); // skip formatting
}

Type guard

fn is_utf8_file(path: &Path) -> bool {
    std::fs::read(path).map(|b| std::str::from_utf8(&b).is_ok()).unwrap_or(false)
}

Try / catch

match process_file(path) {
    Err(e) if e.kind() == std::io::ErrorKind::InvalidData => eprintln!("Skipping {}: not valid UTF-8", path.display()),
    Err(e) => return Err(e),
    Ok(()) => {}
}

Prevention

When it happens

Trigger: Running the LSP formatter on a file whose content is not valid UTF-8 (e.g. Latin-1 encoded .slint file, or a binary file with a registered extension), so fs write/read with String conversion yields io::ErrorKind::InvalidData and process_file rewraps it.

Common situations: A user opens a legacy or tool-generated .slint file saved in a non-UTF-8 encoding (Windows-1252, GBK) and triggers format-on-save in the editor; the LSP fails to format it.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of slint-ui/slint@bb937076de (2026-09-16). Data as JSON: /api/errors/200ec4f74ed6ceee. Report an issue: GitHub.

Appendix: source

Thrown at tools/lsp/fmt/tool.rs:138

    path: &std::path::Path,
    mut file: impl Write,
) -> std::io::Result<()> {
    let result = match path.extension() {
        Some(ext) if ext == "rs" => process_rust_file(source, file),
        Some(ext) if ext == "md" => process_markdown_file(source, file),
        Some(ext) if ext == "slint" => process_slint_file(source, path, file),
        _ => {
            // This allows usage like `cat x.slint | slint-lsp format /dev/stdin`
            if path == Path::new("/dev/stdin") {
                return process_slint_file(source, path, file);
            }
            // With other file types, we just output them in their original form.
            file.write_all(source.as_bytes())
        }
    };
    match result {
        Ok(()) => Ok(()),
        Err(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(std::io::Error::new(
            std::io::ErrorKind::InvalidData,
            format!("Formatting {} failed", path.display()),
        )),
        Err(e) => Err(e),
    }
}

View on GitHub (pinned to bb937076de)