Repository navigation
fix: terraform plan-diff respects metadata.component and workdir - #2056
Conversation
Previously, `atmos terraform plan-diff` would look for planfiles in the wrong directory when the component instance name differed from the actual component (e.g., "foobar-atmos-pro" with metadata.component: "foobar"). The fix adds a ProcessStacks() call to resolve FinalComponent from metadata.component before constructing the component path. Changes: - Add ProcessStacks() call in TerraformPlanDiff to resolve component metadata - Update tests to be proper unit tests for helper functions - Add comprehensive test coverage for comparison and formatting functions - Add test fixtures for plan-diff metadata.component testing - Add documentation in docs/fixes/plan-diff-metadata-component.md - Add vendor-simple-excludes/components to .gitignore Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
Warning This PR exceeds the recommended limit of 1,000 lines.Large PRs are difficult to review and may be rejected due to their size. Please verify that this PR does not address multiple issues. |
Dependency Review✅ No vulnerabilities or license issues found.Scanned FilesNone |
📝 WalkthroughWalkthroughInvoke ProcessStacks before Terraform plan-diff to resolve component metadata (metadata.component and component_folder_prefix); subsequent flag parsing, planfile path construction, preparation, and comparison use the processed info. Adds fixtures, docs, and expanded unit tests for the new flow and helpers. Changes
Sequence DiagramsequenceDiagram
participant User as User
participant TF as TerraformPlanDiff
participant PS as ProcessStacks
participant PlanOps as PlanOperations
User->>TF: invoke plan-diff
TF->>PS: ProcessStacks(stack, component args)
PS-->>TF: processedInfo (FinalComponent, ComponentFolderPrefix, AdditionalArgsAndFlags)
TF->>PlanOps: parse flags from processedInfo.AdditionalArgsAndFlags
TF->>PlanOps: prepareNewPlanFile(&processedInfo)
TF->>PlanOps: comparePlansAndGenerateDiff(&processedInfo)
PlanOps-->>TF: diff output / errors
TF-->>User: present diff or error
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2056 +/- ##
==========================================
+ Coverage 76.01% 76.06% +0.04%
==========================================
Files 797 797
Lines 74558 74558
==========================================
+ Hits 56674 56710 +36
+ Misses 14338 14304 -34
+ Partials 3546 3544 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
📝 WalkthroughWalkthroughThe PR fixes terraform plan-diff to respect metadata.component by resolving component metadata before path resolution. TerraformPlanDiff now calls ProcessStacks to obtain FinalComponent and ComponentFolderPrefix, enabling correct plan file location. Tests are refactored from integration-style to unit-focused approaches with comprehensive test fixtures added. Changes
Sequence DiagramsequenceDiagram
participant Client as TerraformPlanDiff
participant PS as ProcessStacks
participant Parser as parsePlanDiffFlags
participant FileOps as Plan File Operations
participant Diff as comparePlansAndGenerateDiff
Client->>PS: Call with original info
PS->>PS: Resolve FinalComponent & ComponentFolderPrefix from metadata.component
PS-->>Client: Return processedInfo
Client->>Parser: Parse flags with processedInfo
Parser-->>Client: Return parsed arguments
Client->>FileOps: Prepare plan file with resolved component path
FileOps->>FileOps: Locate/copy plan files under components/terraform/<component>/
FileOps-->>Client: Plan files ready
Client->>Diff: Compare plans with processedInfo
Diff->>Diff: Generate resource/attribute/output diffs
Diff-->>Client: Return diff results
Estimated code review effort🎯 4 (Complex) | ⏱️ ~60 minutes Possibly related PRs
Suggested reviewers
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
|
CodeRabbit (@coderabbitai) full review please |
|
🧠 Learnings used✅ Actions performedFull review triggered. |
|
🧠 Learnings used✅ Actions performedFull review triggered. |
There was a problem hiding this comment.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
internal/exec/terraform_plan_diff_main_test.go (1)
46-52:⚠️ Potential issue | 🟡 MinorThis test documents a real flag parsing quirk.
When
--origis followed by another flag (--new=new-plan.out), the parser treats that flag string as the value for--orig. The space-separated branch inparsePlanDiffFlags(line 95-96 interraform_plan_diff.go) doesn't check whether the next argument starts with--.Not a blocker — the bogus path would fail at
validateOriginalPlanFile. But if you ever want to tighten this up, checking for a--prefix on the lookahead would catch the misuse earlier with a clearer error.
🤖 Fix all issues with AI agents
In `@internal/exec/terraform_plan_diff.go`:
- Around line 60-61: The comment above the componentPath construction is missing
a trailing period; update the comment that references component path and
ProcessStacks to end with a period. Locate the comment near the componentPath
variable creation (where filepath.Join is called with
atmosConfig.TerraformDirAbsolutePath, processedInfo.ComponentFolderPrefix,
processedInfo.FinalComponent) and append a period to the end of that comment so
it conforms to the godot linter rule.
🧹 Nitpick comments (3)
internal/exec/terraform_plan_diff_main_test.go (1)
228-253: Duplicate coverage withTestParsePlanDiffFlagsabove.
TestTerraformPlanDiff_FlagParsing(lines 228-253) tests the same scenarios already covered by the table-drivenTestParsePlanDiffFlags(lines 12-87): missing orig, only orig, both with=, both with space. Consider removing this function to avoid maintaining the same assertions in two places.internal/exec/terraform_plan_diff_test.go (2)
421-432: Useerrors.Is()instead of direct equality for sentinel errors.Lines 424 and 431 compare errors with
assert.Equal. The coding guidelines requireerrors.Is()for error checking, which handles wrapped errors correctly.Proposed fix
t.Run("empty_output_returns_ErrNoJSONOutput", func(t *testing.T) { _, err := extractJSONFromOutput("") assert.Error(t, err) - assert.Equal(t, ErrNoJSONOutput, err) + assert.ErrorIs(t, err, ErrNoJSONOutput) }) t.Run("no_json_content_returns_ErrNoJSONOutput", func(t *testing.T) { _, err := extractJSONFromOutput("some random text without json") assert.Error(t, err) - assert.Equal(t, ErrNoJSONOutput, err) + assert.ErrorIs(t, err, ErrNoJSONOutput) })As per coding guidelines: "Use
errors.Is()for error checking."
360-373:parsePlanDiffFlagsis now tested in three places.This function,
TestPlanDiffCommandFlags, plusTestParsePlanDiffFlagsandTestTerraformPlanDiff_FlagParsinginterraform_plan_diff_main_test.goall cover the sameparsePlanDiffFlagslogic. Consider consolidating into the table-driven test in the main test file.
- Remove duplicate TestTerraformPlanDiff_FlagParsing function - Remove duplicate TestPlanDiffCommandFlags function - Use assert.ErrorIs() instead of assert.Equal() for sentinel errors Co-Authored-By: Claude Opus 4.5 <[email protected]>
Co-Authored-By: Claude Opus 4.5 <[email protected]>
|
Does it work with source vendoring and workdir ? |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@internal/exec/terraform_plan_diff.go`:
- Around line 38-45: The ProcessStacks call returns an error that is currently
returned raw; change the error handling in the processedInfo assignment so the
returned error is wrapped with contextual information (e.g., using
fmt.Errorf("process stacks for component metadata: %w", err) or your project's
error-wrap helper) to match the rest of the function's style; reference the
ProcessStacks call and variables processedInfo, atmosConfig and info when adding
the context so callers can trace which config/step failed.
🧹 Nitpick comments (2)
internal/exec/terraform_plan_diff_test.go (2)
842-854: Trim the redundant doc comment block.The function doc says the same thing three different ways (lines 842-844, then 846-847, then 849-854). One concise paragraph is enough.
Suggested trimmed comment
-// TestTerraformPlanDiffMetadataComponentResolution tests that plan-diff respects metadata.component -// when resolving the component path. This reproduces the bug where plan-diff looks in the wrong -// directory when the component instance name differs from the actual component (metadata.component). -// -// TestTerraformPlanDiffMetadataComponentResolution tests that the component path -// is correctly resolved when using metadata.component. -// -// Bug fixed: Component instance "foobar-atmos-pro" with metadata.component: "foobar" -// Expected: Look for planfile in components/terraform/foobar/ -// Actual (bug): Was looking for planfile in components/terraform/foobar-atmos-pro/ (wrong!) -// -// The fix: TerraformPlanDiff now calls ProcessStacks() to resolve FinalComponent -// from metadata.component before constructing the component path. +// TestTerraformPlanDiffMetadataComponentResolution verifies that plan-diff uses FinalComponent +// (resolved from metadata.component) rather than ComponentFromArg when constructing +// the component path. This prevents searching the wrong directory (e.g., +// components/terraform/foobar-atmos-pro/ instead of components/terraform/foobar/).
957-967: Move thecontainslogic to a shared utility package to avoid duplication.Both
internal/exec/terraform_plan_diff_comparison.go:514andcmd/cmd_utils.go:1454implement identical slice-contains logic. Sinceinternal/execshouldn't depend oncmd(architectural layering), consolidate this intopkg/utilsor a similar shared location and have both call it.
- Check WorkdirPathKey in ComponentSection for workdir path resolution - Replace local contains() with shared u.SliceContainsString utility - Add tests for workdir and source vendoring scenarios - Update documentation with workdir fix details - Address code review feedback (trimmed comments, error wrapping) Co-Authored-By: Claude Opus 4.5 <[email protected]>
yes. Also added tests for that |
|
These changes were released in v1.206.0-rc.2. |
what
atmos terraform plan-diffto properly resolvemetadata.componentwhen looking for planfilesProcessStacks()to resolveFinalComponentfrommetadata.componentbefore constructing the component path.workdir/terraform/<stack>-<component>/)why
foobar-atmos-prowithmetadata.component: foobar),plan-diffwas looking in the wrong directorycomponents/terraform/foobar/components/terraform/foobar-atmos-pro/orcomponents/terraform/.workdir/path, not the base component pathgithub-action-atmos-terraform-applywhen comparing planfiles for components withmetadata.componentset or workdir enabledchanges
internal/exec/terraform_plan_diff.goProcessStackscall + workdir path supportinternal/exec/terraform_plan_diff_comparison.gou.SliceContainsStringutilityinternal/exec/terraform_plan_diff_test.godocs/fixes/plan-diff-metadata-component.mdreferences
docs/fixes/plan-diff-metadata-component.mdSummary by CodeRabbit
Bug Fixes
Documentation
Tests