Skip to content

Normalize rendered template output for pre-commit#548

Merged
bschwedler merged 1 commit into
mainfrom
normalize-rendered-output
May 28, 2026
Merged

Normalize rendered template output for pre-commit#548
bschwedler merged 1 commit into
mainfrom
normalize-rendered-output

Conversation

@bschwedler
Copy link
Copy Markdown
Contributor

Bakery's Jinja-rendered files (Containerfiles, goss.yaml, scripts, and bakery.yaml itself) could ship with trailing whitespace or a missing trailing newline, failing trailing-whitespace and end-of-file-fixer pre-commit hooks in consuming repos. Two release PRs failed CI for exactly this reason:

The root cause was two compounding bugs in the renderer. First, ImageVersion.render_files and ImageMatrix.render_files tried to enable trim_blocks for Containerfile templates by passing it to tpl.render(trim_blocks=True) — but trim_blocks is a Jinja2 Environment option, not a Template.render() argument, so it was a silent no-op that just landed in the template context. Second, no rendered-template write path normalized whitespace except BakeryConfig.write(), which had a one-off re.sub added after the first failure. Everything else (Containerfiles via version.py/matrix.py, goss.yaml, scripts, scaffolded bakery.yaml) wrote raw Jinja output, so {% for %} ... {% endfor %} blocks left lines containing only their leading indent — exactly what PR #87 caught in connect/2026.04/test/goss.yaml.

normalize_rendered_output() in posit_bakery/config/templating/render.py strips trailing spaces and tabs from each line and ensures a single trailing newline, matching what the two pre-commit hooks produce. It's now applied at every rendered-file write site, and BakeryConfig.write()'s inline re.sub is replaced by it for consistency. The dead render_kwargs["trim_blocks"] = True branch is removed since it never took effect; the new normalize step covers the same intent more robustly.

Verified by re-rendering all images in images-connect, images-package-manager, and images-workbench against the patched bakery — full pre-commit suite passes on all three.

Bakery's Jinja-rendered files (Containerfiles, goss.yaml, scripts, and
bakery.yaml itself) could ship with trailing whitespace or a missing
trailing newline, failing the trailing-whitespace and end-of-file-fixer
pre-commit hooks in consuming repos. Release PRs in images-connect
(#77, #87) failed CI for exactly this reason.

Add normalize_rendered_output() that strips trailing spaces and tabs
from each line and ensures a single trailing newline, matching the
output of those two pre-commit hooks. Apply it at every site that
writes a rendered template:

- ImageVersion.render_files (Containerfile and other templates)
- ImageMatrix.render_files (Containerfile and other templates)
- BakeryConfig.new (initial bakery.yaml)
- BakeryConfig.write (updates to bakery.yaml, replacing the inline
  re.sub strip added in 48dea80)
- BakeryConfigDocument when scaffolding a new Containerfile template

Also drop the dead render_kwargs["trim_blocks"] = True branch from
ImageVersion and ImageMatrix. trim_blocks is a Jinja2 Environment
option, not a Template.render() argument, so passing it through
.render() was a silent no-op that just added it to the template
context. Removing it doesn't change rendered output because it never
took effect; the new normalize step covers the same intent more
robustly.
@bschwedler bschwedler requested a review from ianpittwood as a code owner May 20, 2026 16:35
@github-actions
Copy link
Copy Markdown

Test Results

1 568 tests  +10   1 568 ✅ +10   8m 7s ⏱️ +7s
    1 suites ± 0       0 💤 ± 0 
    1 files   ± 0       0 ❌ ± 0 

Results for commit 8f6ffe7. ± Comparison against base commit 0c47dd1.

Copy link
Copy Markdown
Contributor

@ianpittwood ianpittwood left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for fixing this!

@bschwedler bschwedler added this pull request to the merge queue May 28, 2026
@bschwedler bschwedler added this to the 2026.a: Post-GA Follow-up milestone May 28, 2026
Merged via the queue into main with commit f3b6b78 May 28, 2026
24 checks passed
@bschwedler bschwedler deleted the normalize-rendered-output branch May 28, 2026 14:31
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.

2 participants