{"record":{"id":"efbd062586120cb8","repo":"astral-sh/ruff","slug":"invalidutf8-efbd06","errorCode":null,"errorMessage":"InvalidUtf8","messagePattern":"InvalidUtf8","errorType":"validation","errorClass":"clap::Error","httpStatus":null,"severity":"error","filePath":"crates/ruff/src/commands/completions/config.rs","lineNumber":121,"sourceCode":"    type Parser = OptionStringParser;\n\n    fn value_parser() -> Self::Parser {\n        OptionStringParser\n    }\n}\n\nimpl TypedValueParser for OptionStringParser {\n    type Value = OptionString;\n\n    fn parse_ref(\n        &self,\n        cmd: &clap::Command,\n        arg: Option<&clap::Arg>,\n        value: &std::ffi::OsStr,\n    ) -> Result<Self::Value, clap::Error> {\n        let value = value\n            .to_str()\n            .ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?;\n\n        value.parse().map_err(|()| {\n            let mut error = clap::Error::new(clap::error::ErrorKind::ValueValidation).with_cmd(cmd);\n            if let Some(arg) = arg {\n                error.insert(\n                    clap::error::ContextKind::InvalidArg,\n                    clap::error::ContextValue::String(arg.to_string()),\n                );\n            }\n            error.insert(\n                clap::error::ContextKind::InvalidValue,\n                clap::error::ContextValue::String(value.to_string()),\n            );\n            error\n        })\n    }\n\n    fn possible_values(&self) -> Option<Box<dyn Iterator<Item = PossibleValue> + '_>> {","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff/src/commands/completions/config.rs#L103-L139","documentation":"This clap InvalidUtf8 error is raised in ruff's completion-config value parser (`parse_ref`) when the raw argument value passed on the command line is not valid UTF-8. The parser converts the `OsStr` to `&str` before further parsing and fails immediately with this error kind if conversion fails.","triggerScenarios":"Invoking ruff with an argument (e.g. a `--config`-style key=value flag handled by this parser) whose bytes are not valid UTF-8, typically by passing a binary-encoded value or a filename from a shell with non-UTF-8 locale.","commonSituations":"Shell environments with non-UTF-8 locales (e.g. LANG=C with latin-1 encoded filenames), scripting bugs that splice raw bytes into command lines, filenames produced by legacy Windows tooling.","solutions":["Re-run the command with a UTF-8 argument value","Set a UTF-8 locale (e.g. `export LANG=C.UTF-8`) so filenames expand correctly","Check the script generating the command line for byte-encoding mistakes"],"exampleFix":"// before\nlet value = value.to_str().ok_or_else(|| clap::Error::new(clap::error::ErrorKind::InvalidUtf8))?;\n// after (accept OsString when non-UTF-8 is legitimate)\nlet value = value.to_str().ok_or_else(|| clap::Error::raw(\n    clap::error::ErrorKind::InvalidUtf8,\n    \"argument value must be valid UTF-8\",\n))?;","handlingStrategy":"validation","validationCode":"value_str = os.fsdecode(raw_arg)\ntry:\n    value_str.encode(\"utf-8\")\nexcept UnicodeEncodeError:\n    raise SystemExit(\"argument must be valid UTF-8\")","typeGuard":"fn is_utf8(value: &std::ffi::OsStr) -> bool {\n    value.to_str().is_some()\n}","tryCatchPattern":"match result {\n    Err(e) if e.kind() == clap::error::ErrorKind::InvalidUtf8 => eprintln!(\"argument must be UTF-8\"),\n    Err(e) => return Err(e.into()),\n    Ok(v) => v,\n}","preventionTips":["Set a UTF-8 locale (LANG/LC_ALL=C.UTF-8) in shells and CI","Avoid splicing raw bytes into command lines; always decode/encode explicitly","Sanitize filenames from external tools before passing them as arguments"],"tags":["cli","encoding","utf8"],"backgroundTag":"invalid-utf8-argument","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}