Skip to content

Commit b66a1c7

Browse files
aledbfclaude
andcommitted
fix(cli): report a failed container lookup in stop and down
stop and down dropped the error of the container lookup and of the inspect that follows it, and of os.Getwd. An engine that refused the list reported {"outcome":"success","result":"no-container-found"} with exit 0, and a container that could not be inspected was stopped as a plain container even when it belonged to a Compose project. Each now ends in an error result. stop and down are Go-only commands, so no reference behavior applies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ca1c3bb commit b66a1c7

2 files changed

Lines changed: 59 additions & 7 deletions

File tree

‎internal/cli/stop.go‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ func addStopFlags(cmd *cobra.Command, opts *stopOpts) {
7777

7878
func runStopOrDown(ctx context.Context, out Output, opts stopOpts, remove bool) error {
7979
if opts.workspaceFolder == "" {
80-
opts.workspaceFolder, _ = os.Getwd()
80+
wd, err := os.Getwd()
81+
if err != nil {
82+
return writeErrorResult(out, fmt.Sprintf("current directory: %v", err))
83+
}
84+
opts.workspaceFolder = wd
8185
}
8286

8387
logger := log.New(log.Options{Writer: out.Stderr(), Format: "text"})
@@ -87,13 +91,20 @@ func runStopOrDown(ctx context.Context, out Output, opts stopOpts, remove bool)
8791
}
8892
defer engine.Close()
8993

90-
id := resolveWorkspaceContainer(ctx, engine, opts)
94+
id, err := resolveWorkspaceContainer(ctx, engine, opts)
95+
if err != nil {
96+
return writeErrorResult(out, err.Error())
97+
}
9198
if id == "" {
9299
return writeSuccessJSON(out, map[string]any{"outcome": "success", "result": "no-container-found"})
93100
}
94101

102+
insp, err := engine.InspectContainer(ctx, id)
103+
if err != nil {
104+
return writeErrorResult(out, fmt.Sprintf("inspect container %s: %v", id, err))
105+
}
95106
project := ""
96-
if insp, ierr := engine.InspectContainer(ctx, id); ierr == nil && insp.Config != nil {
107+
if insp.Config != nil {
97108
project = insp.Config.Labels["com.docker.compose.project"]
98109
}
99110

@@ -129,14 +140,13 @@ func runStopOrDown(ctx context.Context, out Output, opts stopOpts, remove bool)
129140
})
130141
}
131142

132-
func resolveWorkspaceContainer(ctx context.Context, engine *docker.EngineClient, opts stopOpts) string {
143+
func resolveWorkspaceContainer(ctx context.Context, engine *docker.EngineClient, opts stopOpts) (string, error) {
133144
if opts.containerID != "" {
134-
return opts.containerID
145+
return opts.containerID, nil
135146
}
136147
configFile := ""
137148
if opts.configPath != "" {
138149
configFile = resolvePath(opts.configPath)
139150
}
140-
id, _ := resolveContainerID(ctx, engine, opts.workspaceFolder, configFile, opts.idLabels)
141-
return id
151+
return resolveContainerID(ctx, engine, opts.workspaceFolder, configFile, opts.idLabels)
142152
}

‎internal/cli/stop_held_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type fakeEngine struct {
2020
mu sync.Mutex
2121
containers map[string]map[string]string
2222
stopFails bool
23+
listFails bool
2324
calls []string
2425
}
2526

@@ -69,6 +70,11 @@ func (e *fakeEngine) serve(w http.ResponseWriter, r *http.Request) {
6970
}
7071
slices.Sort(want)
7172
e.record("list " + strings.Join(want, ","))
73+
if e.listFails {
74+
w.WriteHeader(http.StatusInternalServerError)
75+
fmt.Fprint(w, `{"message":"cannot list"}`)
76+
return
77+
}
7278
items := []map[string]string{}
7379
for id, labels := range e.containers {
7480
if hasLabels(labels, want) {
@@ -219,6 +225,42 @@ func TestStopWithNoContainerSucceedsWithoutDoingAnything(t *testing.T) {
219225
}
220226
}
221227

228+
func TestStopOrDownReportsALookupTheEngineRefused(t *testing.T) {
229+
engine := startFakeEngine(t, map[string]map[string]string{})
230+
engine.listFails = true
231+
cli, _ := fakeDockerCLI(t, false, 0)
232+
233+
got := runStopOrDownForTest(t, stopOpts{workspaceFolder: t.TempDir(), dockerPath: cli}, true)
234+
235+
assertResult(t, got, map[string]any{"outcome": "error"})
236+
if msg, _ := got["message"].(string); !strings.HasPrefix(msg, "Command failed: docker ps ") {
237+
t.Errorf("message = %q, want the failed container lookup", msg)
238+
}
239+
for _, c := range engine.recorded() {
240+
if !strings.HasPrefix(c, "list ") {
241+
t.Errorf("engine call %q after the lookup failed", c)
242+
}
243+
}
244+
}
245+
246+
func TestStopOrDownReportsAContainerItCannotInspect(t *testing.T) {
247+
engine := startFakeEngine(t, map[string]map[string]string{})
248+
cli, composeCalls := fakeDockerCLI(t, false, 0)
249+
250+
got := runStopOrDownForTest(t, stopOpts{containerID: "gone", dockerPath: cli}, false)
251+
252+
assertResult(t, got, map[string]any{"outcome": "error"})
253+
if msg, _ := got["message"].(string); !strings.HasPrefix(msg, "inspect container gone: ") {
254+
t.Errorf("message = %q, want the inspect failure", msg)
255+
}
256+
if calls := engine.recorded(); len(calls) != 0 {
257+
t.Errorf("engine calls = %v after the inspect failed", calls)
258+
}
259+
if calls := composeCalls(); calls != nil {
260+
t.Errorf("docker CLI ran %v", calls)
261+
}
262+
}
263+
222264
func TestDownStopsAndRemovesTheContainer(t *testing.T) {
223265
engine := startFakeEngine(t, map[string]map[string]string{"c1": {}})
224266
cli, _ := fakeDockerCLI(t, false, 0)

0 commit comments

Comments
 (0)