Skip to content

Preserve quotes and version prefixes in YAML revisions - #68

Open
tianhaogutum wants to merge 1 commit into
tsvikas:mainfrom
tianhaogutum:fix/preserve-quoted-yaml-revs
Open

tianhaogutum wants to merge 1 commit into
tsvikas:mainfrom
tianhaogutum:fix/preserve-quoted-yaml-revs

Conversation

@tianhaogutum

Copy link
Copy Markdown

Summary

The YAML rev regex in process_config_text() captured the surrounding quotes as part of the revision. As a result, a quoted rev that already matched uv.lock was reported as changed (--check exited 1) and rewritten without its quotes. For repos without a version template, it also lost the inferred v prefix ('v1.5.0' → 1.5.0).

This change makes the YAML regex keep optional, matching quote delimiters outside the repo_rev group, as the TOML regex already does. The existing version-template logic and in-place splice are unchanged, so the original quotes and any trailing comment are preserved.

Reproduction

Starting from an empty directory with uv installed:

mkdir repro && cd repro
cat > .pre-commit-config.yaml <<'YAML'
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
  rev: "v0.8.0"
  hooks:
    - id: ruff
- repo: https://github.com/asottile/yesqa
  rev: 'v1.5.0'
  hooks:
    - id: yesqa
YAML
cat > uv.lock <<'TOML'
version = 1
revision = 3
requires-python = ">=3.11"

[[package]]
name = "ruff"
version = "0.8.0"
source = { registry = "https://pypi.org/simple" }

[[package]]
name = "yesqa"
version = "1.5.0"
source = { registry = "https://pypi.org/simple" }
TOML
uvx --from git+https://github.com/tsvikas/sync-with-uv@0d242e3c029a20e9c2288a79b6a49aaaf0361a19 sync-with-uv --diff
uvx --from git+https://github.com/tsvikas/sync-with-uv@0d242e3c029a20e9c2288a79b6a49aaaf0361a19 sync-with-uv --check; echo "exit=$?"

Expected:

Both revisions already match uv.lock, so the file is left unchanged: 0 packages would be changed, 2 packages would be left unchanged. and --check exits 0.

Actual:

2 packages would be changed, 0 packages would be left unchanged.
-  rev: "v0.8.0"
+  rev: v0.8.0
-  rev: 'v1.5.0'
+  rev: 1.5.0

--check exits 1.

Changes

  • src/sync_with_uv/sync_with_uv.py: YAML rev regex only (one expression).
  • tests/test_quoted_revs.py: 25 test cases covering single and double quotes; already-current and stale revisions; inferred, built-in and user version templates; trailing comments; LF, CRLF and no final newline; quoted numeric-looking revisions staying strings (checked with pre-commit validate-config); repeated write, --check and --diff CLI runs; the unaffected TOML path; and unquoted refs that contain a quote character.
  • CHANGELOG.md: entry under Unreleased → Bug Fixes.

Testing

  • New tests against current main (0d242e3) code: 21 failed, 4 passed. With this change: 25 passed.
  • Full suite: 119 passed (94 existing + 25 new), Python 3.14.4, no skips.
  • ruff check, black --check ., mypy, ty check pass; prek run mdformat passes on CHANGELOG.md.

Out of scope: quoted repo: URLs, multi-line or escaped YAML scalars, and general YAML parsing.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/sync_with_uv/sync_with_uv.py" line_range="117" />
<code_context>
-        "yaml": re.compile(r"^\s*rev\s*:\s*(?P<repo_rev>\S*).*$"),
+        # Keep YAML quote delimiters outside the revision span, as for TOML.
+        "yaml": re.compile(
+            r"""^\s*rev\s*:\s*(?P<quote>['"]?)(?P<repo_rev>\S*?)"""
+            r"(?P=quote)(?=\s|$).*$"
+        ),
</code_context>
<issue_to_address>
**Quoted revisions become invalid YAML**

When a quoted YAML `rev` is updated using a custom version template whose result contains the matching quote character, the target revision is spliced between the existing delimiters without escaping, so a quote-bearing custom version template turns an otherwise valid quoted `rev` into invalid YAML and config validation fails.

Escape the replacement as a YAML scalar value for the captured quote style before splicing it into the line.
</issue_to_address>

Sourcery assessment

Needs a human reviewer. 1 finding to address first, and if the new YAML match captures the wrong revision, the sync tool could write an incorrect rev into a user's config file; reverting the code would not undo that edit, but the affected config is bounded and can be corrected or regenerated.

Blocking findings: src/sync_with_uv/sync_with_uv.py:117


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

"yaml": re.compile(r"^\s*rev\s*:\s*(?P<repo_rev>\S*).*$"),
# Keep YAML quote delimiters outside the revision span, as for TOML.
"yaml": re.compile(
r"""^\s*rev\s*:\s*(?P<quote>['"]?)(?P<repo_rev>\S*?)"""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Quoted revisions become invalid YAML

When a quoted YAML rev is updated using a custom version template whose result contains the matching quote character, the target revision is spliced between the existing delimiters without escaping, so a quote-bearing custom version template turns an otherwise valid quoted rev into invalid YAML and config validation fails.

Escape the replacement as a YAML scalar value for the captured quote style before splicing it into the line.

Prompt for AI agents
In `src/sync_with_uv/sync_with_uv.py` at line 117:

**Quoted revisions become invalid YAML**

When a quoted YAML `rev` is updated using a custom version template whose result contains the matching quote character, the target revision is spliced between the existing delimiters without escaping, so a quote-bearing custom version template turns an otherwise valid quoted `rev` into invalid YAML and config validation fails.

Escape the replacement as a YAML scalar value for the captured quote style before splicing it into the line.

This branch has not been deployed

No deployments
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