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
4 changes: 3 additions & 1 deletion cmd/config/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,14 +384,16 @@ func (cmd *ApplyCmd) copyAndExecFeatures(
) error {
featureSets, featureStageDir := stage.featureSets, stage.dir
containerFeaturesPath := "/tmp/build-features"
streams := docker.Streams{Stdout: os.Stdout, Stderr: os.Stderr}
streams := docker.Streams{LiveStdout: os.Stdout, LiveStderr: os.Stderr}
Comment thread
skevetter marked this conversation as resolved.
Resolved
if emitJSON {
// Raw Docker output must never share stdout with status and result
// envelopes. The configured logger preserves the selected machine
// encoding on stderr instead.
outputWriter := log.Writer(log.LevelInfo)
defer func() { _ = outputWriter.Close() }()
streams.LiveStdout = nil
streams.Stdout = outputWriter
streams.LiveStderr = nil
streams.Stderr = outputWriter
}

Expand Down
33 changes: 27 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ func exitCodeForError(err error, machineMode bool) int {
return exitcode.Success
}

if code, ok := unsafeOutputExitCode(err); ok {
return code
}
cliErr := clierr.Classify(err)

// Stay transparent for unclassified child-process exits (e.g. `devsy ssh -- cmd`).
Expand All @@ -269,7 +272,31 @@ func exitCodeForError(err error, machineMode bool) int {
return exitcode.Failure
}

func unsafeOutputExitCode(err error) (int, bool) {
unsafe, ok := errors.AsType[interface {
error
SkipFinalStatus() bool
}](err)
if !ok || !unsafe.SkipFinalStatus() {
return 0, false
}
if commandExit, ok := errors.AsType[*workspace.CommandExitError](err); ok {
return commandExit.Code, true
}
return exitcode.Failure, true
}

func commandExitCode(commandExit *workspace.CommandExitError, machineMode bool) int {
if machineMode || commandExit.Cause != nil {
renderCLIError(clierr.Classify(commandExit), machineMode)
}
return commandExit.Code
Comment thread
skevetter marked this conversation as resolved.
Resolved
}

func passthroughExitCode(err error, machineMode bool) (int, bool) {
if commandExit, ok := errors.AsType[*workspace.CommandExitError](err); ok {
return commandExitCode(commandExit, machineMode), true
}
if sshExitErr, ok := errors.AsType[*ssh.ExitError](err); ok {
if machineMode {
renderCLIError(
Expand All @@ -294,12 +321,6 @@ func passthroughExitCode(err error, machineMode bool) (int, bool) {
}
return execExitErr.ExitCode(), true
}
if commandExit, ok := errors.AsType[*workspace.CommandExitError](err); ok {
if machineMode {
renderCLIError(clierr.Classify(commandExit), true)
}
return commandExit.Code, true
}
return 0, false
}

Expand Down
42 changes: 42 additions & 0 deletions cmd/root_live_output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cmd

import (
"context"
"errors"
"io"
"os"
"testing"

"github.com/devsy-org/devsy/pkg/docker"
"github.com/devsy-org/devsy/pkg/workspace"
"github.com/stretchr/testify/suite"
)

type rootLiveOutputSuite struct{ suite.Suite }

type canceledUnsafeOutput struct{}

func (canceledUnsafeOutput) Error() string { return context.Canceled.Error() }
func (canceledUnsafeOutput) Unwrap() error { return context.Canceled }
func (canceledUnsafeOutput) SkipFinalStatus() bool { return true }

func TestRootLiveOutputSuite(t *testing.T) { suite.Run(t, new(rootLiveOutputSuite)) }

func (s *rootLiveOutputSuite) TestOutputErrorDoesNotWriteToStderr() {
reader, writer, err := os.Pipe()
s.Require().NoError(err)
defer func() { _ = reader.Close() }()
defer func() { _ = writer.Close() }()
original := os.Stderr
os.Stderr = writer
defer func() { os.Stderr = original }()
outputErr := &docker.LiveOutputError{Err: errors.New("live output: 12 bytes dropped")}
code := exitCodeForError(&workspace.CommandExitError{Code: 7, Cause: outputErr}, false)
s.Equal(7, code)
s.Equal(1, exitCodeForError(errors.Join(context.Canceled, outputErr), false))
s.Equal(1, exitCodeForError(canceledUnsafeOutput{}, false))
s.Require().NoError(writer.Close())
data, err := io.ReadAll(reader)
s.Require().NoError(err)
s.Empty(data)
}
23 changes: 17 additions & 6 deletions cmd/workspace/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workspace
import (
"context"
"fmt"
"math"
"os"
"strings"

Expand Down Expand Up @@ -353,16 +354,26 @@ func (cmd *ExecCmd) execInContainer(ctx context.Context, opts execOpts, args []s
stdoutIsTerminal := term.IsTerminal(
int(os.Stdout.Fd()),
) // #nosec G115 -- valid standard descriptor
stderrFD := os.Stderr.Fd()
stderrIsTerminal := stderrFD <= math.MaxInt && term.IsTerminal(int(stderrFD))
code, err := opts.runtime.Exec(ctx, workspace2.ExecRequest{
Target: opts.target, Workdir: opts.workdir, Env: opts.envMap, Argv: args,
Stdin: os.Stdin, Stdout: os.Stdout, Stderr: os.Stderr,
TTY: stdinIsTerminal && stdoutIsTerminal,
Target: opts.target,
Workdir: opts.workdir,
Env: opts.envMap,
Argv: args,
Stdin: os.Stdin,
Stdout: os.Stdout,
Stderr: os.Stderr,
TTY: stdinIsTerminal &&
stdoutIsTerminal,
LiveStdout: stdoutIsTerminal,
LiveStderr: stderrIsTerminal,
})
if err != nil {
if err != nil && code < 0 {
return err
}
if code != 0 {
return &workspace2.CommandExitError{Code: code}
if code != 0 || err != nil {
return &workspace2.CommandExitError{Code: code, Cause: err}
Comment thread
skevetter marked this conversation as resolved.
Resolved
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
}
return nil
}
58 changes: 58 additions & 0 deletions cmd/workspace/exec_live_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package workspace

import (
"context"
"errors"
"testing"
"time"

"github.com/devsy-org/devsy/cmd/flags"
devcconfig "github.com/devsy-org/devsy/pkg/devcontainer/config"
"github.com/devsy-org/devsy/pkg/docker"
"github.com/devsy-org/devsy/pkg/status"
workspace2 "github.com/devsy-org/devsy/pkg/workspace"
"github.com/stretchr/testify/suite"
)

type liveStatusSuite struct{ suite.Suite }

func TestLiveStatusSuite(t *testing.T) { suite.Run(t, new(liveStatusSuite)) }

type failedLiveRuntime struct{ workspace2.ContainerRuntime }

func (failedLiveRuntime) Exec(context.Context, workspace2.ExecRequest) (int, error) {
return 7, &docker.LiveOutputError{Err: errors.New("12 bytes dropped")}
}

type blockingFinalReporter struct {
calls int
release chan struct{}
}

func (r *blockingFinalReporter) Report(status.Event) {
r.calls++
if r.calls > 1 {
<-r.release
}
}

func (s *liveStatusSuite) TestOutputErrorDoesNotBlockFinalStatus() {
reporter := &blockingFinalReporter{release: make(chan struct{})}
defer close(reporter.release)
command := &ExecCmd{
GlobalFlags: &flags.GlobalFlags{ResultFormat: formatPlain},
statusReporter: reporter,
}
done := make(chan error, 1)
go func() {
opts := execOpts{runtime: failedLiveRuntime{}}
done <- command.execAndReport(context.Background(), opts, nil, devcconfig.ResultEnvelope{})
}()
select {
case err := <-done:
s.Require().ErrorContains(err, "12 bytes dropped")
s.Equal(1, reporter.calls)
case <-time.After(time.Second):
s.FailNow("final status did not return")
}
}
12 changes: 7 additions & 5 deletions cmd/workspace/exec_tty_unix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,28 @@ func TestExecInContainerRequestsTTYOnlyWhenInputAndOutputAreTerminals(t *testing
runtime := &execTTYCaptureRuntime{}
target := workspace2.ContainerTarget{ContainerID: "container-id", User: "1001:1002"}
env := map[string]string{"LANG": "C.UTF-8"}
originalStdin, originalStdout := os.Stdin, os.Stdout
os.Stdin, os.Stdout = stdin, stdout
originalStdin, originalStdout, originalStderr := os.Stdin, os.Stdout, os.Stderr
os.Stdin, os.Stdout, os.Stderr = stdin, stdout, stdin
t.Cleanup(func() {
os.Stdin, os.Stdout = originalStdin, originalStdout
os.Stdin, os.Stdout, os.Stderr = originalStdin, originalStdout, originalStderr
})
err := (&ExecCmd{}).execInContainer(
t.Context(),
execOpts{runtime: runtime, target: target, workdir: "/workspace", envMap: env},
[]string{"printf", "hello"},
)
os.Stdin, os.Stdout = originalStdin, originalStdout
os.Stdin, os.Stdout, os.Stderr = originalStdin, originalStdout, originalStderr
require.NoError(t, err)
require.Equal(t, test.wantTTY, runtime.request.TTY)
require.Equal(t, test.stdoutTTY, runtime.request.LiveStdout)
require.Equal(t, test.stdinTTY, runtime.request.LiveStderr)
require.Equal(t, target, runtime.request.Target)
require.Equal(t, "/workspace", runtime.request.Workdir)
require.Equal(t, env, runtime.request.Env)
require.Equal(t, []string{"printf", "hello"}, runtime.request.Argv)
require.Same(t, stdin, runtime.request.Stdin)
require.Same(t, stdout, runtime.request.Stdout)
require.Same(t, os.Stderr, runtime.request.Stderr)
require.Same(t, stdin, runtime.request.Stderr)
})
}
}
Expand Down
30 changes: 19 additions & 11 deletions cmd/workspace/lifecycle.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ type LifecycleExecParams struct {

// EnvArgsRedactor masks sensitive values passed to docker exec as -e entries.
func EnvArgsRedactor(envArgs []string) *secrets.Redactor {
return secrets.NewEnvironmentRedactor(envArgsEntries(envArgs))
}

func envArgsEntries(envArgs []string) []string {
entries := make([]string, 0, len(envArgs)/2)
for _, arg := range envArgs {
if arg != "-e" {
entries = append(entries, arg)
}
}

return secrets.NewEnvironmentRedactor(entries)
return entries
}

// ExecLifecycleHook runs a single devcontainer lifecycle hook against a container.
Expand All @@ -104,9 +108,12 @@ func ExecLifecycleHook(params *LifecycleExecParams, name string, hook types.Life
rec := newLifecycleRecording(params, name, key)
started := time.Now()
err := params.Helper.Run(params.Ctx, args, docker.Streams{
Stdin: os.Stdin,
Stdout: rec.tee(os.Stdout, workspacelogs.StreamStdout),
Stderr: rec.tee(os.Stderr, workspacelogs.StreamStderr),
Stdin: os.Stdin,
Stdout: rec.capture(workspacelogs.StreamStdout),
LiveStdout: os.Stdout,
Stderr: rec.capture(workspacelogs.StreamStderr),
LiveStderr: os.Stderr,
RedactionEnv: envArgsEntries(params.EnvArgs),
})
rec.finish(params.Ctx, time.Since(started), err)
if err != nil {
Expand Down Expand Up @@ -144,16 +151,14 @@ func newLifecycleRecording(params *LifecycleExecParams, hook, key string) *lifec
return r
}

// tee keeps dst as the only functional destination when nothing is recorded.
func (r *lifecycleRecording) tee(dst io.Writer, stream workspacelogs.Stream) io.Writer {
func (r *lifecycleRecording) capture(stream workspacelogs.Stream) io.Writer {
if r == nil {
return dst
return nil
}
if stream == workspacelogs.StreamStderr {
return io.MultiWriter(dst, r.stderr)
return r.stderr
}

return io.MultiWriter(dst, r.stdout)
return r.stdout
}

func (r *lifecycleRecording) record(state workspacelogs.InvocationState, reason string) {
Expand All @@ -180,7 +185,7 @@ func (r *lifecycleRecording) finish(ctx context.Context, elapsed time.Duration,
code := exitErr.ExitCode()
inv.ExitCode = &code
}
} else if runErr == nil {
} else if _, outputOnly := runErr.(*docker.LiveOutputError); runErr == nil || outputOnly {
code := 0
inv.ExitCode = &code
}
Expand All @@ -194,6 +199,9 @@ func lifecycleOutcome(ctx context.Context, runErr error) (workspacelogs.Invocati
case ctx != nil && ctx.Err() != nil:
return workspacelogs.InvocationCanceled, ""
}
if _, ok := errors.AsType[*docker.LiveOutputError](runErr); ok {
return workspacelogs.InvocationFailed, "output_failed"
}
if _, ok := errors.AsType[*exec.ExitError](runErr); !ok {
return workspacelogs.InvocationFailed, "start_failed"
}
Expand Down
51 changes: 51 additions & 0 deletions cmd/workspace/lifecycle_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ package workspace

import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"testing"
"time"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/devsy-org/devsy/pkg/workspacelogs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

const fakeDockerScript = `#!/bin/sh
Expand Down Expand Up @@ -204,3 +207,51 @@ func TestExecLifecycleHookWithoutRecorderIsUnchanged(t *testing.T) {
require.Error(t, err)
assert.NotContains(t, err.Error(), "full output")
}

type lifecycleOutputSuite struct{ suite.Suite }

func TestLifecycleOutputSuite(t *testing.T) { suite.Run(t, new(lifecycleOutputSuite)) }

func (s *lifecycleOutputSuite) TestSuccessfulCommandWithLostOutput() {
run := newRecordedLifecycle(s.T())
rec := newLifecycleRecording(execParams(s.T(), run.rec), "postCreateCommand", sourceSetup)
rec.finish(
context.Background(),
time.Second,
&docker.LiveOutputError{Err: errors.New("bytes dropped")},
)
inv := lastInvocation(run.records(s.T()))
s.Require().NotNil(inv)
s.Equal(workspacelogs.InvocationFailed, inv.State)
s.Equal("output_failed", inv.Reason)
s.Require().NotNil(inv.ExitCode)
s.Zero(*inv.ExitCode)
}

func (s *lifecycleOutputSuite) TestFileLiveOutputPreservesRawHookOutput() {
stdoutPath, _ := redirectStdio(s.T())
params := execParams(s.T(), nil)
params.EnvArgs = []string{"-e", "API_TOKEN=fake-out"}
s.Require().
NoError(ExecLifecycleHook(params, "postCreateCommand", types.LifecycleHook{sourceSetup: {"echo hi"}}))
s.Equal("fake-out\n", readFile(s.T(), stdoutPath))
}

func (s *lifecycleOutputSuite) TestFailedCommandWithLostOutput() {
run := newRecordedLifecycle(s.T())
rec := newLifecycleRecording(execParams(s.T(), run.rec), "postCreateCommand", sourceSetup)
cmd := exec.Command("sh", "-c", "exit 7")
exitErr := cmd.Run()
s.Require().Error(exitErr)
rec.finish(
context.Background(),
time.Second,
errors.Join(exitErr, &docker.LiveOutputError{Err: errors.New("bytes dropped")}),
)
inv := lastInvocation(run.records(s.T()))
s.Require().NotNil(inv)
s.Equal(workspacelogs.InvocationFailed, inv.State)
s.Equal("output_failed", inv.Reason)
s.Require().NotNil(inv.ExitCode)
s.Equal(7, *inv.ExitCode)
}
Loading
Loading