gleam-lang/gleam · error

Non Utf-8 Path

Error message

Non Utf-8 Path

What it means

In the test `git_init_already_in_git`, `Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path")` panics if the temp directory path cannot be converted to a UTF-8 path. The conversion fails only when the OS temp path contains non-UTF-8 bytes.

Solutions

  1. Set TMPDIR to a UTF-8-safe directory when running tests: `TMPDIR=/tmp/utf8safe cargo test`.
  2. Rename the temp/username directory to contain only UTF-8 characters.
  3. Ignore/patch the test on non-UTF-8 filesystems, or make the helper return a Result instead of expecting.

Example fix

// before
let git = Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path").join(".git");
// after
let git = Utf8Path::from_path(tmp_dir.path())
    .ok_or_else(|| anyhow!("temp dir path is not UTF-8"))?
    .join(".git");
Defensive patterns

Strategy: validation

Validate before calling

if let Err(_) = std::str::from_utf8(tmp_dir.path().as_os_str().as_encoded_bytes()) {
    eprintln!("skipping: TMPDIR is not UTF-8");
    return;
}

Type guard

fn is_utf8_path(p: &std::path::Path) -> bool { p.to_str().is_some() }

Prevention

When it happens

Trigger: Running the test suite with TMPDIR (or the platform temp dir) set to a path containing non-UTF-8 bytes; the first expect at the `.git` join (compiler-cli/src/fs/tests.rs:37) panics before any assertion runs.

Common situations: CI machines or developer environments where the temp directory lives under a non-UTF-8 directory name (non-English locale setups, legacy-encoded usernames).

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


AI-assisted analysis of gleam-lang/gleam@15b07c7830 (2026-09-14). Data as JSON: /api/errors/a2109b898b76b7c5. Report an issue: GitHub.

Appendix: source

Thrown at compiler-cli/src/fs/tests.rs:37

    assert!(super::is_inside_git_work_tree(path).unwrap())
}

#[test]
fn git_init_success() {
    let tmp_dir = tempfile::tempdir().unwrap();
    let path = Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path");
    let git = path.join(".git");

    assert!(!git.exists());
    assert_eq!(super::git_init(path), Ok(()));
    assert!(git.exists());
}

#[test]
fn git_init_already_in_git() {
    let tmp_dir = tempfile::tempdir().unwrap();
    let git = Utf8Path::from_path(tmp_dir.path())
        .expect("Non Utf-8 Path")
        .join(".git");
    assert!(!git.exists());
    assert_eq!(
        super::git_init(Utf8Path::from_path(tmp_dir.path()).expect("Non Utf-8 Path")),
        Ok(())
    );
    assert!(git.exists());

    let sub = Utf8Path::from_path(tmp_dir.path())
        .expect("Non Utf-8 Path")
        .join("subproject");
    let git = sub.join(".git");
    crate::fs::mkdir(&sub).unwrap();
    assert!(!git.exists());
    assert_eq!(super::git_init(&sub), Ok(()));
    assert!(!git.exists());
}

View on GitHub (pinned to 15b07c7830)