Repository navigation
Fix destroy command failure for environments in Created state - #53
Merged
Merged
Conversation
Co-authored-by: josecelano <[email protected]>
Co-authored-by: josecelano <[email protected]>
Copilot
AI
changed the title
[WIP] Fix destroy command to handle created state gracefully
Fix destroy command failure for environments in Created state
Oct 27, 2025
Member
|
@copilot the testing workflow is failing: |
Contributor
Author
... Fixed in commit 0d9c043. The doctest was using an incorrect function signature - updated to use |
- Update all test fixtures to use absolute SSH key paths via CARGO_MANIFEST_DIR - Fix E2E tests to use correct CLI structure: 'create environment' instead of 'create' - Resolves test failures caused by relative path resolution issues - Fixes ProcessRunner to include 'environment' subcommand in cargo run invocation All 974 library tests and 4 E2E tests now pass successfully.
josecelano
force-pushed
the
copilot/fix-destroy-command-created-state
branch
from
October 27, 2025 16:10
0d9c043 to
3e6fc3e
Compare
- Update doctest example to use CreateAction enum - Correct function name from 'handle' to 'handle_create_command' - Add proper imports for PathBuf and CreateAction - Ensures doctest compiles and runs successfully
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix Destroy Command: Handle Created State Gracefully ✅
Issue: #X (Subissue 8/10 of Epic #34)
Status: ✅ Complete - Ready for Merge
Summary
This PR fixes a critical bug where the destroy command failed when attempting to destroy environments in the
Createdstate. The command unconditionally tried to destroy OpenTofu infrastructure that was never provisioned, resulting in "No such file or directory" errors.Solution
The fix uses a simple and reliable approach: check if the OpenTofu build directory exists before attempting infrastructure destruction. If the directory doesn't exist, infrastructure destruction is skipped.
Changes Made
Files Modified:
src/application/command_handlers/destroy.rs- Core fix for destroy commandsrc/presentation/commands/create/mod.rs- Fixed pre-existing doctest bugAdded
should_destroy_infrastructure()helper functionenvironment.tofu_build_dir().exists()trueif infrastructure should be destroyed,falseotherwiseUpdated
execute_destruction_with_tracking()Added 3 comprehensive unit tests
it_should_skip_infrastructure_destruction_when_tofu_build_dir_does_not_exist- Tests Created state handlingit_should_attempt_infrastructure_destruction_when_tofu_build_dir_exists- Tests Provisioned state (regression)it_should_clean_up_state_files_regardless_of_infrastructure_state- Tests cleanup always happensFixed doctest in create command module
CreateActionenumhandle()function instead ofhandle_create_command()Test Results
Before and After
DestroyFailedDestroyedImplementation Details
Why Directory Existence Check Works:
Backward Compatibility:
Architecture Compliance
Original prompt
This section details on the original issue you should resolve
<issue_title>[Subissue 8/10] Fix Destroy Command: Handle Created State Gracefully</issue_title>
<issue_description># Fix Destroy Command: Handle Created State Gracefully
Epic Subissue: 8 of 9
Issue: #X (to be assigned)
Parent Epic: #34 - Create Environment Command
Related: Destroy Command Handler, Destroy Presentation Layer, Subissue 9 (Fix Working Directory Support - to be created)
Overview
Fix a critical bug in the destroy command where it fails when attempting to destroy environments in the
Createdstate. The command unconditionally tries to destroy OpenTofu infrastructure that was never provisioned, causing the operation to fail with "No such file or directory" errors.This bug prevents users from cleaning up newly created environments that were never provisioned, forcing manual cleanup or confusing error messages.
Note: This issue focuses only on the Created state handling bug. A separate issue (Subissue 9) will address the working directory parameter support. For manual testing in this issue, use the default working directory (environments in
./data/) to avoid the working directory bug.Goals
Createdstate environments gracefully🏗️ Architecture Requirements
DDD Layer: Application (command logic)
Module Path:
src/application/command_handlers/destroy.rsPattern: Command Handler Pattern
Module Structure Requirements
.help()methodsArchitectural Constraints
Anti-Patterns to Avoid
Specifications
Current Behavior (Incorrect)
When destroying an environment in the
Createdstate (never provisioned), the destroy command unconditionally attempts to run OpenTofu destroy operations:This causes errors like:
The environment transitions to
DestroyFailedstate with this error context:{ "DestroyFailed": { "context": { ... }, "state": { "context": { "failed_step": "DestroyInfrastructure", "error_kind": "CommandExecution", "error_summary": "Command execution failed: Failed to start command 'tofu destroy ...", ... } } } }Expected Behavior
The destroy command should be state-aware and skip infrastructure destruction for environments that never had infrastructure provisioned: