fix(observed_state): skip the hook-container query under ScopeStart

Second Copilot review finding in the same vein as the previous
network/volume fix: collectObservedState's mergeHookContainers call
(getHookContainers, an extra ContainerList with a hook-specific label
filter) is consumed exclusively by create-phase runner reconciliation
(planPreStartHookRunners, reconcile.go) -- the start phase's own
RunPreStart never reads observed.HookContainers at all; it executes a
runner the create phase already prepared, or fails outright if none
exists. Performing this query unconditionally meant a no-op
`compose start` on a service declaring pre_start hooks could fail on
a daemon call the old imperative engine never made.

Gated behind the same scope.plansCreatePhase() check as the network/
volume listing.

New regression test TestCollectObservedState_ScopeStartSkipsHookContainerQuery
registers exactly one ContainerList expectation (the default Times(1)):
gomock fails the test if the extra hook-container listing fires under
ScopeStart. Verified by ablation (reverting the guard reproduces the
failure deterministically, 3/3 runs).

Verified: go build, go test ./pkg/compose/..., and golangci-lint all
clean; full e2e suite re-run clean against a locally built binary
(same 3 pre-existing, environment-specific failures as the stack's
baseline: TestWatch, TestIPC, TestComposeModel).

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-10-07 19:08:36 +02:00 • committed by Guillaume Lours
parent 56a751b3ad
commit d19b86b9e5
2 changed files with 38 additions and 3 deletions

View file

@ -210,9 +210,20 @@ func (s *composeService) collectObservedState(ctx context.Context, project *type
if err != nil {
return nil, err
}
raw, err = s.mergeHookContainers(ctx, project, raw)
if err != nil {
return nil, err
// Hook-runner snapshots are consumed exclusively by create-phase runner
// reconciliation (planPreStartHookRunners, reconcile.go) -- the start
// phase's own RunPreStart never reads observed.HookContainers, it
// executes a runner the create phase already prepared or fails outright
// if none exists. Skipping this for ScopeStart matches the
// network/volume guard below: one more unconditional daemon call the old
// imperative engine never made, which could otherwise fail a no-op
// `compose start` on a service with pre_start hooks declared (second
// Copilot review finding in the same vein as the first).
if scope.plansCreatePhase() {
raw, err = s.mergeHookContainers(ctx, project, raw)
if err != nil {
return nil, err
}
}
knownServices := map[string]bool{}

View file

@ -295,6 +295,30 @@ func TestCollectObservedState_ScopeStartSkipsNetworksAndVolumes(t *testing.T) {
assert.Equal(t, len(state.Volumes), 0)
}
// TestCollectObservedState_ScopeStartSkipsHookContainerQuery is a regression
// test for a second Copilot review finding in the same vein: hook-runner
// snapshots (mergeHookContainers/getHookContainers) are consumed exclusively
// by create-phase runner reconciliation (planPreStartHookRunners) -- the
// start phase's own RunPreStart never reads observed.HookContainers. Only
// one ContainerList expectation is registered (the default Times(1)):
// gomock fails the test if collectObservedState issues the extra
// getHookContainers listing under ScopeStart for a service declaring
// pre_start hooks.
func TestCollectObservedState_ScopeStartSkipsHookContainerQuery(t *testing.T) {
svc, apiClient := newTestService(t)
project := &types.Project{
Name: "myproject",
Services: types.Services{
"web": {Name: "web", PreStart: []types.PreStartHook{{}}},
},
}
apiClient.EXPECT().ContainerList(gomock.Any(), gomock.Any()).Return(client.ContainerListResult{}, nil)
state, err := svc.collectObservedState(t.Context(), project, ScopeStart)
assert.NilError(t, err)
assert.Equal(t, len(state.HookContainers["web"]), 0)
}
// TestCollectObservedState_LegacyHookRunnerNotDuplicated covers a runner
// created before the ConfigHashLabel exclusion existed (or by an older
// compose version): it still carries the label, so it matches getContainers'