From 7a157294bcdcb3f687ce1bb4a78f6d3d07846599 Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Fri, 30 Oct 2026 23:24:43 -0400 Subject: [PATCH 1/2] fix(terraform): report resolved component name in exec metadata for path-style args The single-component exec-metadata record captured args[0] in RunE, before a path-style argument (./components/terraform/vpc) is resolved to its logical component name. The parser now receives info.ComponentFromArg when it runs, so plan/apply/deploy report the resolved name. Fixes #3110 Co-Authored-By: Claude Sonnet 5.5 --- cmd/terraform/apply.go | 14 +++------- cmd/terraform/deploy.go | 14 +++------- cmd/terraform/destroy.go | 6 +---- cmd/terraform/plan.go | 14 +++------- cmd/terraform/utils.go | 18 +++++++------ cmd/terraform/utils_exec_metadata_test.go | 24 ++++++++--------- internal/exec/shell_utils.go | 9 ++++--- .../exec/shell_utils_exec_metadata_test.go | 4 +-- internal/exec/terraform.go | 4 +-- .../terraform_exec_metadata_flags_test.go | 27 ++++++++++++++++--- ...erraform_execute_helpers_workspace_test.go | 2 +- 11 files changed, 68 insertions(+), 68 deletions(-) diff --git a/cmd/terraform/apply.go b/cmd/terraform/apply.go index ce0abd09a72..9cd85cb061b 100644 --- a/cmd/terraform/apply.go +++ b/cmd/terraform/apply.go @@ -73,16 +73,10 @@ For complete Terraform/OpenTofu documentation, see: // receives it in real time, and the buffer collects a copy. ciMode := ci.ModeEnabled(cmd) - // component/stack for the exec-metadata Data payload (research.md - // Decision 21) — the multi-component --affected/--all path never - // reaches captureExecMetadataSync's single-component parser closure - // (gated out by its existing info.NodeHooks == nil check), so an - // empty component here (no positional arg) is expected, not an error. - var execComponent string - if len(args) > 0 { - execComponent = args[0] - } - shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(execComponent, v.GetString("stack")) + // The exec-metadata parser receives the resolved component name when it + // runs (a path-style argument is only resolved later), so only the stack is + // passed here. + shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(v.GetString("stack")) err = terraformRunWithOptions(terraformCmd, cmd, args, opts, shellOpts...) diff --git a/cmd/terraform/deploy.go b/cmd/terraform/deploy.go index 322d4b7778b..aa3b2bce963 100644 --- a/cmd/terraform/deploy.go +++ b/cmd/terraform/deploy.go @@ -69,16 +69,10 @@ This ensures that the changes defined in your Terraform configuration are applie // receives it in real time, and the buffer collects a copy. ciMode := ci.ModeEnabled(cmd) - // component/stack for the exec-metadata Data payload (research.md - // Decision 21) — the multi-component --affected/--all path never - // reaches captureExecMetadataSync's single-component parser closure - // (gated out by its existing info.NodeHooks == nil check), so an - // empty component here (no positional arg) is expected, not an error. - var execComponent string - if len(args) > 0 { - execComponent = args[0] - } - shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(execComponent, v.GetString("stack")) + // The exec-metadata parser receives the resolved component name when it + // runs (a path-style argument is only resolved later), so only the stack is + // passed here. + shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(v.GetString("stack")) err = terraformRunWithOptions(terraformCmd, cmd, args, opts, shellOpts...) diff --git a/cmd/terraform/destroy.go b/cmd/terraform/destroy.go index 7f536f74af5..413ab563c42 100644 --- a/cmd/terraform/destroy.go +++ b/cmd/terraform/destroy.go @@ -48,11 +48,7 @@ For complete Terraform/OpenTofu documentation, see: if err != nil { return err } - var component string - if len(args) > 0 { - component = args[0] - } - shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(component, v.GetString("stack")) + shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(v.GetString("stack")) err = terraformRunWithOptions(terraformCmd, cmd, args, opts, shellOpts...) capturedDestroyOutput = ansi.Strip(stdoutBuf.String() + "\n" + stderrBuf.String()) return err diff --git a/cmd/terraform/plan.go b/cmd/terraform/plan.go index 9ba83ef8c0b..7e05807ce53 100644 --- a/cmd/terraform/plan.go +++ b/cmd/terraform/plan.go @@ -77,16 +77,10 @@ For complete Terraform/OpenTofu documentation, see: // 2. A CI platform is auto-detected (e.g., GITHUB_ACTIONS=true). ciMode := ci.ModeEnabled(cmd) - // component/stack for the exec-metadata Data payload (research.md - // Decision 21) — the multi-component --affected/--all path never - // reaches captureExecMetadataSync's single-component parser closure - // (gated out by its existing info.NodeHooks == nil check), so an - // empty component here (no positional arg) is expected, not an error. - var execComponent string - if len(args) > 0 { - execComponent = args[0] - } - shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(execComponent, v.GetString("stack")) + // The exec-metadata parser receives the resolved component name when it + // runs (a path-style argument is only resolved later), so only the stack is + // passed here. + shellOpts, stdoutBuf, stderrBuf := terraformCaptureShellOpts(v.GetString("stack")) err = terraformRunWithOptions(terraformCmd, cmd, args, opts, shellOpts...) diff --git a/cmd/terraform/utils.go b/cmd/terraform/utils.go index 7bf8f9f83d1..94346db39a4 100644 --- a/cmd/terraform/utils.go +++ b/cmd/terraform/utils.go @@ -998,17 +998,19 @@ func buildTerraformExecData(subCommand, output, component, stack string, exitCod // buffers populated regardless of whether Native CI job summaries are also // enabled. Callers read the returned buffers themselves for their own // ciMode-gated CI-job-summary post-processing (e.g. capturedPlanOutput). -// Component/stack are the invocation's already-resolved identity (research.md -// Decision 21), threaded through to buildTerraformExecData's Data payload — -// component is typically args[0], stack the already-parsed --stack value; -// both empty for the multi-component path, which never reaches this closure. -func terraformCaptureShellOpts(component, stack string) (opts []e.ShellCommandOption, stdoutBuf, stderrBuf *bytes.Buffer) { +// The stack is the already-parsed --stack value (research.md Decision 21), +// threaded through to buildTerraformExecData's Data payload. The component is +// not known yet: a path-style argument is only resolved to its logical name +// later, so the parser receives it at invocation time instead of capturing +// args[0] here. Both are empty for the multi-component path, which never +// reaches this closure. +func terraformCaptureShellOpts(stack string) (opts []e.ShellCommandOption, stdoutBuf, stderrBuf *bytes.Buffer) { stdoutBuf = &bytes.Buffer{} stderrBuf = &bytes.Buffer{} opts = []e.ShellCommandOption{ e.WithStdoutCapture(stdoutBuf), e.WithStderrCapture(stderrBuf), - e.WithExecMetadataParser(terraformExecMetadataParserFunc(component, stack)), + e.WithExecMetadataParser(terraformExecMetadataParserFunc(stack)), } return opts, stdoutBuf, stderrBuf } @@ -1024,8 +1026,8 @@ func terraformCaptureShellOpts(component, stack string) (opts []e.ShellCommandOp // (captureExecMetadataSync already computes it from // info.ExecMetadataRawExitCode, research.md Decision 27) — not captured // here, since neither value is known when this closure is created. -func terraformExecMetadataParserFunc(component, stack string) func(subCommand string, exitCode int, output string) any { - return func(subCommand string, exitCode int, output string) any { +func terraformExecMetadataParserFunc(stack string) func(subCommand, component string, exitCode int, output string) any { + return func(subCommand, component string, exitCode int, output string) any { data := buildTerraformExecData(subCommand, ansi.Strip(output), component, stack, exitCode) if data == nil { return nil diff --git a/cmd/terraform/utils_exec_metadata_test.go b/cmd/terraform/utils_exec_metadata_test.go index 4bf0c244647..f978643f449 100644 --- a/cmd/terraform/utils_exec_metadata_test.go +++ b/cmd/terraform/utils_exec_metadata_test.go @@ -358,7 +358,7 @@ func TestEncodeLogs(t *testing.T) { // closure must always be wired, regardless of whether the caller separately // decides to also use ciMode for its own CI-job-summary post-processing. func TestTerraformCaptureShellOpts_AlwaysWiresCaptureAndParser(t *testing.T) { - opts, stdoutBuf, stderrBuf := terraformCaptureShellOpts("web", "plat-use2-dev") + opts, stdoutBuf, stderrBuf := terraformCaptureShellOpts("plat-use2-dev") require.NotNil(t, stdoutBuf) require.NotNil(t, stderrBuf) @@ -380,12 +380,12 @@ func TestTerraformExecMetadataParserFunc_UsesSuppliedOutput(t *testing.T) { fixture, err := os.ReadFile("../../pkg/ci/plugins/terraform/testdata/stdout/apply_success.txt") require.NoError(t, err) - parser := terraformExecMetadataParserFunc("web", "plat-use2-dev") + parser := terraformExecMetadataParserFunc("plat-use2-dev") // Empty output: a covered subcommand ("apply") with unparseable (empty) // output still gets a minimal defaulted Data, not nil (research.md // Decision 29) — exit_code is threaded through even here. - emptyResult := parser("apply", 0, "") + emptyResult := parser("apply", "web", 0, "") require.NotNil(t, emptyResult) emptyWrapper, ok := emptyResult.(map[string]any) require.True(t, ok) @@ -398,7 +398,7 @@ func TestTerraformExecMetadataParserFunc_UsesSuppliedOutput(t *testing.T) { assert.Equal(t, 0, emptyMap["exit_code"]) assert.NotContains(t, emptyMap, "version") - result := parser("apply", 0, string(fixture)) + result := parser("apply", "web", 0, string(fixture)) require.NotNil(t, result) wrapper, ok := result.(map[string]any) require.True(t, ok) @@ -418,17 +418,15 @@ func TestTerraformExecMetadataParserFunc_UsesSuppliedOutput(t *testing.T) { } // TestTerraformExecMetadataParserFunc_ReportsFullComponentNameForNestedComponent -// is the regression guard for spec FR-004: the single-component -// exec-metadata parser closure is called with the full CLI argument -// (cmd/terraform/plan.go's execComponent = args[0]) as its component, not -// the working-directory leaf ProcessStacks derives internally — so a nested -// (slash-containing) logical component name must already pass through here -// unmodified, both before and after the recordExecResult/uploadStatus fix -// (GitHub issue #3102). +// is the regression guard for spec FR-004: the exec-metadata parser closure +// reports the full logical component name it is called with, not the +// working-directory leaf ProcessStacks derives internally — so a nested +// (slash-containing) logical component name must pass through here +// unmodified (GitHub issue #3102). func TestTerraformExecMetadataParserFunc_ReportsFullComponentNameForNestedComponent(t *testing.T) { - parser := terraformExecMetadataParserFunc("foo/bar/baz", "dev") + parser := terraformExecMetadataParserFunc("dev") - result := parser("plan", 0, "") + result := parser("plan", "foo/bar/baz", 0, "") require.NotNil(t, result) wrapper, ok := result.(map[string]any) require.True(t, ok) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index c0432be0f70..46965165f00 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -67,7 +67,7 @@ type shellCommandConfig struct { // import cycle (research.md Decision 18). The output parameter is the // scoped exec-metadata output text executeCommandPipeline captured for // just the main plan/apply/deploy subprocess (FR-006f). - execMetadataParser func(subCommand string, exitCode int, output string) any + execMetadataParser func(subCommand, component string, exitCode int, output string) any // execMetadataStdoutCapture/execMetadataStderrCapture, when set, additionally // tee this specific ExecuteShellCommand call's stdout/stderr into these @@ -113,7 +113,8 @@ func invokingCommandFromOpts(opts ...ShellCommandOption) *cobra.Command { } // WithExecMetadataParser provides a closure that, given the invoking -// subcommand name and the command's own exit code, returns command-specific +// subcommand name, the resolved component name (after any path-style CLI +// argument has been resolved) and the command's own exit code, returns command-specific // structured data (FR-006) for the execution record — or nil if there is // none to report. The exitCode parameter is the terraform/tofu subprocess's // own exit code (research.md Decision 27), supplied by @@ -122,7 +123,7 @@ func invokingCommandFromOpts(opts ...ShellCommandOption) *cobra.Command { // supplies this closure so ExecuteTerraform's exec-metadata sync capture can // obtain parsed terraform plan/apply/deploy output without internal/exec // itself importing the CI plugin's parser (research.md Decision 18). -func WithExecMetadataParser(fn func(subCommand string, exitCode int, output string) any) ShellCommandOption { +func WithExecMetadataParser(fn func(subCommand, component string, exitCode int, output string) any) ShellCommandOption { defer perf.Track(nil, "exec.WithExecMetadataParser")() return func(c *shellCommandConfig) { @@ -132,7 +133,7 @@ func WithExecMetadataParser(fn func(subCommand string, exitCode int, output stri // execMetadataParserFromOpts extracts the parser closure (if any) set via // WithExecMetadataParser among opts. -func execMetadataParserFromOpts(opts ...ShellCommandOption) func(subCommand string, exitCode int, output string) any { +func execMetadataParserFromOpts(opts ...ShellCommandOption) func(subCommand, component string, exitCode int, output string) any { var cfg shellCommandConfig for _, opt := range opts { opt(&cfg) diff --git a/internal/exec/shell_utils_exec_metadata_test.go b/internal/exec/shell_utils_exec_metadata_test.go index e2dde639262..cdc1b70f5db 100644 --- a/internal/exec/shell_utils_exec_metadata_test.go +++ b/internal/exec/shell_utils_exec_metadata_test.go @@ -15,7 +15,7 @@ func TestExecMetadataParserFromOpts_RoundTrips(t *testing.T) { t.Parallel() called := false - fn := func(subCommand string, exitCode int, output string) any { + fn := func(subCommand, component string, exitCode int, output string) any { called = true return subCommand + "-data" } @@ -24,7 +24,7 @@ func TestExecMetadataParserFromOpts_RoundTrips(t *testing.T) { require_ := assert.New(t) require_.NotNil(extracted) - result := extracted("plan", 0, "") + result := extracted("plan", "web", 0, "") require_.True(called) require_.Equal("plan-data", result) } diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index 5a5afa2c001..c009d4c7f1b 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -279,7 +279,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOptio // to stay under the linter's argument-count limit. type execMetadataSyncParams struct { Cmd *cobra.Command - Parser func(subCommand string, exitCode int, output string) any + Parser func(subCommand, component string, exitCode int, output string) any Err error } @@ -327,7 +327,7 @@ func captureExecMetadataSync(atmosConfig *schema.AtmosConfiguration, subCommand var data any if params.Parser != nil { - data = params.Parser(subCommand, rawExitCode, info.ExecMetadataRawOutput) + data = params.Parser(subCommand, info.ComponentFromArg, rawExitCode, info.ExecMetadataRawOutput) } // info.ExecMetadataRawMetrics is typed `any` (not *process.ProcessMetrics) diff --git a/internal/exec/terraform_exec_metadata_flags_test.go b/internal/exec/terraform_exec_metadata_flags_test.go index 469029cd8c3..20ef84dde47 100644 --- a/internal/exec/terraform_exec_metadata_flags_test.go +++ b/internal/exec/terraform_exec_metadata_flags_test.go @@ -170,7 +170,7 @@ func TestCaptureExecMetadataSync_CallsParserForSyncAllowlistedSingleComponent(t var calls atomic.Int32 var gotSubCommand string var gotExitCode int - parser := func(subCommand string, exitCode int, output string) any { + parser := func(subCommand, component string, exitCode int, output string) any { calls.Add(1) gotSubCommand = subCommand gotExitCode = exitCode @@ -200,7 +200,7 @@ func TestCaptureExecMetadataSync_NeverCallsParserForNonSyncSubcommand(t *testing info := &schema.ConfigAndStacksInfo{} var calls atomic.Int32 - parser := func(string, int, string) any { + parser := func(string, string, int, string) any { calls.Add(1) return "should never be reached" } @@ -221,7 +221,7 @@ func TestCaptureExecMetadataSync_NeverCallsParserWhenNodeHooksWired(t *testing.T info := &schema.ConfigAndStacksInfo{NodeHooks: fakeNodeHooks{}} var calls atomic.Int32 - parser := func(string, int, string) any { + parser := func(string, string, int, string) any { calls.Add(1) return "should never be reached" } @@ -230,3 +230,24 @@ func TestCaptureExecMetadataSync_NeverCallsParserWhenNodeHooksWired(t *testing.T assert.Equal(t, int32(0), calls.Load()) } + +// TestCaptureExecMetadataSync_ParserReceivesResolvedComponent verifies the +// parser is handed info.ComponentFromArg, which holds the logical component +// name once a path-style CLI argument has been resolved, rather than a name +// captured from the raw argument before resolution. +func TestCaptureExecMetadataSync_ParserReceivesResolvedComponent(t *testing.T) { + t.Setenv("CI", "") + + atmosConfig := &schema.AtmosConfiguration{} + info := &schema.ConfigAndStacksInfo{ComponentFromArg: "vpc", Stack: "plat-use2-dev"} + + var gotComponent string + parser := func(_, component string, _ int, _ string) any { + gotComponent = component + return nil + } + + captureExecMetadataSync(atmosConfig, "plan", info, execMetadataSyncParams{Parser: parser}) + + assert.Equal(t, "vpc", gotComponent) +} diff --git a/internal/exec/terraform_execute_helpers_workspace_test.go b/internal/exec/terraform_execute_helpers_workspace_test.go index d525a3561cd..5dc562a02b9 100644 --- a/internal/exec/terraform_execute_helpers_workspace_test.go +++ b/internal/exec/terraform_execute_helpers_workspace_test.go @@ -392,7 +392,7 @@ func TestCaptureExecMetadataSync_RawExitCodeSurvivesGlobalRemap(t *testing.T) { } var gotExitCode int - parser := func(subCommand string, exitCode int, output string) any { + parser := func(subCommand, component string, exitCode int, output string) any { gotExitCode = exitCode return map[string]any{"exit_code": exitCode} } From eaf47a17698f5b11a9889a3e5c893d3f33f57c8e Mon Sep 17 00:00:00 2001 From: Alex Castillo Date: Fri, 6 Nov 2026 18:09:51 -0400 Subject: [PATCH 2/2] test(terraform): assert exec metadata reports the component instance, not its directory Covers a metadata.component alias (instance network/primary backed by components/terraform/vpc) and asserts the component in the round-trip test. Co-Authored-By: Claude Sonnet 5.5 --- .../exec/shell_utils_exec_metadata_test.go | 3 ++ .../terraform_exec_metadata_flags_test.go | 29 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/internal/exec/shell_utils_exec_metadata_test.go b/internal/exec/shell_utils_exec_metadata_test.go index cdc1b70f5db..0ff0fe2680c 100644 --- a/internal/exec/shell_utils_exec_metadata_test.go +++ b/internal/exec/shell_utils_exec_metadata_test.go @@ -15,8 +15,10 @@ func TestExecMetadataParserFromOpts_RoundTrips(t *testing.T) { t.Parallel() called := false + var gotComponent string fn := func(subCommand, component string, exitCode int, output string) any { called = true + gotComponent = component return subCommand + "-data" } @@ -26,6 +28,7 @@ func TestExecMetadataParserFromOpts_RoundTrips(t *testing.T) { result := extracted("plan", "web", 0, "") require_.True(called) + require_.Equal("web", gotComponent) require_.Equal("plan-data", result) } diff --git a/internal/exec/terraform_exec_metadata_flags_test.go b/internal/exec/terraform_exec_metadata_flags_test.go index 20ef84dde47..b7934d0a358 100644 --- a/internal/exec/terraform_exec_metadata_flags_test.go +++ b/internal/exec/terraform_exec_metadata_flags_test.go @@ -251,3 +251,32 @@ func TestCaptureExecMetadataSync_ParserReceivesResolvedComponent(t *testing.T) { assert.Equal(t, "vpc", gotComponent) } + +// TestCaptureExecMetadataSync_ParserReceivesInstanceNotDirectory verifies the +// parser is handed the component instance name (the stack key) even when +// metadata.component points that instance at a different physical directory. +// Here the instance "network/primary" lives in components/terraform/vpc; the +// metadata must report the instance, never the directory ("vpc") or its last +// segment ("primary"). +func TestCaptureExecMetadataSync_ParserReceivesInstanceNotDirectory(t *testing.T) { + t.Setenv("CI", "") + + atmosConfig := &schema.AtmosConfiguration{} + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "network/primary", + Component: "primary", + FinalComponent: "vpc", + BaseComponentPath: "vpc", + Stack: "dev", + } + + var gotComponent string + parser := func(_, component string, _ int, _ string) any { + gotComponent = component + return nil + } + + captureExecMetadataSync(atmosConfig, "plan", info, execMetadataSyncParams{Parser: parser}) + + assert.Equal(t, "network/primary", gotComponent) +}