astral-sh/ruff · error

could not find `pyproject.toml` in any ancestor of

Error message

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

What it means

`discover_project_directory` in `ty_completion_bench` walks up from the supplied file path looking for a `pyproject.toml` that defines the project root. If no ancestor directory contains one, the tool cannot determine the project to benchmark and bails with this message naming the input file.

Solutions

  1. Move the target file inside a directory that has a `pyproject.toml` (or its subtree)
  2. Create a `pyproject.toml` at the project root (`uv init` or a minimal `[project]` table)
  3. Verify the path passed on the command line is correct and inside the intended project
  4. If using a legacy setup.py project, add a minimal `pyproject.toml`

Example fix

// before
ty_completion_bench /tmp/scratch.py 10   # no pyproject.toml anywhere above /tmp
// after
mkdir -p ~/proj && cd ~/proj && uv init && cp /tmp/scratch.py .
ty_completion_bench ~/proj/scratch.py 10
Defensive patterns

Strategy: validation

Validate before calling

import pathlib
file = pathlib.Path("path/to/file.py").resolve()
assert any((p / "pyproject.toml").exists() for p in file.parents), f"no pyproject.toml above {file}"

Try / catch

match tool.run() {
    Err(e) if e.to_string().starts_with("could not find `pyproject.toml`") => {
        eprintln!("run the tool on a file inside a project with pyproject.toml");
        std::process::ExitCode::from(1)
    }
    other => other,
}

Prevention

When it happens

Trigger: Invoking `ty_completion_bench <file> <offset>` where `file` lives in a directory tree with no `pyproject.toml` in the file's directory or any parent (e.g. a standalone script in /tmp or a non-Python-project folder).

Common situations: Pointing the tool at an isolated .py scratch file outside any project; running on a newly created directory before `uv init`/`cargo`-style project setup; the project uses `setup.py`-only packaging without a `pyproject.toml`; the file path was mistyped so it resolves outside the project.

Related errors


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

Appendix: 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 5bb508f594)