{"record":{"id":"1619996927e3e1f6","repo":"gleam-lang/gleam","slug":"non-utf8-path-fs","errorCode":null,"errorMessage":"Non Utf8 Path","messagePattern":"Non Utf8 Path","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"compiler-cli/src/fs.rs","lineNumber":881,"sourceCode":"        Ok(_) => Ok(()),\n        Err(err) => match err {\n            Error::ShellProgramNotFound { .. } => Ok(()),\n            _ => Err(Error::GitInitialization {\n                error: err.to_string(),\n            }),\n        },\n    }\n}\n\npub fn canonicalise(path: &Utf8Path) -> Result<Utf8PathBuf, Error> {\n    std::fs::canonicalize(path)\n        .map_err(|err| Error::FileIo {\n            action: FileIoAction::Canonicalise,\n            kind: FileKind::File,\n            path: Utf8PathBuf::from(path),\n            err: Some(err.to_string()),\n        })\n        .map(|pb| Utf8PathBuf::from_path_buf(pb).expect(\"Non Utf8 Path\"))\n}\n\n#[derive(Debug, Clone, Copy)]\npub struct ConsoleWarningEmitter;\n\nimpl WarningEmitterIO for ConsoleWarningEmitter {\n    fn emit_warning(&self, warning: Warning) {\n        let buffer_writer = crate::cli::stderr_buffer_writer();\n        let mut buffer = buffer_writer.buffer();\n        warning.pretty(&mut buffer);\n        buffer_writer\n            .print(&buffer)\n            .expect(\"Writing warning to stderr\");\n    }\n}\n\n/// Returns root of Git repository in base path if it is initialised.\npub fn get_git_repository_root(mut path: Utf8PathBuf) -> Option<Utf8PathBuf> {","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/gleam-lang/gleam/blob/15b07c783065c92bb016b1b74ed0995a5259850f/compiler-cli/src/fs.rs#L863-L899","documentation":"This panic comes from an `.expect(\"Non Utf8 Path\")` after canonicalising a file path and converting the std PathBuf back to a Utf8PathBuf. The gleam CLI assumes all paths it handles are valid UTF-8; if the canonicalised path contains bytes that are not valid UTF-8, `Utf8PathBuf::from_path_buf` returns Err and the expect panics. It is an internal invariant check, not a recoverable error.","triggerScenarios":"Calling the fs helper (canonicalise path around compiler-cli/src/fs.rs:881) with a path that, once canonicalised (symlinks resolved), resolves to a location whose name contains non-UTF-8 bytes, e.g. a symlink pointing into a directory with non-UTF-8 characters, on a filesystem using arbitrary byte sequences in names.","commonSituations":"Developers on Linux with filenames created in non-UTF-8 locales, or projects symlinked to directories with unusual byte-encoded names; macOS/Windows users rarely hit it since those filesystems enforce UTF-8-ish encodings.","solutions":["Rename the offending file or directory so its path is valid UTF-8 (e.g. `convmv --notest -f latin1 -t utf8 <path>`).","Check where the symlink resolves (`readlink -f <path>`) and ensure the target path is UTF-8.","Run the compiler with a UTF-8 locale (e.g. LANG=C.UTF-8) so tool-created names are UTF-8.","Patch the call site to use `to_string_lossy` or return an Error::FileIo instead of expecting, if you maintain a fork."],"exampleFix":"// before\n.map(|pb| Utf8PathBuf::from_path_buf(pb).expect(\"Non Utf8 Path\"))\n// after\n.map(|pb| Utf8PathBuf::from_path_buf(pb).map_err(|_| Error::FileIo {\n    action: FileIoAction::Canonicalise,\n    kind: FileKind::File,\n    path: Utf8PathBuf::from(path),\n    err: Some(\"path is not valid UTF-8\".into()),\n}))","handlingStrategy":"validation","validationCode":"fn ensure_utf8_path(path: &std::path::Path) -> Result<Utf8PathBuf, String> {\n    path.to_str()\n        .map(|s| Utf8PathBuf::from(s.to_owned()))\n        .ok_or_else(|| format!(\"path {:?} is not valid UTF-8\", path))\n}","typeGuard":"fn is_utf8_path(p: &std::path::Path) -> bool { p.to_str().is_some() }","tryCatchPattern":null,"preventionTips":["Keep project and temp paths ASCII/UTF-8; avoid non-UTF-8 locale filenames","Resolve symlinks with `readlink -f` and check the target encoding when paths come from untrusted sources","Use LossyString conversion or explicit Result-returning conversions instead of expect in library code","Set LANG/LC_ALL to a UTF-8 locale in build environments"],"tags":["rust","filesystem","panic","encoding"],"backgroundTag":"internal-invariant-violation","analyzedSha":"15b07c783065c92bb016b1b74ed0995a5259850f","analyzedAt":"2026-09-14T11:14:59.388Z","contentChangedAt":"2026-09-14T11:14:59.388Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}