Skip to content

Fix pkgrepo.managed honouring clean_file when line already present (#68208)#69432

Open
dwoz wants to merge 2 commits into
saltstack:3006.xfrom
dwoz:fix/issue-68208
Open

Fix pkgrepo.managed honouring clean_file when line already present (#68208)#69432
dwoz wants to merge 2 commits into
saltstack:3006.xfrom
dwoz:fix/issue-68208

Conversation

@dwoz

@dwoz dwoz commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Makes pkgrepo.managed actually empty the managed file and reconfigure
the repo when the user passes clean_file: True and the desired repo
line already happens to be present in the file alongside other lines.

What issues does this PR fix or reference?

Fixes #68208

Previous Behavior

If the file the state manages already contained the requested repo line
(plus unrelated stale lines the user wanted removed), pkg.get_repo
returned the matching definition, the kwarg-vs-pre comparison fell
through to the else branch, and the state returned

Package repo '<line>' already configured

with no changes — silently skipping both the file truncation and the
re-write that clean_file: True asks for. The user's
/etc/apt/sources.list.d/backports.list listing both
bullseye-backports and bookworm-backports is the canonical example:
managing the bookworm line with clean_file: True should drop the
bullseye line but didn't.

New Behavior

When clean_file: True is set, the short-circuit "already configured"
comparison is skipped entirely. The state proceeds to truncate the file,
call pkg.mod_repo to re-write it, and reports the change via the
existing post-mod_repo diff loop.

Merge requirements satisfied?

  • Docs (no documented behavior change — the docstring already says
    "empty the file before configuring the defined repository", which
    is now what actually happens)
  • Changelog (changelog/68208.fixed.md)
  • Tests written/updated
    (tests/pytests/unit/states/test_pkgrepo.py::test_managed_clean_file_with_matching_existing_repo_68208)

Commits signed with GPG?

No (matches the base-branch convention)

@dwoz dwoz requested a review from a team as a code owner June 12, 2026 00:39
@dwoz dwoz added this to the Sulphur v3006.26 milestone Jun 12, 2026
@dwoz dwoz added the test:full Run the full test suite label Jun 12, 2026
Daniel A. Wozniak added 2 commits June 14, 2026 23:00
pkgrepo.managed compared the desired repo definition against the result
of pkg.get_repo and, when they matched, short-circuited with "already
configured" — even when the caller passed clean_file: True. That left
any unrelated stale entries in the managed file untouched, with no
reported change. The user's bookworm-backports state that also still
listed bullseye-backports is the canonical example.

Skip the short-circuit comparison when clean_file is requested so the
truncate + reconfigure path always runs in that case. The post-mod_repo
change-diff loop still reports the change correctly.

Fixes saltstack#68208
The first cut for saltstack#68208 unconditionally skipped the pre/sanitized-kwargs
short-circuit when clean_file was set. That broke the idempotency contract
that the test_adding_repo_file_signedby[Without(aptsources.sourceslist)]
functional test exercises: after a successful first run the file already
contains exactly the desired line, and a subsequent test=True run should
report no changes -- but the unconditional skip caused the test=True path
to emit a spurious "file" diff (the file kwarg was no longer popped),
producing changes != {}.

Narrow the condition: only force the clean + reconfigure path when the
managed file currently holds any non-blank, non-comment line other than
the desired one. The exact-match-only case still short-circuits to
"already configured", so repeated runs stay idempotent.

Adds a companion regression test (the all-desired-line file remains
no-op) alongside the original saltstack#68208 regression test (a file with stale
lines triggers truncate + mod_repo).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant