Skip to content

fix(builtins): keep empty files empty when fixing with tofu, terraform and other stdin formatters - #1874

Merged
jdx merged 1 commit into
mainfrom
claude/github-issue-1873-7dfdff
Nov 18, 2026
Merged

jdx merged 1 commit into
mainfrom
claude/github-issue-1873-7dfdff

Conversation

@jdx

@jdx jdx commented Nov 18, 2026 •

Copy link
Copy Markdown
Owner

Fixes #1873.

Problem

With `Builtins.tofu`, `hk fix --all` wrote a single newline into an intentionally empty `.tf` file, even though `hk check` and `tofu fmt empty.tf` both accept it. The builtin's `check_diff` pipes each file to `tofu fmt -` through `hk util format-diff`. Given empty input, `tofu fmt -` prints `\n`, and hk applied that as a patch.

Change

`hk util format-diff` now treats an empty file as unchanged when the formatter only prints whitespace for it. This covers every builtin that formats through `format-diff`, not just tofu. Terraform and packer use the same `fmt -` pattern, though I only reproduced the behavior with a stand-in formatter, not with those tools.

Before: `: > empty.tf; hk fix --all` leaves `empty.tf` containing `\n`.
After: `empty.tf` stays empty.

Validation

Added a bats test in `test/util_format_diff.bats` using a formatter that prints a newline for empty input; `test/util_format_diff.bats` passes (20 tests). I did not run `tofu` itself.

🤖 Generated with Claude Code

AI-assisted — Tool: Claude Code; model: anthropic/claude-sonnet-5-5; version: unavailable.


Note

Low Risk
Narrow guard in format-diff comparison logic; only affects empty files where formatter output is whitespace-only.

Overview
hk util format-diff no longer emits a patch when the file is empty and the formatter’s stdout is only whitespace (e.g. a single newline from tofu fmt - on empty stdin). Those cases are reported as unchanged, so hk fix won’t rewrite intentionally empty files that the same formatter would leave alone when run on a path.

A bats test uses a stub formatter that prints a newline for empty input and asserts format-diff exits 0 with no output.

Reviewed by Cursor Bugbot for commit cc8ba62. Bugbot is set up for automated code reviews on this repo. Configure here.

Summary by CodeRabbit

  • Bug Fixes
    • Formatting an empty file no longer reports a change when the formatter outputs only whitespace, including a newline. Other differing formatter output continues to be shown as a diff.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
AI-Assisted: Tool: Claude Code; model: unavailable; version: 2.1.326
Entire-Checkpoint: 01M7QRETX372BY18EDDTJYA0NH
@coderabbitai

coderabbitai Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

When the original file is empty and formatter output is whitespace-only, format_file now returns Unchanged instead of generating a unified diff. A test checks the case where the formatter emits a newline.

Changes: Empty-file format diff handling

Layer / File(s) Summary
Treat whitespace-only output as unchanged
src/cli/util/format_diff.rs, test/util_format_diff.bats
format_file returns Unchanged for an empty input when formatter output trims to an empty string. The test checks that newline output produces no output from hk util format-diff.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Merge Risk: Minimal · up to cc8ba

Empty files stay unchanged when a formatter prints only whitespace for empty input, so hk fix no longer writes a newline into them. The change is narrow and tested, and no merge-blocking risk remains.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main behavior change: empty files stay empty when fixed by stdin-based formatters such as tofu and terraform. The raw summary and objectives confirm the change in format-diff treat…
Linked Issues check Passed Issue #1873 requires hk fix --all to leave an empty .tf file unchanged when Builtins.tofu runs. The builtin pipes the file to tofu fmt -, which prints \\n for empty input. The reviewed head a…
Out of Scope Changes check Passed The code change is a six-line guard in format_file and a supporting bats test. Both sit in the code path that issue #1873 describes. The guard applies to every builtin that formats through `format-d…
Full details: Title check

Explanation

