Skip to content

fix(operations.files): match files.block markers as plain text - #2128

Open
knjoroge wants to merge 2 commits into
pyinfra-dev:3.xfrom
knjoroge:fix/files-block-literal-marker
Open

knjoroge wants to merge 2 commits into
pyinfra-dev:3.xfrom
knjoroge:fix/files-block-literal-marker

Conversation

@knjoroge

@knjoroge knjoroge commented Dec 1, 2026

Copy link
Copy Markdown

Fixes #2111. Picks up #2112 by @sniwas (approved, then closed by its author), with the same approach.

Why

files.block and the files.Block fact put the marker inside an awk /regex/. So a marker with regex characters like (, . or * was never found, and the block was added again on every run. A marker with / or ' broke the awk command completely.

Changes

  • The markers are passed to awk as arguments and matched with index(), so they are always plain text. This covers all 3 places: the fact, updating a block, and removing a block.
  • Docstring: marker is matched as plain text.
  • Tests: updated the existing fixtures for the new command, and added fixtures for markers with (block), / and '.

Testing

  • Ran the issue's example with @local twice. Before: each run adds another copy of the block (3 runs, 3 blocks). After: 1 block, and later runs are "No changes". Update and remove also work.
  • Ran the 3 awk programs on macOS awk, busybox, mawk and gawk with the same result. Normal markers give the same output as the old regex version.

Note: files already hit by this bug have the block more than once. The fix finds all copies, so present=False removes them all and you can then add the block again.

  • Pull request is based on the default branch (3.x at this time)
  • Pull request includes tests for any new/updated operations/facts
  • Pull request includes documentation for any new/updated operations/facts
  • Tests pass (see scripts/dev-test.sh)
  • Type checking & code style passes (see scripts/dev-lint.sh)
  • Pull request title follows the conventional commits format

AI disclosure: I used Claude Code to write the code and tests, and I reviewed and understand all of it.

The Block fact and files.block put the marker inside an awk regex, so a
marker with regex characters like `(`, `.` or `*` was never found and the
block was appended again on every run. A marker with `/` or `'` broke the
awk command.

Pass the markers to awk as arguments and match them with index(), so they
are always plain text.

Fixes pyinfra-dev#2111. Based on pyinfra-dev#2112 by sniwas, which Fizzadar approved.

Co-authored-by: sniwas <122344020+sniwas@users.noreply.github.com>
@wowi42 wowi42 added bug Label for all kind of bugs. operations Issues with operations. facts Issues with facts. labels Dec 1, 2026

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

bug Label for all kind of bugs. facts Issues with facts. operations Issues with operations.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

files.block treats marker as an unescaped awk regex — markers containing regex metacharacters are never detected, making the operation non-idempotent

2 participants