astral-sh/ruff · error · anyhow::Error

could not find `pyproject.toml` in any ancestor of `{file}`

Error message

could not find `pyproject.toml` in any ancestor of `{file}`

What it means

ty_completion_bench requires each benchmarked file to live inside a project: `discover_project_directory` walks the canonicalized ancestors looking for a `pyproject.toml` and bails when none exists. The file's own name never counts — discovery only inspects directories above it.

Source

Thrown at crates/ty_completion_bench/src/main.rs:162

        CompletionCapabilities::default(),
        db.program_file(file),
        offset,
    ))
}

fn discover_project_directory(file: &SystemPath) -> anyhow::Result<SystemPathBuf> {
    for ancestor in file.as_std_path().canonicalize()?.ancestors() {
        if ancestor.join("pyproject.toml").exists() {
            return SystemPathBuf::from_path_buf(ancestor.to_path_buf()).map_err(|path| {
                anyhow!(
                    "Detected project directory `{path}` contains non-Unicode characters. \
                     ty only supports Unicode paths.",
                    path = path.display()
                )
            });
        }
    }
    anyhow::bail!("could not find `pyproject.toml` in any ancestor of `{file}`")
}

View on GitHub (pinned to 672bb4edf0)

Solutions

  1. Create a minimal `pyproject.toml` at the scratch project root and benchmark files under it
  2. Move the file into an existing project that already has a `pyproject.toml`
  3. For ad-hoc completion behavior checks, write an mdtest instead of using the bench harness

Example fix

# before
cargo run -p ty_completion_bench -- /tmp/scratch/snippet.py
# after
printf '[project]\nname = "scratch"\nversion = "0"\n' > /tmp/scratch/pyproject.toml
cargo run -p ty_completion_bench -- /tmp/scratch/snippet.py
Defensive patterns

Strategy: validation

Validate before calling

d=$(dirname "$(realpath "$FILE")")
while [ "$d" != "/" ]; do
  [ -f "$d/pyproject.toml" ] && break
  d=$(dirname "$d")
done
[ -f "$d/pyproject.toml" ] || { echo "no pyproject.toml above $FILE" >&2; exit 2; }

Prevention

When it happens

Trigger: Passing a standalone scratch file (e.g. under /tmp) with no `pyproject.toml` in any parent directory; benchmarking a file from a pre-PEP-621 project that only has `setup.py`/`requirements.txt`.

Common situations: Quick completion experiments on snippets outside any repo; vendored code dropped into a directory without a project manifest.

Related errors


AI-assisted analysis of astral-sh/ruff@672bb4edf0 (2026-08-16). Data as JSON: /api/errors/899ff20f6b911002. Report an issue: GitHub.