{"record":{"id":"200ec4f74ed6ceee","repo":"slint-ui/slint","slug":"formatting-failed","errorCode":null,"errorMessage":"Formatting {} failed","messagePattern":"Formatting (.+?) failed","errorType":"exception","errorClass":"std::io::Error","httpStatus":null,"severity":"error","filePath":"tools/lsp/fmt/tool.rs","lineNumber":138,"sourceCode":"    path: &std::path::Path,\n    mut file: impl Write,\n) -> std::io::Result<()> {\n    let result = match path.extension() {\n        Some(ext) if ext == \"rs\" => process_rust_file(source, file),\n        Some(ext) if ext == \"md\" => process_markdown_file(source, file),\n        Some(ext) if ext == \"slint\" => process_slint_file(source, path, file),\n        _ => {\n            // This allows usage like `cat x.slint | slint-lsp format /dev/stdin`\n            if path == Path::new(\"/dev/stdin\") {\n                return process_slint_file(source, path, file);\n            }\n            // With other file types, we just output them in their original form.\n            file.write_all(source.as_bytes())\n        }\n    };\n    match result {\n        Ok(()) => Ok(()),\n        Err(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(std::io::Error::new(\n            std::io::ErrorKind::InvalidData,\n            format!(\"Formatting {} failed\", path.display()),\n        )),\n        Err(e) => Err(e),\n    }\n}\n","sourceCodeStart":120,"sourceCodeEnd":145,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/tools/lsp/fmt/tool.rs#L120-L145","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["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).","Verify the file is not binary and has the expected text extension so it goes through the formatter path.","If you control the code, include the underlying error in the wrapped message for diagnosability: format!(\"Formatting {} failed: {e}\", path.display())."],"exampleFix":"// before\nErr(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(std::io::Error::new(\n    std::io::ErrorKind::InvalidData,\n    format!(\"Formatting {} failed\", path.display()),\n)),\n// after\nErr(e) if e.kind() == std::io::ErrorKind::InvalidData => Err(std::io::Error::new(\n    std::io::ErrorKind::InvalidData,\n    format!(\"Formatting {} failed: {e} (is the file valid UTF-8?)\", path.display()),\n)),","handlingStrategy":"validation","validationCode":"// before running the formatter\nlet bytes = std::fs::read(path)?;\nif std::str::from_utf8(&bytes).is_err() {\n    eprintln!(\"{} is not valid UTF-8; convert before formatting\", path.display());\n    return Ok(()); // skip formatting\n}","typeGuard":"fn is_utf8_file(path: &Path) -> bool {\n    std::fs::read(path).map(|b| std::str::from_utf8(&b).is_ok()).unwrap_or(false)\n}","tryCatchPattern":"match process_file(path) {\n    Err(e) if e.kind() == std::io::ErrorKind::InvalidData => eprintln!(\"Skipping {}: not valid UTF-8\", path.display()),\n    Err(e) => return Err(e),\n    Ok(()) => {}\n}","preventionTips":["Configure editors to save .slint files as UTF-8 (files.encoding setting).","Lint CI for non-UTF-8 files with `file` or a byte-level check before they reach the formatter.","Never point the formatter at binary or generated non-text files."],"tags":["file-io","encoding","lsp","formatter"],"backgroundTag":"file-write-failed","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}