21 lines
623 B
Python
21 lines
623 B
Python
"""Workspace root detection: walk up from a file to find language-specific markers."""
|
|
|
|
import os
|
|
from pathlib import Path
|
|
|
|
|
|
def find_root(filepath: str, markers: list[str], fallback: str | None = None) -> str:
|
|
"""Walk up from filepath looking for any marker file/dir. Return the directory containing it."""
|
|
p = Path(filepath).resolve()
|
|
if p.is_file():
|
|
p = p.parent
|
|
while True:
|
|
for marker in markers:
|
|
if (p / marker).exists():
|
|
return str(p)
|
|
parent = p.parent
|
|
if parent == p:
|
|
break
|
|
p = parent
|
|
return fallback or os.getcwd()
|