Skip to content

Fix | Propagate the tofu/terraform exit code to the CLI - #336

Merged
diego-ojeda-binbash merged 1 commit into
masterfrom
fix/propagate-tf-exit-code
Nov 13, 2026
Merged

diego-ojeda-binbash merged 1 commit into
masterfrom
fix/propagate-tf-exit-code

Conversation

@diego-ojeda-binbash

@diego-ojeda-binbash diego-ojeda-binbash commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

What

leverage tofu|terraform commands now exit with the same code as the underlying binary, as they did before #316.

Closes #335.

Why

leverage/modules/tf.py discarded the exit code returned by TFRunner.run (which does not raise), so the CLI always exited 0:

  • CI pipelines running leverage tofu apply passed when the apply failed.
  • plan -detailed-exitcode could not report changes (2).
  • --layers kept going after a failing layer.

Regression from #316, shipped in v3.1.0. It also blocks #330 (the ref-arch test must fail when a leverage tofu command fails).

Changes

  • leverage/modules/tf.py: if exit_code := tf.run(...): raise Exit(exit_code) in init, plan, apply, output, destroy, version, format, force-unlock, validate and import (same pattern as tfautomv.py). Since Exit propagates out of invoke_for_all_commands, --layers stops at the first failing layer.
  • tests/conftest.py: leverage_runner accepts an optional working_directory (its docstring already documented it), to run from a layers-group with --layers.
  • tests/test_modules/test_tf.py: CLI tests asserting result.exit_code:
    • every command × tofu/terraform × binary exit code 0/1/2;
    • --layers stops at the first failing layer, and runs all of them when none fails.

Testing

  • Full unit suite: 301 passed.
  • Without the fix, 49 of the new tests fail (every non-zero case and the --layers stop case).

Out of scope

validate-layout returns False on an invalid layout but still exits 0. That was already the case before #316, so it is left for a separate change.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Terraform and OpenTofu commands now return the command’s exit status when an operation fails, making failures easier to identify.
    • In multi-layer runs, execution stops at the first failed layer, and later layers are not run.
    • Successful commands continue to run across all configured layers.

Every `leverage tofu|terraform` command discarded the exit code returned by
`TFRunner.run`, so the CLI always exited 0, even when the binary failed. With
`--layers` it also kept running the remaining layers after a failing one.

Restore the behavior from before #316: raise `Exit(exit_code)` on a non-zero
code for init, plan, apply, output, destroy, version, format, force-unlock,
validate and import, which also stops `--layers` at the first failing layer.

Closes #335

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Terraform and OpenTofu commands now propagate nonzero binary exit codes through the CLI. Multi-layer execution stops after a failing layer. Tests cover exit codes and layer execution.

Changes

Terraform and OpenTofu exit codes

Layer / File(s) Summary
Propagate binary exit codes
leverage/modules/tf.py
Terraform and OpenTofu command handlers raise Exit with the nonzero code returned by tf.run. The multi-layer loop comment now describes that a failing layer stops subsequent layers.
Test exit codes and layer execution
tests/conftest.py, tests/test_modules/test_tf.py
The test runner accepts an optional working directory. Tests check exit-code propagation and verify that layer execution stops after a failure or runs all layers when successful.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Possibly related issues

  • binbashar/leverage#330 — This change provides the exit-code propagation that the issue’s CI script expects. The issue’s other reported problems are not addressed.

Suggested reviewers: angelofenoglio, juanmatias

Merge Risk: Minimal · up to 1c4f3

This change restores propagation of tofu/terraform exit codes and stops multi-layer runs at the first failing layer. It comes with targeted tests, and no concrete merge-blocking risk was identified.

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 states the primary change: the Terraform/OpenTofu CLI now propagates the underlying binary's exit code, which matches the raw summary and the linked issue #335 objectives. It is concise, spe…
Linked Issues check Passed [#335] requires that leverage tofu and leverage terraform exit with the underlying binary's exit code for init, plan, apply, output, destroy, version, format, force-unlock, validate, and import. At he…
Out of Scope Changes check Passed All changes map to #335. The tf.py edits implement the exit-code propagation for the listed commands. The invoke_for_all_commands edit only updates comments to describe the new stop behavior. The test…
Full details: Title check

Explanation

The title states the primary change: the Terraform/OpenTofu CLI now propagates the underlying binary's exit code, which matches the raw summary and the linked issue #335 objectives. It is concise, specific, and does not use vague terms. The 'Fix' prefix is appropriate because the issue describes a regression from v3.1.0.

Full details: Linked Issues check

Explanation

[#335] requires that leverage tofu and leverage terraform exit with the underlying binary's exit code for init, plan, apply, output, destroy, version, format, force-unlock, validate, and import. At head, each of these executors in leverage/modules/tf.py uses if exit_code := tf.run(...): raise Exit(exit_code). Exit is imported from click.exceptions. The apply executor also logs the failure before raising. Because Exit propagates out of the command call in invoke_for_all_commands, a failing layer stops the --layers loop, which meets the stop-at-first-failure requirement. The new test_exit_code_is_propagated test covers every listed command, both groups, and binary codes 0, 1, and 2, including plan -detailed-exitcode. The test_layers_stop_at_the_first_failing_layer and test_layers_run_all_layers_when_none_fails tests cover the --layers behavior. The requirements are coding requirements and the implementation and tests are present in the reviewed diff. The PR author reports a full unit suite run; that run was not independently executed here.

Full details: Out of Scope Changes check

Explanation

All changes map to #335. The tf.py edits implement the exit-code propagation for the listed commands. The invoke_for_all_commands edit only updates comments to describe the new stop behavior. The tests/conftest.py change adds an optional working_directory to leverage_runner, which the new --layers tests require. The tests/test_modules/test_tf.py additions verify the fix. validate-layout is unchanged and is excluded from the issue's command list, so it is not an out-of-scope change. The PR description notes this as a known, separate item.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • 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

A rabbit checks the plan’s return,
Then marks the code for all to learn.
If one layer fails, the next must wait,
The proper exit seals its fate.
The burrow’s tests confirm the gate.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: b6552fbb-57e4-4884-ab8b-55b31c53340e
Commits

Reviewing files that changed from the base of the PR and between 0fca1ae and 1c4f33b.

Files selected for processing: 3

  • leverage/modules/tf.py
  • tests/conftest.py
  • tests/test_modules/test_tf.py

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

@coveralls

coveralls commented Nov 13, 2026 •

Copy link
Copy Markdown

Coverage Report for CI Build 43355745844

Coverage increased (+1.7%) to 67.689%

Details

  • Coverage increased (+1.7%) from the base build.
  • Patch coverage: 21 of 21 lines across 1 file are fully covered (100%).
  • 26 coverage regressions across 1 file.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

26 previously-covered lines in 1 file lost coverage.

File Lines Losing Coverage Coverage
modules/tf.py 26 86.99%

Coverage Stats

Coverage Status
Relevant Lines: 4070
Covered Lines: 2898
Line Coverage: 71.2%
Relevant Branches: 1080
Covered Branches: 588
Branch Coverage: 54.44%
Branches in Coverage %: Yes
Coverage Strength: 0.71 hits per line

💛 - Coveralls

@diego-ojeda-binbash diego-ojeda-binbash self-assigned this Nov 13, 2026
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.

BUG | leverage tofu/terraform always exits 0, even when the binary fails

3 participants