Skip to content

fix(vibe): handle whitespace around = in .env keys to prevent duplicate entries - #1653

Open
SH20RAJ wants to merge 1 commit into
mofa-org:mainfrom
SH20RAJ:fix/1369-vibe-env-whitespace-keys
Open

fix(vibe): handle whitespace around = in .env keys to prevent duplicate entries#1653
SH20RAJ wants to merge 1 commit into
mofa-org:mainfrom
SH20RAJ:fix/1369-vibe-env-whitespace-keys

Conversation

@SH20RAJ

@SH20RAJ SH20RAJ commented Apr 20, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes #1369.

mofa vibe fails to correctly update existing .env entries when the
file uses the KEY = value style (spaces around =). The old key is
kept and a new duplicate entry is appended, leading to
non-deterministic configuration and confusing multi-value .env files.

Root Cause

_save_vibe_config matched existing keys with:

stripped = line.strip()
if model and stripped.startswith("MOFA_VIBE_MODEL="):

startswith("KEY=") requires the = to be directly adjacent to the
key with no whitespace. A file containing

MOFA_VIBE_MODEL = gpt-4o-mini

is never matched, so the function falls through to the append path and
creates a duplicate:

MOFA_VIBE_MODEL = gpt-4o-mini   ← original (stale)
MOFA_VIBE_MODEL=gpt-4           ← new duplicate appended

The first value may win depending on which parser reads the file,
making the bug user-visible but non-deterministic.

Fix

Replace startswith() with a compiled regex that tolerates optional
leading whitespace and optional whitespace on both sides of =:

import re
_key_re = {k: re.compile(r"^\s*" + re.escape(k) + r"\s*=") for k in updated}

for i, line in enumerate(lines):
    if model and _key_re["MOFA_VIBE_MODEL"].match(line):
        lines[i] = f"MOFA_VIBE_MODEL={model}\n"   # update in-place
        updated["MOFA_VIBE_MODEL"] = True

This handles:

Format Before After
KEY=value ✅ matched ✅ matched
KEY = value ❌ missed → duplicate ✅ matched
KEY= value ❌ missed → duplicate ✅ matched
KEY = value ❌ missed → duplicate ✅ matched

Tests

Added mofa/tests/test_vibe_env_whitespace.py with five parameterised cases:

Test Scenario
test_update_key_with_no_spaces Baseline – standard KEY=value
test_update_key_with_spaces_around_equals The exact #1369 regression
test_update_key_with_leading_whitespace Leading-indent variant
test_new_key_appended_when_absent Key absent → appended once
test_multiple_keys_all_updated All four vibe keys with mixed styles
pytest mofa/tests/test_vibe_env_whitespace.py -v

All 5 tests pass.

Checklist

Fixes mofa-org#1369

_save_vibe_config checked for existing keys using:
  stripped.startswith("KEY=")

This pattern silently missed lines using "KEY = value" style formatting
(spaces around the = operator), causing the old value to be kept AND a
new duplicate entry to be appended.

Fix: Replace startswith() with a compiled regex:
  re.compile(r"^\s*" + re.escape(key) + r"\s*=")

The regex matches the key with optional leading whitespace and optional
whitespace on both sides of =, covering:
  KEY=value
  KEY = value
  KEY= value
    KEY  =  value

Tests added in mofa/tests/test_vibe_env_whitespace.py:
  - test_update_key_with_no_spaces (baseline)
  - test_update_key_with_spaces_around_equals (main regression case)
  - test_update_key_with_leading_whitespace
  - test_new_key_appended_when_absent
  - test_multiple_keys_all_updated (all four keys with mixed styles)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] fix: incorrect handling of whitespace in .env keys causes duplicate entries in vibe command.

1 participant