{"record":{"id":"dc094a4c545a3f7f","repo":"getzola/zola","slug":"could-not-convert-filename-to-str","errorCode":null,"errorMessage":"Could not convert filename to &str","messagePattern":"Could not convert filename to &str","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cmd/init.rs","lineNumber":52,"sourceCode":"// This is a workaround until this issue https://github.com/rust-lang/rust/issues/42869 was fixed.\nconst LOCAL_UNC: &str = \"\\\\\\\\?\\\\\";\n\n// Given a path, return true if it is a directory and it doesn't have any\n// non-hidden files, otherwise return false (path is assumed to exist)\npub fn is_directory_quasi_empty(path: &Path) -> Result<bool> {\n    if path.is_dir() {\n        let mut entries = match path.read_dir() {\n            Ok(entries) => entries,\n            Err(e) => {\n                bail!(\"Could not read `{}` because of error: {}\", path.to_string_lossy(), e);\n            }\n        };\n        // If any entry raises an error or isn't hidden (i.e. starts with `.`), we raise an error\n        if entries.any(|x| match x {\n            Ok(file) => !file\n                .file_name()\n                .to_str()\n                .expect(\"Could not convert filename to &str\")\n                .starts_with('.'),\n            Err(_) => true,\n        }) {\n            return Ok(false);\n        }\n        return Ok(true);\n    }\n\n    Ok(false)\n}\n\n// Remove the unc part of a windows path\nfn strip_unc(path: &Path) -> String {\n    let path_to_refine = path.to_str().unwrap();\n    path_to_refine.trim_start_matches(LOCAL_UNC).to_string()\n}\n\npub fn create_new_project(name: &str, force: bool) -> Result<()> {","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/getzola/zola/blob/61d30828217957120309db657950224edf9707e2/src/cmd/init.rs#L34-L70","documentation":"is_directory_quasi_empty iterates directory entries and converts each file_name OsString to &str with .expect, panicking if a filename is not valid UTF-8. On Unix filenames are arbitrary bytes, so any non-UTF-8 entry in the target directory aborts `zola init`.","triggerScenarios":"Running `zola init` in (or pointing init at) a directory containing a file whose name is not valid UTF-8, e.g. created by tools using legacy encodings or raw bytes.","commonSituations":"Projects synced from Windows/macOS with unusual names, files created by other programs with byte-level names, extracted archives with latin-1 filenames.","solutions":["Rename or remove the non-UTF-8 file in the target directory","Use to_string_lossy() instead of to_str().expect() so odd names are treated as visible (non-hidden) entries","Pre-validate the directory with a script that lists non-UTF-8 names (e.g. `find . | grep -P '[^\\x00-\\x7F]'`)"],"exampleFix":"// before\nOk(file) => !file\n    .file_name()\n    .to_str()\n    .expect(\"Could not convert filename to &str\")\n    .starts_with('.'),\n// after\nOk(file) => !file\n    .file_name()\n    .to_string_lossy()\n    .starts_with('.'),","handlingStrategy":"validation","validationCode":"// before running zola init, check for non-UTF-8 filenames\nuse std::ffi::OsStr;\nuse std::os::unix::ffi::OsStrExt;\nfor entry in std::fs::read_dir(\".\")? {\n    let name = entry?.file_name();\n    if std::str::from_utf8(name.as_bytes()).is_err() {\n        eprintln!(\"non-UTF-8 filename: {:?}\", name);\n    }\n}","typeGuard":"fn is_utf8_name(name: &std::ffi::OsStr) -> bool {\n    use std::os::unix::ffi::OsStrExt;\n    std::str::from_utf8(name.as_bytes()).is_ok()\n}","tryCatchPattern":null,"preventionTips":["Use to_string_lossy() when only a heuristic check (hidden-file detection) is needed","Avoid extracting archives with legacy encodings into the target directory","Normalize filenames to UTF-8 before pointing tools at a directory"],"tags":["rust","panic","utf-8","filesystem","filename"],"backgroundTag":"non-utf8-filename","analyzedSha":"61d30828217957120309db657950224edf9707e2","analyzedAt":"2026-09-03T14:39:09.727Z","contentChangedAt":"2026-09-03T14:39:09.727Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}