Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions cmd/terraform/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down
14 changes: 4 additions & 10 deletions cmd/terraform/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down
6 changes: 1 addition & 5 deletions cmd/terraform/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 4 additions & 10 deletions cmd/terraform/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)

Expand Down
18 changes: 10 additions & 8 deletions cmd/terraform/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down
24 changes: 11 additions & 13 deletions cmd/terraform/utils_exec_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 5 additions & 4 deletions internal/exec/shell_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions internal/exec/shell_utils_exec_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ func TestExecMetadataParserFromOpts_RoundTrips(t *testing.T) {
t.Parallel()

called := false
fn := func(subCommand string, exitCode int, output string) any {
var gotComponent string
fn := func(subCommand, component string, exitCode int, output string) any {
called = true
gotComponent = component
return subCommand + "-data"
}

extracted := execMetadataParserFromOpts(WithExecMetadataParser(fn))
require_ := assert.New(t)
require_.NotNil(extracted)

result := extracted("plan", 0, "")
result := extracted("plan", "web", 0, "")
require_.True(called)
require_.Equal("web", gotComponent)
require_.Equal("plan-data", result)
}

Expand Down
4 changes: 2 additions & 2 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
56 changes: 53 additions & 3 deletions internal/exec/terraform_exec_metadata_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
}
Expand All @@ -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"
}
Expand All @@ -230,3 +230,53 @@ 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)
}

// 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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down
Loading