fix(vibe): handle whitespace around = in .env keys to prevent duplicate entries - #1653
Open
SH20RAJ wants to merge 1 commit into
Open
fix(vibe): handle whitespace around = in .env keys to prevent duplicate entries#1653SH20RAJ wants to merge 1 commit into
SH20RAJ wants to merge 1 commit into
Conversation
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #1369.
mofa vibefails to correctly update existing.enventries when thefile uses the
KEY = valuestyle (spaces around=). The old key iskept and a new duplicate entry is appended, leading to
non-deterministic configuration and confusing multi-value
.envfiles.Root Cause
_save_vibe_configmatched existing keys with:startswith("KEY=")requires the=to be directly adjacent to thekey with no whitespace. A file containing
is never matched, so the function falls through to the append path and
creates a duplicate:
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 optionalleading whitespace and optional whitespace on both sides of
=:This handles:
KEY=valueKEY = valueKEY= valueKEY = valueTests
Added
mofa/tests/test_vibe_env_whitespace.pywith five parameterised cases:test_update_key_with_no_spacesKEY=valuetest_update_key_with_spaces_around_equalstest_update_key_with_leading_whitespacetest_new_key_appended_when_absenttest_multiple_keys_all_updatedAll 5 tests pass.
Checklist
KEY=valueformat