Auto-detected project name is malformed when the program is in a sibling directory of the git repo
Bug description
wandb.util.auto_project_name() is used by wandb.init() and the CLI to pick a
project name from the enclosing git repository: <repo> for a program at the
repo root, <repo>-<subdir> for a program in a subdirectory.
The check that decides whether the program lives inside the repository is a plain string prefix test:
# wandb/util.py
prog_dir = os.path.dirname(os.path.abspath(program))
if not prog_dir.startswith(root_dir):
return str(repo_name)A sibling directory whose name shares a string prefix with the repo directory
satisfies startswith even though it is not inside the repo. The generated
project name then embeds a ../ relative path.
Reproduction
import os, subprocess, tempfile
import wandb.util
with tempfile.TemporaryDirectory() as td:
repo = os.path.join(td, "repo")
sibling = os.path.join(td, "repo2") # shares the "repo" prefix
os.makedirs(os.path.join(repo, "pkg"))
os.makedirs(sibling)
subprocess.run(["git", "init"], cwd=repo, check=True)
os.chdir(repo)
print(wandb.util.auto_project_name(os.path.join(repo, "pkg", "train.py"))) # repo-pkg
print(wandb.util.auto_project_name(os.path.join(sibling, "train.py"))) # repo-.._repo2 (!)The second call should return "repo" (the program is outside the repository),
but returns "repo-.._repo2".
Expected behavior
Programs outside the repository — including sibling directories that share a
name prefix — should yield the bare repo name, not a name containing ...
Environment
- wandb: current
main(commit 955991a) - Python 3.12, Linux
Source: wandb/wandb