The title names the main behavior change: empty files stay empty when fixed by stdin-based formatters such as tofu and terraform. The raw summary and objectives confirm the change in format-diff treats whitespace-only output for an empty input as unchanged, which addresses the reported newline written to empty .tf files. The title names the tools as examples, so it is slightly broader than the code change, but it is related to the main change.

Full details: Linked Issues check

Explanation

Issue #1873 requires hk fix --all to leave an empty .tf file unchanged when Builtins.tofu runs. The builtin pipes the file to tofu fmt -, which prints \n for empty input. The reviewed head adds an early return in format_file (src/cli/util/format_diff.rs): when original.is_empty() and formatted.trim().is_empty(), the function returns Outcome::Unchanged and no patch is produced. This covers the reported case. The PR also adds a bats test in test/util_format_diff.bats that uses a formatter printing a newline for empty input and expects no output. I read the test only through the PR summary, not the file itself. The issue has no non-coding requirements beyond this behavior. The PR reports that tofu itself was not run, so the fix is verified with a stand-in formatter only.

Full details: Out of Scope Changes check

Explanation

The code change is a six-line guard in format_file and a supporting bats test. Both sit in the code path that issue #1873 describes. The guard applies to every builtin that formats through format-diff, not only tofu. That breadth follows from the root cause, since the shared helper produces the patch. The PR description states this scope explicitly. No unrelated files or behavior changes appear in the reviewed diff for src/cli/util/format_diff.rs.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 6bd55d6 and cc8ba62.

Files selected for processing: 2

  • src/cli/util/format_diff.rs
  • test/util_format_diff.bats

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

Finishing Touches
Fix failing CI checks

  • Commit to this branch
  • Create a new PR

@greptile-apps

greptile-apps Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge.

Summary

This change keeps empty files empty when a formatter prints only whitespace.

  • Empty files stay empty when formatters return only whitespace.

No actionable issues found. Tests were not run during this review.

Reviews (1) · Last reviewed commit: "fix(run): keep empty files empty when a ..." · Reviewed by Greptile

@github-actions

Copy link
Copy Markdown
Contributor

Instruction counts

benchmark trend instructions Δ wall (min) Δ
builtins ▁▁▁▁▁▁▄▄▄▅▅▄▃▄▄▄▄█ 1,981,388 → 1,991,983 +0.53% 3.05 → 3.16ms +3.59%
check — — — 19.23 → 19.23ms +0.01%
config-cold ▁▁▁▁▁▁████████████ 72,268,424 → 72,173,987 -0.13% 22.14 → 21.64ms -2.27%
stash ▂▁▂▁▂▁▂▁▁▂▁▁▂▂▂▂▂█ 60,922,543 → 61,649,935 +1.19% ⚠️ 284.28 → 286.85ms +0.90%
usage ▅▅▅▅▅▅▇▇▇███▇▇▇▇▇▁ 4,434,327 → 4,409,986 -0.55% 5.01 → 5.12ms +2.32%
validate — — — 14.40 → 14.14ms -1.76%

1 benchmark(s) above the 1% gate: stash +1.19%

Only instruction counts gate. Wall clock is shown for context — on identical hardware it moves 4-20% run to run.

Measured by tak — instruction-counted CLI benchmarks, stored in this repository's git notes.

cc8ba6259794 vs 512afce8ead6 · measured on the runner, not pushed to the history.

@jdx
jdx merged commit 704b141 into main Nov 18, 2026
31 of 33 checks passed
@jdx
jdx deleted the claude/github-issue-1873-7dfdff branch November 18, 2026 19:22
@jdx jdx mentioned this pull request Nov 18, 2026
jdx added a commit that referenced this pull request Nov 23, 2026
### 🚀 Features

