Repository: axodotdev/cargo-dist (rust)

Issue:
Homebrew publishing: autogenerated formula fails autogenerated tests
The formulas autogenerated by cargo-dist when publishing to a Homebrew tap have formatting that fails the default Github Actions tests that are created when you run `brew tap-new`. Specifically, they fail the style checks run with `brew style`. 

Apologies if this is out of scope, but given that users will run into this CI failure if they follow the [cargo-dist instructions](https://opensource.axo.dev/cargo-dist/book/installers/homebrew.html) for setting up homebrew releases, I figured I'd open an issue. Similar to [this issue](https://github.com/axodotdev/cargo-dist/issues/810).

Here are the logged failures I personally ran into (first one is fixable user-side, the others are from cargo-dist's template):

```
==> Running TapSyntax#run!
==> brew style cxreiff/tap
==> FAILED
Full style cxreiff/tap output
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:16:5: C: [Correctable] Style/SoleNestedConditional: Consider merging nested conditions into outer if conditions.
      if Hardware::CPU.intel?
      ^^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:23:20: C: [Correctable] Layout/SpaceInsideHashLiteralBraces: Space inside { missing.
    BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
                     ^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:23:20: C: [Correctable] Style/MutableConstant: Freeze mutable objects assigned to constants.
    BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:23:119: C: [Correctable] Layout/LineLength: Line is too long. [135/118]
    BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
                                                                                                                        ^^^^^^^^^^^^^^^^^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:23:135: C: [Correctable] Layout/SpaceInsideHashLiteralBraces: Space inside } missing.
    BINARY_ALIASES = {"aarch64-apple-darwin": {}, "x86_64-apple-darwin": {}, "x86_64-pc-windows-gnu": {}, "x86_64-unknown-linux-gnu": {}}
                                                                                                                                        ^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:41:5: C: [Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
      if OS.mac? && Hardware::CPU.arm?
      ^^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:44:5: C: [Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
      if OS.mac? && Hardware::CPU.intel?
      ^^
  Taps/cxreiff/homebrew-tap/Formula/ttysvr.rb:47:5: C: [Correctable] Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||.
      if OS.linux? && Hardware::CPU.intel?
      ^^
  
  1 file inspected, 9 offenses detected, 9 offenses autocorrectable
```

Pull request description:
format formulas with brew style --fix
Fixes #1331.

## Problem
Creating a personal tap with `brew tap-new` creates a default [Github Actions workflow](https://github.com/cxreiff/homebrew-tap/blob/main/.github/workflows/tests.yml) that runs a homebrew-specific style check (`brew style <TAPNAME>`) on the repo. Cargo-dist's generated homebrew formulas fail this style check. This does not block the core functionality of cargo-dist's homebrew publishing functionality (formula still functions), ~~but does mean that users following [cargo-dist's instructions](https://opensource.axo.dev/cargo-dist/book/installers/homebrew.html) exactly will run into workflow failures~~ (EDIT: This was not true at all- docs mention a plain repo, not one created with `brew tap-new`).

## Previously
It looks like this has come up before (#810) and it was solved by individually fixing each formatting problem in the templates ([PR 818](https://github.com/axodotdev/cargo-dist/pull/818)).

## Solution
Rather than separately chase formatting issues as they come up, the homebrew publish workflow could run `brew style --fix` on each formula before committing it.

### Pros
- Changes to the homebrew style rules can be adjusted to automatically in the future without changes to cargo-dist's templates.
- Changes can be made to cargo-dist's templates more freely with less fear of breaking style rules.
- Keeps templates simple: some Homebrew style rules interact with cargo-dist's templating strategy in ways that take complicated template conditions to solve (there are rules that apply to single statements inside conditionals, while in the formula templates the number of statements in a conditional might vary based on supported platforms.

### Cons
- Homebrew publish workflow will take longer as it will need to install Homebrew every run.
- Depends on reliability of `brew style --fix` (could break something without cargo-dist code changes if brew pushes a bad change).
- Not all style changes are auto-fixable.

## Testing
I've tested this by manually making these changes in the generated workflow itself and tagging a release. This means that the generation itself hasn't explicitly been tested.

Example run with new action steps: https://github.com/cxreiff/ttysvr/actions/runs/10436301681

Patch:
diff --git a/cargo-dist/templates/ci/github/partials/publish_homebrew.yml.j2 b/cargo-dist/templates/ci/github/partials/publish_homebrew.yml.j2
index c0285dd5..d011e567 100644
--- a/cargo-dist/templates/ci/github/partials/publish_homebrew.yml.j2
+++ b/cargo-dist/templates/ci/github/partials/publish_homebrew.yml.j2
@@ -36,6 +36,10 @@
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done


Test patch:
diff --git a/cargo-dist/tests/snapshots/akaikatana_basic.snap b/cargo-dist/tests/snapshots/akaikatana_basic.snap
index 036b61a9..8f3d5e22 100644
--- a/cargo-dist/tests/snapshots/akaikatana_basic.snap
+++ b/cargo-dist/tests/snapshots/akaikatana_basic.snap
@@ -2270,6 +2270,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/akaikatana_one_alias_among_many_binaries.snap b/cargo-dist/tests/snapshots/akaikatana_one_alias_among_many_binaries.snap
index d457fefd..4d432f07 100644
--- a/cargo-dist/tests/snapshots/akaikatana_one_alias_among_many_binaries.snap
+++ b/cargo-dist/tests/snapshots/akaikatana_one_alias_among_many_binaries.snap
@@ -2284,6 +2284,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/akaikatana_two_bin_aliases.snap b/cargo-dist/tests/snapshots/akaikatana_two_bin_aliases.snap
index e721e579..c6b83efe 100644
--- a/cargo-dist/tests/snapshots/akaikatana_two_bin_aliases.snap
+++ b/cargo-dist/tests/snapshots/akaikatana_two_bin_aliases.snap
@@ -2298,6 +2298,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/akaikatana_updaters.snap b/cargo-dist/tests/snapshots/akaikatana_updaters.snap
index de67e190..7a738dc0 100644
--- a/cargo-dist/tests/snapshots/akaikatana_updaters.snap
+++ b/cargo-dist/tests/snapshots/akaikatana_updaters.snap
@@ -2310,6 +2310,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_alias.snap b/cargo-dist/tests/snapshots/axolotlsay_alias.snap
index 4ed31969..11b8cb09 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_alias.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_alias.snap
@@ -3829,6 +3829,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_alias_ignores_missing_bins.snap b/cargo-dist/tests/snapshots/axolotlsay_alias_ignores_missing_bins.snap
index 15992ea6..5dac8e2b 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_alias_ignores_missing_bins.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_alias_ignores_missing_bins.snap
@@ -3827,6 +3827,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_basic.snap b/cargo-dist/tests/snapshots/axolotlsay_basic.snap
index 3e625f3d..9675f995 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_basic.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_basic.snap
@@ -3813,6 +3813,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_basic_lies.snap b/cargo-dist/tests/snapshots/axolotlsay_basic_lies.snap
index eaf54ae8..5d278aef 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_basic_lies.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_basic_lies.snap
@@ -3903,6 +3903,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_build_setup_steps.snap b/cargo-dist/tests/snapshots/axolotlsay_build_setup_steps.snap
index e8239f35..af7e62f5 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_build_setup_steps.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_build_setup_steps.snap
@@ -3830,6 +3830,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_custom_formula.snap b/cargo-dist/tests/snapshots/axolotlsay_custom_formula.snap
index 0fbbb2a4..023c31f7 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_custom_formula.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_custom_formula.snap
@@ -643,6 +643,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_disable_source_tarball.snap b/cargo-dist/tests/snapshots/axolotlsay_disable_source_tarball.snap
index d702dcb7..a2adc026 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_disable_source_tarball.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_disable_source_tarball.snap
@@ -3802,6 +3802,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap b/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap
index 4e67e0f8..6724293e 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_edit_existing.snap
@@ -3785,6 +3785,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_generic_workspace_basic.snap b/cargo-dist/tests/snapshots/axolotlsay_generic_workspace_basic.snap
index 78da52e2..ec5ec9e4 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_generic_workspace_basic.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_generic_workspace_basic.snap
@@ -4107,6 +4107,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_homebrew_packages.snap b/cargo-dist/tests/snapshots/axolotlsay_homebrew_packages.snap
index 9832f7a8..2ab6269e 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_homebrew_packages.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_homebrew_packages.snap
@@ -3815,6 +3815,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_several_aliases.snap b/cargo-dist/tests/snapshots/axolotlsay_several_aliases.snap
index f00378f4..ddbfa4fe 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_several_aliases.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_several_aliases.snap
@@ -3831,6 +3831,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_updaters.snap b/cargo-dist/tests/snapshots/axolotlsay_updaters.snap
index 6a0380ad..4fcd91a6 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_updaters.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_updaters.snap
@@ -3853,6 +3853,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_user_global_build_job.snap b/cargo-dist/tests/snapshots/axolotlsay_user_global_build_job.snap
index 0470cbfe..58c4427a 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_user_global_build_job.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_user_global_build_job.snap
@@ -3807,6 +3807,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_user_host_job.snap b/cargo-dist/tests/snapshots/axolotlsay_user_host_job.snap
index d49b9542..11071f75 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_user_host_job.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_user_host_job.snap
@@ -3809,6 +3809,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_user_local_build_job.snap b/cargo-dist/tests/snapshots/axolotlsay_user_local_build_job.snap
index eb9377bb..1bcb4f03 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_user_local_build_job.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_user_local_build_job.snap
@@ -3809,6 +3809,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_user_plan_job.snap b/cargo-dist/tests/snapshots/axolotlsay_user_plan_job.snap
index 8562595b..8d3011f9 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_user_plan_job.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_user_plan_job.snap
@@ -3801,6 +3801,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done
diff --git a/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap b/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap
index 6959e818..35692518 100644
--- a/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap
+++ b/cargo-dist/tests/snapshots/axolotlsay_user_publish_job.snap
@@ -3787,6 +3787,10 @@ jobs:
             name=$(echo "$filename" | sed "s/\.rb$//")
             version=$(echo "$release" | jq .app_version --raw-output)
 
+            export PATH="/home/linuxbrew/.linuxbrew/bin:$PATH"
+            brew update
+            brew style --fix "Formula/${filename}" || true
+
             git add "Formula/${filename}"
             git commit -m "${name} ${version}"
           done