Skip to content

Fail closed in check --munki-installcheck when the check errors - #22

Open
alexmikheen wants to merge 1 commit into
macadmins:mainfrom
alexmikheen:fix/installcheck-fail-closed
Open

alexmikheen wants to merge 1 commit into
macadmins:mainfrom
alexmikheen:fix/installcheck-fail-closed

Conversation

@alexmikheen

@alexmikheen alexmikheen commented Nov 17, 2026 •

Copy link
Copy Markdown

With --munki-installcheck, exit 1 means "nothing to do". Any error from check used to exit 1, so a broken brew, a formula from an untrusted tap (Homebrew's tap trust), an unparseable installed version or a typo in --min-version looked compliant to Munki.

Changes

  • brew.Check returns the failing code for the selected mode on every error path (0 under --munki-installcheck, 1 otherwise). Two paths returned the passing code.
  • check returns an exitCodeError that carries that code; main() uses it via exitCodeFor. Other commands still exit 1 on error.
  • Argument errors take the same failing code, so a bad pkginfo makes the Munki item fail visibly instead of reading as compliant: an invalid formula name, --skip-not-installed without --min-version, and a --min-version that go-version cannot parse (a Homebrew revision such as 2.50.1_1 is rejected). They wrap a new exported brew.ErrUsage and still print usage help.
  • With --skip-not-installed, brew's No available formula with the name "<item>" counts as not installed only when <prefix>/Cellar/<name> does not exist. A typo, a formula removed from homebrew-core, or one from a tap that is not tapped no longer turns into "install needed" on every Mac, while a keg that is still in the Cellar (formula removed from its tap after install, or tap untapped with --force) fails closed. The match is case-insensitive (brew prints the name in lower case) and only for the requested name, so a missing dependency, or a formula file that exists but cannot be loaded, fails closed.
  • Runtime check errors no longer print the usage text, and the error is printed once instead of twice (cobra's copy is silenced; main() prints it).
  • cudo.MockOSFunc gets an optional StatFunc for per-path answers in tests.
  • README: exit codes on error, and the cases that still exit 1.

Behaviour changes to be aware of

  • A Mac where the check cannot decide now runs the Munki item, and the paired carafe upgrade fails on every run until the cause is fixed. Examples: brew broken after a macOS update, a stale API cache with no network, a HEAD-… install, or one of the ~20 core formulae whose version go-version cannot parse (such as x264 r3222). Before, these were silently reported as compliant.
  • A configuration error (invalid --min-version, invalid name, missing --min-version with --skip-not-installed) now exits 0 under --munki-installcheck, also when the formula is not installed. Plain mode still exits 1.
  • Still exit 1 with --munki-installcheck: no Homebrew at all, no console user (exec.NewConfig fails before the command runs), and a command line that cobra cannot parse (unknown flag, wrong number of arguments).
  • Plain mode: an unknown formula with --skip-not-installed and no keg in the Cellar now exits 0 with "installed": false JSON (on main it was an error, exit 1).
  • Library API (brew package): Check returns the failing code on all error paths, including argument errors, and ErrUsage is new.

Behaviour (--munki-installcheck)

Case main this PR
brew error (broken brew, untrusted tap) 1 (no action) 0 (install needed)
unparseable installed version 1 0
unknown formula + --skip-not-installed, no keg 1 1
unknown formula + --skip-not-installed, keg still in Cellar 1 0
unknown formula without --skip-not-installed 1 0
invalid --min-version / invalid name / skip without min 1 0
no Homebrew, no console user, unparseable command line 1 1

Tests

  • brew/check_test.go: TestCheckErrorsFailClosed, TestCheckUnknownFormulaAndUsageErrors (mixed case, through the cache, untapped tap, keg still in the Cellar, broken formula file, dependency named in the error, usage errors in both modes).
  • main_test.go: TestCheckCmd_CheckErrorExitCode, TestCheckCmd_SkipNotInstalledNeedsMinVersion, TestCheckCmd_InvalidFormulaNameExitCode, TestCheckCmd_InvalidMinVersionIsUsageError, TestCheckCmd_RuntimeErrorHasNoUsage (no usage text, no duplicate error), TestExitCodeFor.
  • go test ./..., go test -race ./..., golangci-lint run (v2.6.2, Go 1.25.1) and the repo's coverage check pass, alone and merged with the other two PRs. Coverage (go-test-coverage with .testcoverage.yml): 79.5% on main, 80.7% with this PR.

Live run as root on macOS (Apple Silicon, Homebrew 7.0), main vs this PR, --munki-installcheck:

Case main this PR
unknown formula + --skip-not-installed (direct, via cache, mixed-case name) 1 1
unknown formula without --skip-not-installed 1 0
--min-version=v2.x / --min-version=1.0_1 / skip without min / name ../x 1 0 (usage shown)
installed, above / below floor 1 / 0 1 / 0
tap-qualified name, cache path, info, no-op upgrade unchanged unchanged
usage text and duplicate error on a runtime error printed not printed

With --munki-installcheck, exit 1 means "nothing to do". Any error
from check used to exit 1, so a broken brew, a formula from an
untrusted tap (Homebrew tap trust), an unparseable installed version
or a typo in --min-version looked compliant to Munki.

- brew.Check returns the failing code for the selected mode on every
  error path (exit 0 under --munki-installcheck, 1 otherwise). Two
  paths returned the passing code.
- The check command passes that code to main() through
  exitCodeError / exitCodeFor. Other commands still exit 1.
- Argument errors (an invalid formula name, --skip-not-installed
  without --min-version, a --min-version that go-version cannot
  parse) take the same failing code, so a bad pkginfo makes the
  Munki item fail visibly. They wrap the new brew.ErrUsage and still
  print usage help. A revision such as "2.50.1_1" is rejected.
- With --skip-not-installed, "No available formula with the name
  "<item>"" counts as not installed only when the Cellar has no keg
  for the item. This covers a typo, a formula removed from its tap
  and a tap that is not tapped, so they do not become "install
  needed" on every Mac; a keg that is still installed fails closed.
  The match is case-insensitive (brew prints the name in lower
  case) and only for the requested name, so a missing dependency or
  a formula file that cannot be loaded still fails closed.
- Runtime check errors no longer print the usage text, and the error
  is printed once instead of twice.
- cudo.MockOSFunc gets an optional StatFunc for per-path answers.
- README: exit codes on error and the cases that still exit 1 (no
  Homebrew, no console user, an unparseable command line).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.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.

1 participant