Skip to content

Bug Investigation: Inconsistent .env Path in MoFA Vibe #1258

@Ananya-Kaushal137

Description

@Ananya-Kaushal137

Description

The vibe command provides an interactive way to generate agents and flows. During setup, if an OpenAI API key is missing, it prompts the user to enter and save it. However, the saving logic uses a different path than the loading logic, leading to "lost" API keys if the command is run from any directory other than the project root.

Loading Path: os.path.join(project_root, ".env")
Saving Path: os.path.join(os.getcwd(), ".env")

Impact

If a user runs mofa vibe from a subdirectory (e.g., examples/) and saves their API key, the key is written to examples/.env. When they subsequent run vibe again, the application looks for the key in project_root/.env, fails to find it, and prompts the user again, creating a frustrating experience and a fragmented state.

Reproduction

I created a reproduction script reproduce_vibe_bug.py that demonstrates this by:

Setting a mock project_root.
Changing the working directory to a different folder.
Invoking the API key setup function.
Verifying that the .env file was created in the current working directory instead of the project root.

Reproduction Log

Project root is: C:\Users\prems\OneDrive\Desktop\Mofa\mofa\repro_test\project
Current working directory is: C:\Users\prems\OneDrive\Desktop\Mofa\mofa\repro_test\other
Running _check_and_setup_api_key()...
Checking for .env files:
Expected path (project root): C:\Users\prems\OneDrive\Desktop\Mofa\mofa\repro_test\project\.env - MISSING
Actual path (CWD): C:\Users\prems\OneDrive\Desktop\Mofa\mofa\repro_test\other\.env - EXISTS
[BUG CONFIRMED] API key was saved to the current working directory instead of the project root!
Contents of .env: OPENAI_API_KEY=sk-test-api-key

Root Cause Analysis

The issue is located in 
vibe.py
 at line 121:

python
105: def _check_and_setup_api_key():

120:             if click.confirm("\nSave to .env file?", default=True):
121:                 env_file = os.path.join(os.getcwd(), '.env')
122: 
123:                 # Append to .env or create new one
124:                 with open(env_file, 'a') as f:

While other functions in the same file use the
_get_env_file_path()
helper (which correctly uses project_root), this specific section hardcodes os.getcwd().

Proposed Fix

Change line 121 to use the existing helper:

python
- env_file = os.path.join(os.getcwd(), '.env')
+ env_file = _get_env_file_path()

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions