From e18a5b40ebf803da541f716553c334be4a155264 Mon Sep 17 00:00:00 2001 From: Adam Banky Date: Mon, 12 Jan 2026 02:32:09 +0100 Subject: [PATCH] fix(cli): load .env file from project root, not working directory Users expect ANTHROPIC_API_KEY from .env in project root to be used when running weft commands from subdirectories. Previously, load_dotenv() only looked in the current working directory. Changes: - Updated main.py to find project root and load .env from there - Falls back to current directory if project root not found - Maintains backward compatibility with non-weft projects --- src/weft/cli/main.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/weft/cli/main.py b/src/weft/cli/main.py index a195748..16b4682 100644 --- a/src/weft/cli/main.py +++ b/src/weft/cli/main.py @@ -111,10 +111,23 @@ def cli(ctx, config: str | None, verbose: bool): # type: ignore[no-untyped-def] load_dotenv(config) else: - # Try loading from default .env file + # Try loading from default .env file in project root + from dotenv import load_dotenv - load_dotenv() + from weft.utils.project import get_project_root + + try: + project_root = get_project_root() + env_path = project_root / ".env" + if env_path.exists(): + load_dotenv(dotenv_path=env_path) + else: + # Fallback to current directory for projects not initialized with weft + load_dotenv() + except Exception: + # If we can't find project root, try current directory + load_dotenv() # Validate environment for most commands (except --version, --help) if ctx.invoked_subcommand not in [None]: