{"record":{"id":"65e21b03ea9e8bc3","repo":"pemistahl/grex","slug":"the-specified-file-s-encoding-is-not-valid-utf-8","errorCode":null,"errorMessage":"The specified file's encoding is not valid UTF-8","messagePattern":"The specified file's encoding is not valid UTF-8","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/builder.rs","lineNumber":78,"sourceCode":"    /// Each test case needs to be on a separate line.\n    /// Lines may be ended with either a newline (`\\n`) or\n    /// a carriage return with a line feed (`\\r\\n`).\n    /// The final line ending is optional.\n    ///\n    /// ⚠ Panics if:\n    /// - the file cannot be found\n    /// - the file's encoding is not valid UTF-8 data\n    /// - the file cannot be opened because of conflicting permissions\n    pub fn from_file<T: Into<PathBuf>>(file_path: T) -> Self {\n        match std::fs::read_to_string(file_path.into()) {\n            Ok(file_content) => Self {\n                test_cases: file_content.lines().map(|it| it.to_string()).collect_vec(),\n                config: RegExpConfig::new(),\n            },\n            Err(error) => match error.kind() {\n                ErrorKind::NotFound => panic!(\"The specified file could not be found\"),\n                ErrorKind::InvalidData => {\n                    panic!(\"The specified file's encoding is not valid UTF-8\")\n                }\n                ErrorKind::PermissionDenied => {\n                    panic!(\"Permission denied: The specified file could not be opened\")\n                }\n                _ => panic!(\"{}\", error),\n            },\n        }\n    }\n\n    /// Converts any Unicode decimal digit to character class `\\d`.\n    ///\n    /// This method takes precedence over\n    /// [`with_conversion_of_words`](Self::with_conversion_of_words) if both are set.\n    /// Decimal digits are converted to `\\d`, the remaining word characters to `\\w`.\n    ///\n    /// This method takes precedence over\n    /// [`with_conversion_of_non_whitespace`](Self::with_conversion_of_non_whitespace) if both are set.\n    /// Decimal digits are converted to `\\d`, the remaining non-whitespace characters to `\\S`.","sourceCodeStart":60,"sourceCodeEnd":96,"githubUrl":"https://github.com/pemistahl/grex/blob/99cc347707375e00514f938b76d885a201dfc110/src/builder.rs#L60-L96","documentation":"RegExpBuilder::from_file requires the input file to be valid UTF-8 because std::fs::read_to_string decodes it as such. When the underlying io::Error kind is InvalidData, the builder panics with 'The specified file's encoding is not valid UTF-8'. Non-UTF-8 encodings (e.g. Latin-1, UTF-16, or arbitrary binaries) cannot be read this way.","triggerScenarios":"Calling RegExpBuilder::from_file on a file saved in UTF-16, Windows-1252/Latin-1, or a binary file; files produced by tools that default to a legacy codepage.","commonSituations":"Windows-edited files exported in a legacy codepage; test case lists converted through Excel or PowerShell (UTF-16 by default); accidentally pointing the builder at a binary blob.","solutions":["Re-save the file as UTF-8 (no BOM needed) and retry","Convert the file's encoding explicitly (iconv -f UTF-16 -t UTF-8, or read with encoding_rs and re-encode)","Read bytes yourself with std::fs::read, decode with String::from_utf8 (or a transcoder), then pass the lines to RegExpBuilder::from"],"exampleFix":"// before\nlet builder = RegExpBuilder::from_file(\"examples_latin1.txt\");\n// after\nlet bytes = std::fs::read(\"examples_latin1.txt\")?;\nlet text = String::from_utf8_lossy(&bytes);\nlet cases: Vec<&str> = text.lines().collect();\nlet builder = RegExpBuilder::from(&cases);","handlingStrategy":"validation","validationCode":"let bytes = std::fs::read(path)?;\nif String::from_utf8(bytes.clone()).is_err() { return Err(anyhow!(\"file is not valid UTF-8: {}\", path)); }","typeGuard":null,"tryCatchPattern":"std::panic::catch_unwind(|| RegExpBuilder::from_file(path))\n    .map_err(|_| format!(\"examples file is not UTF-8: {}\", path.display()))","preventionTips":["Enforce UTF-8 encoding at file creation/export time","Run `file` or a BOM check on generated input files in CI","Prefer reading bytes yourself and transcoding so you control the error message"],"tags":["panic","encoding","utf-8","file"],"backgroundTag":"invalid-file-encoding","analyzedSha":"99cc347707375e00514f938b76d885a201dfc110","analyzedAt":"2026-09-13T15:42:56.144Z","contentChangedAt":"2026-09-13T15:42:56.144Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}