Skip to content

Commit 51db7a8

Browse files
aledbfclaude
andcommitted
fix(lifecycle): log initializeCommand output instead of printing it
initializeCommand's stdout went straight to the CLI's stdout, ahead of up's JSON result, and its stderr to the CLI's stderr outside the log. TS runInitializeCommand writes every chunk of the command's output to the log with output.raw at info level; do the same, so stdout holds only the result and --log-format json carries the output as raw events. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent b1188b9 commit 51db7a8

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎internal/lifecycle/initialize.go‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"fmt"
66
"maps"
7-
"os"
87
"os/exec"
98
"slices"
109
"strings"
@@ -44,7 +43,7 @@ func RunInitializeCommand(ctx context.Context, logger log.Logger, cmd *config.Li
4443
return err
4544
}
4645
logger.Write(fmt.Sprintf("Running initializeCommand: %s", resolved), log.LevelInfo)
47-
return runHostExec(ctx, []string{"/bin/sh", "-c", resolved}, workspaceFolder)
46+
return runHostExec(ctx, logger, []string{"/bin/sh", "-c", resolved}, workspaceFolder)
4847
}
4948

5049
// Array form
@@ -57,7 +56,7 @@ func RunInitializeCommand(ctx context.Context, logger log.Logger, cmd *config.Li
5756
arr[i] = resolved
5857
}
5958
logger.Write(fmt.Sprintf("Running initializeCommand: %s", strings.Join(arr, " ")), log.LevelInfo)
60-
return runHostExec(ctx, arr, workspaceFolder)
59+
return runHostExec(ctx, logger, arr, workspaceFolder)
6160
}
6261

6362
// Object form (parallel commands)
@@ -80,7 +79,7 @@ func RunInitializeCommand(ctx context.Context, logger log.Logger, cmd *config.Li
8079
return
8180
}
8281
logger.Write(fmt.Sprintf(" [%s] %s", name, resolved), log.LevelInfo)
83-
if err := runHostExec(ctx, []string{"/bin/sh", "-c", resolved}, workspaceFolder); err != nil {
82+
if err := runHostExec(ctx, logger, []string{"/bin/sh", "-c", resolved}, workspaceFolder); err != nil {
8483
errs[i] = fmt.Errorf("%s: %w", name, err)
8584
}
8685
})
@@ -97,10 +96,22 @@ func RunInitializeCommand(ctx context.Context, logger log.Logger, cmd *config.Li
9796
return nil
9897
}
9998

100-
func runHostExec(ctx context.Context, args []string, workDir string) error {
99+
// runHostExec runs args in workDir and streams the command's stdout and stderr
100+
// into the log, as TS runInitializeCommand does (runCommand writes each chunk
101+
// with output.raw at info level): the CLI's stdout is left to its JSON result.
102+
func runHostExec(ctx context.Context, logger log.Logger, args []string, workDir string) error {
101103
cmd := exec.CommandContext(ctx, args[0], args[1:]...)
102104
cmd.Dir = workDir
103-
cmd.Stdout = os.Stdout
104-
cmd.Stderr = os.Stderr
105+
out := rawLog{logger}
106+
cmd.Stdout = out
107+
cmd.Stderr = out
105108
return cmd.Run()
106109
}
110+
111+
// rawLog writes what it is given into a log as raw text at info level.
112+
type rawLog struct{ logger log.Logger }
113+
114+
func (w rawLog) Write(p []byte) (int, error) {
115+
w.logger.Raw(string(p), log.LevelInfo)
116+
return len(p), nil
117+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package lifecycle
2+
3+
import (
4+
"context"
5+
"path/filepath"
6+
"strings"
7+
"sync"
8+
"testing"
9+
10+
"github.com/devcontainers/cli/internal/config"
11+
"github.com/devcontainers/cli/internal/log"
12+
)
13+
14+
// rawCapture records the raw text written to a log.
15+
type rawCapture struct {
16+
log.Logger
17+
mu sync.Mutex
18+
raw strings.Builder
19+
}
20+
21+
func (c *rawCapture) Raw(text string, _ ...log.Level) {
22+
c.mu.Lock()
23+
defer c.mu.Unlock()
24+
c.raw.WriteString(text)
25+
}
26+
27+
func (c *rawCapture) Write(string, ...log.Level) {}
28+
29+
// initializeCommand runs in the workspace folder, and its stdout and stderr go
30+
// to the log, never to the CLI's stdout, which holds only the JSON result: TS
31+
// runInitializeCommand runs it through runCommand with the CLI host's cwd and
32+
// writes every chunk with output.raw.
33+
func TestRunInitializeCommandLogsItsOutputFromTheWorkspace(t *testing.T) {
34+
ws, err := filepath.EvalSymlinks(t.TempDir())
35+
if err != nil {
36+
t.Fatal(err)
37+
}
38+
logger := &rawCapture{Logger: log.Null}
39+
cmd := cmdFromJSON(t, `"pwd -P; echo to-stderr >&2"`)
40+
if err := RunInitializeCommand(context.Background(), logger, cmd, config.HostSubContext{Platform: "linux", LocalWorkspaceFolder: ws}); err != nil {
41+
t.Fatal(err)
42+
}
43+
got := logger.raw.String()
44+
if !strings.Contains(got, ws+"\n") || !strings.Contains(got, "to-stderr\n") {
45+
t.Errorf("logged %q, want the workspace folder %s from stdout and to-stderr from stderr", got, ws)
46+
}
47+
}

0 commit comments

Comments
 (0)