- **(builtins)** add hk_validate builtin to fail on config warnings by
[@jdx](https://github.com/jdx) in
[#1862](#1862)
- **(builtins)** report ox_lint findings as diagnostics by
[@jdx](https://github.com/jdx) in
[#1871](#1871)
- **(builtins)** add bun_fmt formatter by [@jdx](https://github.com/jdx)
in [#1870](#1870)
- **(builtins)** add scan = "staged" option to betterleaks by
[@jdx](https://github.com/jdx) in
[#1878](#1878)
- **(builtins)** scan every pushed commit for secrets with scan =
"commits" by [@jdx](https://github.com/jdx) in
[#1880](#1880)
- **(step)** add recheck_after_fix to rerun checks a later fix changed
by [@jdx](https://github.com/jdx) in
[#1891](#1891)

### 🐛 Bug Fixes

- **(builtins)** keep empty files empty when fixing with tofu, terraform
and other stdin formatters by [@jdx](https://github.com/jdx) in
[#1874](#1874)
- **(install)** let steps call mise from the global --mise launcher by
[@jdx](https://github.com/jdx) in
[#1875](#1875)
- **(install)** start the per-repo --mise launcher without mise on PATH
by [@jdx](https://github.com/jdx) in
[#1883](#1883)
- **(stash)** stop a nested hk from waiting on the stash lock during
stash recovery by [@jdx](https://github.com/jdx) in
[#1852](#1852)
- **(stash)** stop a stash restore from starting a nested hk through
post-checkout by [@jdx](https://github.com/jdx) in
[#1869](#1869)
- **(stash)** keep intent-to-add files out of the commit with stash =
"none" by [@jdx](https://github.com/jdx) in
[#1884](#1884)
- **(step)** stop adding GIT_WORK_TREE to steps without a dir by
[@jdx](https://github.com/jdx) in
[#1889](#1889)

### 🛡️ Security

- **(deps)** update mise tools by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1854](#1854)
- **(deps)** update dependency jouet to v0.0.4 by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1859](#1859)

### 🔍 Other Changes

- **(deps)** update url to 2.5.9 by [@jdx](https://github.com/jdx) in
[#1893](#1893)
- **(entire)** remove trail runners by [@jdx](https://github.com/jdx) in
[8cfa90f](8cfa90f)
- remove the pull_request_target workflows by
[@jdx](https://github.com/jdx) in
[#1876](#1876)

### 📦️ Dependency Updates

- update github actions by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1853](#1853)
- lock file maintenance by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1856](#1856)
- update dependency jsdom to v31 by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1855](#1855)
- lock file maintenance by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1860](#1860)
- ignore unmaintained chrono advisories in cargo-deny by
[@jdx](https://github.com/jdx) in
[#1868](#1868)
- update rust crate pklr to v6.0.4 by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1858](#1858)
- update mise tools by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1895](#1895)
- update github actions by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1894](#1894)
- lock file maintenance by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1896](#1896)
- lock file maintenance by
[@renovate[bot]](https://github.com/renovate[bot]) in
[#1899](#1899)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Low Risk**
> Release-metadata and documentation URL updates only; runtime behavior
changes are documented in the changelog but not introduced in this diff.
> 
> **Overview**
> Cuts **hk v2.14.0** by bumping the crate and CLI version from `2.13.0`
to `2.14.0` in `Cargo.toml`, `Cargo.lock`, `hk.usage.kdl`, and generated
CLI docs, and aligning every docs/example `package://` import with the
new release URL.
> 
> Adds a **2.14.0** section to `CHANGELOG.md` that records what ships in
this tag: new builtins (`hk_validate`, `bun_fmt`, betterleaks `scan =
"staged"` / `"commits"`, ox_lint diagnostics), **`recheck_after_fix`**,
stash/mise/install/step env fixes, dependency and CI housekeeping
(including removing trail runners and `pull_request_target` workflows).
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
c86fddc. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

Co-authored-by: mise-en-dev <123107610+mise-en-dev@users.noreply.github.com>
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.

Builtins.tofu fix adds a newline to empty .tf files that tofu fmt accepts

1 participant