Skip to content

Fix YAML replacement affecting wrong fields with same value#22

Merged
ChrisJBurns merged 2 commits intomainfrom
fix/key-aware-yaml-replacement
Feb 4, 2026
Merged

Fix YAML replacement affecting wrong fields with same value#22
ChrisJBurns merged 2 commits intomainfrom
fix/key-aware-yaml-replacement

Conversation

@ChrisJBurns
Copy link
Collaborator

Summary

  • Fix bug where updating one YAML field would accidentally modify another field with the same value but different formatting
  • Make replacement patterns key-aware to target only the specific field being updated
  • Add regression tests covering the version/appVersion scenario

Problem

When a Chart.yaml has both version: 0.9.0 (unquoted) and appVersion: "0.9.0" (quoted), updating the version field would accidentally modify appVersion first because the double-quoted regex pattern matched before the unquoted pattern.

This caused the toolhive release workflow to fail with:

Error: version mismatch in deploy/charts/operator-crds/Chart.yaml at path appVersion: 
expected to find "0.9.0" but found "0.9.1"

Root Cause

The surgicalReplace function searched the entire file for pattern matches, not just the value at the target path. When patterns like "(0\.9\.0)" were tried, they matched the quoted appVersion even when we wanted to update the unquoted version.

Solution

  1. Extract the key name from the YAML path (e.g., metadata.versionversion)
  2. Include the key in replacement patterns so they only match the specific field:
    • Before: "(0\.9\.0)" (matches any quoted 0.9.0)
    • After: (version:\s*)"(0\.9\.0)" (only matches version's quoted value)
  3. Fix Go regex $1 syntax by using ${1} to avoid ambiguity when versions start with digits

Test plan

  • Added TestUpdateYAMLFile_SameValueDifferentKeys with 4 test cases covering:
    • Update unquoted version without affecting quoted appVersion
    • Update quoted appVersion without affecting unquoted version
    • Sequential updates to different keys with same value
    • Nested paths with different keys
  • Added TestExtractKeyFromPath for the new helper function
  • All existing tests pass

🤖 Generated with Claude Code

The surgicalReplace function was searching the entire file for pattern
matches, causing issues when multiple fields have the same version value
but different formatting (e.g., `version: 0.9.0` and `appVersion: "0.9.0"`).

The double-quoted pattern would match the quoted appVersion first, even
when updating the unquoted version field, corrupting the wrong field.

Changes:
- Add extractKeyFromPath() to get the YAML key name from a path
- Make replacement patterns key-aware to only match the specific field
- Fix Go regex capture group syntax (use ${1} instead of $1) to avoid
  ambiguity when new version starts with a digit
- Add regression tests for the fix

Fixes an issue where updating version in Chart.yaml would accidentally
modify appVersion, causing version mismatch errors on subsequent runs.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Rename unused 'key' parameter to '_' in replacement functions.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@ChrisJBurns ChrisJBurns merged commit cd90dbc into main Feb 4, 2026
3 checks passed
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.

1 participant