astral-sh/ruff · error

Temporary directory path is not valid UTF-8

Error message

Temporary directory path is not valid UTF-8: {}

What it means

After creating a temporary directory, the evaluator converts its path to a `SystemPath`, which requires valid UTF-8. If the OS temp path (e.g. TMPDIR-derived) is not valid UTF-8, this error aborts the run.

Solutions

  1. Set TMPDIR to a valid UTF-8 path (e.g. `TMPDIR=/tmp/evaltmp`) and rerun
  2. Rename or recreate the offending temp directory with a UTF-8-safe name

Example fix

# before
TMPDIR=/tmp/w$(printf '\xff') cargo run -p ty_completion_eval -- all
// after
TMPDIR=/tmp/evaltmp cargo run -p ty_completion_eval -- all
Defensive patterns

Strategy: validation

Validate before calling

import os, sys
tmp = os.environ.get('TMPDIR', '/tmp')
try:
    tmp.encode('utf-8')
except UnicodeEncodeError:
    sys.exit(f"TMPDIR {tmp!r} is not UTF-8; set TMPDIR to a UTF-8 path")

Try / catch

match eval_main() {
    Err(e) if e.to_string().contains("Temporary directory path is not valid UTF-8") => {
        std::env::set_var("TMPDIR", "/tmp/utf8safe");
        // retry once
    }
    result => result,
}

Prevention

When it happens

Trigger: Running ty_completion_eval with TMPDIR set to a non-UTF-8 path, or systems whose default temp directory contains invalid UTF-8 bytes.

Common situations: Custom TMPDIR exports with byte-level names; non-UTF-8 container mount points used for /tmp.

Related errors


AI-assisted analysis of astral-sh/ruff@5bb508f594 (2026-09-05). Data as JSON: /api/errors/80980de906265a74. Report an issue: GitHub.

Appendix: source

Thrown at crates/ty_completion_eval/src/main.rs:159

    let truth = cwd.join("crates").join("ty_completion_eval").join("truth");
    anyhow::ensure!(
        truth.as_std_path().exists(),
        "{truth} does not exist: ty's completion evaluation must be run from the root \
         of the ruff repository",
        truth = truth.as_std_path().display(),
    );

    // The temporary directory at which we copy our truth
    // data to. We do this because we can't use the truth
    // data as-is with its `<CURSOR>` annotations (and perhaps
    // any other future annotations we add).
    let mut tmp_eval_dir = tempfile::Builder::new()
        .prefix("ty-completion-eval-")
        .tempdir()
        .context("Failed to create temporary directory")?;
    let tmp_eval_path = SystemPath::from_std_path(tmp_eval_dir.path())
        .ok_or_else(|| {
            anyhow::anyhow!(
                "Temporary directory path is not valid UTF-8: {}",
                tmp_eval_dir.path().display()
            )
        })?
        .to_path_buf();

    let sources = TaskSource::all(&truth)?;
    match args.command {
        Command::ShowOne(ref cmd) => {
            tmp_eval_dir.disable_cleanup(cmd.keep_tmp_dir);

            let Some(source) = sources
                .iter()
                .find(|source| cmd.matches_source_task(source))
            else {
                anyhow::bail!("could not find task named `{}`", cmd.task_name);
            };
            let tasks = source.to_tasks(&tmp_eval_path)?;

View on GitHub (pinned to 5bb508f594)