You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Malformed config.tf file when first creating backend layer #321
The backend layer should be correctly initialized, so it can be applied and then, with command leverage tofu init the backend should be migrated to the newly created bucket.
Steps to Reproduce
Steps to reproduce the behavior:
Create a new backend layer
Go to to the new layer
Ensure s3 key is commented out in the config.tf file
Run leverage tofu init --skip-validation
See error as described above
Environment (please complete the following information):
Anything that will help us triage the bug will help. Here are some ideas:
get_backend_key() has a try/except tree. The else branch correctly detects the commented-out backend block and raises an ExitError with a specific message:
Malformed config.tf file. Missing backend block.
In some cases you may want to skip this check by using the --skip-validation flag...
But ExitError extends Exception, so the bare except Exception handler catches it and re-raises with the generic:
Malformed config.tf file. Unable to parse.
The helpful message is silently discarded.
Fix: Added except ExitError: raise before the generic handler.
Bug 3 — set_backend_key requires a pre-existing backend block
set_backend_key had a strict validation gate checking that the backend "s3" block exists before adding the key. For bootstrap tf-backend layers, the block is intentionally commented out (# Uncomment after
first \apply``). This created a catch-22: the code path wanted to set the key, but its own validation prevented it.
Fix: Changed the validation from a hard error to a no-op — if no backend block exists, set_backend_key simply returns without modifying the file.
--skip-validation skips validate_for_all_commands() → _validate_layout(), but the same code path still runs get_backend_key() → set_backend_key() in invoke_for_all_commands() (tf.py:274-278). Bug 2 (exception instead of None) caused that path to fail regardless of the flag.
if (structure_checks):
returnconfig_tf["terraform"][0]["backend"][0]["s3"].get("key")
else:
returnNone
The else branch raised ExitError when the backend block was missing (commented out). Both callers (invoke_for_all_commands and _validate_layout in tf.py) already handle a None return gracefully — they either
call set_backend_key or log "No backend key found."
Added except ExitError: raise so any ExitError raised inside the try block propagates instead of being swallowed by the bare Exception handler. Also prints the real exception in the catch-all so genuine parse
failures aren't opaque.
Fix 3 — set_backend_key(): no-op instead of error when backend block missing
Before (hard error):
ifnot (structure_checks):
raiseExitError(1, f"Malformed config.tf: File must contain a terraform block with an S3 backend.\n...")
backend_config=config_tf["terraform"][0]["backend"][0]["s3"]
key_exists="key"inbackend_configmodified_content=_modify_backend_key(content, key, key_exists)
config_file_path.write_text(modified_content)
After (graceful skip):
if (structure_checks):
backend_config=config_tf["terraform"][0]["backend"][0]["s3"]
key_exists="key"inbackend_configmodified_content=_modify_backend_key(content, key, key_exists)
config_file_path.write_text(modified_content)
else:
# No backend block yet (e.g. bootstrap tf-backend layer with commented-out backend)pass
The validation gate was inverted from a hard error to a conditional. If no backend "s3" block exists (commented out), it silently skips — the user will uncomment and re-run tofu init after apply.
Activity
juanmatias commentedon Jul 31, 2026
Report: Root Cause of leverage tofu init Error
Error
New backend layer.
In config.tf the s3 key lines are commented out.
Error:
Testing it with s3 key lines uncommented worked ok (failed because bucket does not exist).
Testing it with s3 key lines deleted failed with same message.
Root Cause — 3 bugs in the installed leverage CLI
All three bugs are in /home/jdelacamara/Work/OvenLabs/code/ovenlabs-leverage-infra/.venv/lib/python3.14/site-packages/leverage/_backend_config.py.
────────────────────────────────────────────────────────────────────────────────
Bug 1 — Exception swallowing (wrong error message shown)
get_backend_key() has a try/except tree. The else branch correctly detects the commented-out backend block and raises an ExitError with a specific message:
But ExitError extends Exception, so the bare except Exception handler catches it and re-raises with the generic:
The helpful message is silently discarded.
Fix: Added except ExitError: raise before the generic handler.
────────────────────────────────────────────────────────────────────────────────
Bug 2 — get_backend_key raises instead of returning None
Its return type is Optional[str], and both callers handle None gracefully:
Yet the else branch raised ExitError instead of returning None.
Fix: Changed else: raise ExitError(...) to else: return None.
────────────────────────────────────────────────────────────────────────────────
Bug 3 — set_backend_key requires a pre-existing backend block
set_backend_key had a strict validation gate checking that the backend "s3" block exists before adding the key. For bootstrap tf-backend layers, the block is intentionally commented out (# Uncomment after
first \apply``). This created a catch-22: the code path wanted to set the key, but its own validation prevented it.
Fix: Changed the validation from a hard error to a no-op — if no backend block exists, set_backend_key simply returns without modifying the file.
────────────────────────────────────────────────────────────────────────────────
Why --skip-validation also failed
--skip-validation skips validate_for_all_commands() → _validate_layout(), but the same code path still runs get_backend_key() → set_backend_key() in invoke_for_all_commands() (tf.py:274-278). Bug 2 (exception instead of None) caused that path to fail regardless of the flag.
juanmatias commentedon Jul 31, 2026
Workaroud report
All edits were in a single file. Here's the summary:
File Changed
/home/jdelacamara/Work/OvenLabs/code/ovenlabs-leverage-infra/.venv/lib/python3.14/site-packages/leverage/_backend_config.py
Fix 1 — get_backend_key(): return None instead of raising
Before:
After:
The else branch raised ExitError when the backend block was missing (commented out). Both callers (invoke_for_all_commands and _validate_layout in tf.py) already handle a None return gracefully — they either
call set_backend_key or log "No backend key found."
────────────────────────────────────────────────────────────────────────────────
────────────────────────────────────────────────────────────────────────────────
Fix 2 — get_backend_key(): exception chaining (defensive)
Before:
After:
Added except ExitError: raise so any ExitError raised inside the try block propagates instead of being swallowed by the bare Exception handler. Also prints the real exception in the catch-all so genuine parse
failures aren't opaque.
────────────────────────────────────────────────────────────────────────────────
Fix 3 — set_backend_key(): no-op instead of error when backend block missing
Before (hard error):
After (graceful skip):
The validation gate was inverted from a hard error to a conditional. If no backend "s3" block exists (commented out), it silently skips — the user will uncomment and re-run tofu init after apply.