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: 14 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,20 @@ mise installs the pinned `protoc`. The task installs the Go protobuf plugins.

## Test your changes

### Go resource lifecycle checks

Run affected packages with `go test -race -short -count=1`, then run
`mise run cli:lint:ci`. Resource cleanup changes need coverage of failure paths
as well as successful calls.

The browser tunnel regression in `pkg/ide/opener` injects a failing `/dev/null`
open on Linux and macOS. It checks that a failed helper launch releases its
reserved port. Platform reachability tests in `cmd/pro` use local HTTP servers
to check connection cleanup between probes.

Cross-compilation checks platform buildability, not runtime behavior. Windows
PTY tests run through the `windows-pty` step in `hack/ci/suites.yaml`.

### Quick start

1. Build Devsy:
Expand Down
35 changes: 10 additions & 25 deletions cmd/config/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,7 @@ func (cmd *ReadCmd) resolveConfigFromContainer(ctx context.Context) (
return nil, "", fmt.Errorf("container %s not found", cmd.ContainerID)
}

containerDetails := &details[0]
subCtx := &devcconfig.SubstitutionContext{}

imageMetadata, err := metadata.GetImageMetadataFromContainer(
containerDetails,
subCtx,
)
if err != nil {
return nil, "", fmt.Errorf("get image metadata from container: %w", err)
}

parsedConfig := &devcconfig.DevContainerConfig{}
if len(imageMetadata.Config) > 0 {
last := imageMetadata.Config[len(imageMetadata.Config)-1]
parsedConfig.DevContainerConfigBase = last.DevContainerConfigBase
parsedConfig.DevContainerActions = last.DevContainerActions
parsedConfig.NonComposeBase = last.NonComposeBase
}

workspaceFolder := containerDetails.Config.WorkingDir
if workspaceFolder == "" {
workspaceFolder = "/"
}

return parsedConfig, workspaceFolder, nil
return configFromContainerDetails(&details[0])
}

func (cmd *ReadCmd) resolveConfigFromIDLabels(ctx context.Context) (
Expand All @@ -234,7 +210,16 @@ func (cmd *ReadCmd) resolveConfigFromIDLabels(ctx context.Context) (
return nil, "", err
}

return configFromContainerDetails(containerDetails)
}

func configFromContainerDetails(containerDetails *devcconfig.ContainerDetails) (
*devcconfig.DevContainerConfig,
string,
error,
) {
subCtx := &devcconfig.SubstitutionContext{}

imageMetadata, err := metadata.GetImageMetadataFromContainer(
containerDetails,
subCtx,
Expand Down
18 changes: 12 additions & 6 deletions cmd/internal/agent_daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -499,13 +499,20 @@ func (cmd *DaemonCmd) shutdownWorkspace(
func (cmd *DaemonCmd) effectiveShutdownAction(
workspace *provider2.AgentWorkspaceInfo,
) string {
if action, ok := workspaceShutdownAction(workspace); ok {
return action
}
return cmd.ShutdownAction
}

func workspaceShutdownAction(workspace *provider2.AgentWorkspaceInfo) (string, bool) {
if workspace != nil &&
workspace.LastDevContainerConfig != nil &&
workspace.LastDevContainerConfig.Config != nil &&
workspace.LastDevContainerConfig.Config.ShutdownAction != "" {
return workspace.LastDevContainerConfig.Config.ShutdownAction
return workspace.LastDevContainerConfig.Config.ShutdownAction, true
}
return cmd.ShutdownAction
return "", false
}

func (cmd *DaemonCmd) runShutdownCommand(
Expand Down Expand Up @@ -772,10 +779,9 @@ func blockedWorkspaceResult(
}

func shutdownActionEnabled(workspace *provider2.AgentWorkspaceInfo, fallback string) bool {
action := fallback
if workspace.LastDevContainerConfig != nil && workspace.LastDevContainerConfig.Config != nil &&
workspace.LastDevContainerConfig.Config.ShutdownAction != "" {
action = workspace.LastDevContainerConfig.Config.ShutdownAction
action, ok := workspaceShutdownAction(workspace)
if !ok {
action = fallback
}
return len(workspace.Agent.Exec.Shutdown) > 0 && action != config.ShutdownActionNone
}
Expand Down
45 changes: 45 additions & 0 deletions cmd/internal/agent_daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,51 @@ func TestEffectiveShutdownAction(t *testing.T) {
})
}

func TestShutdownActionEnabled(t *testing.T) {
tests := []struct {
name string
action string
fallback string
command types.StrArray
want bool
}{
{
name: "workspace none overrides fallback",
action: config.ShutdownActionNone,
fallback: config.ShutdownActionStopContainer,
command: types.StrArray{testEcho},
},
{
name: "workspace action overrides none",
action: config.ShutdownActionStopContainer,
fallback: config.ShutdownActionNone,
command: types.StrArray{testEcho},
want: true,
},
{
name: "empty action uses fallback",
fallback: config.ShutdownActionStopContainer,
command: types.StrArray{testEcho},
want: true,
},
{name: "empty fallback is enabled", command: types.StrArray{testEcho}, want: true},
{name: "command required", action: config.ShutdownActionStopContainer},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ws := workspaceWithShutdownAction(tt.action)
ws.Agent.Exec.Shutdown = tt.command
assert.Equal(t, tt.want, shutdownActionEnabled(ws, tt.fallback))
})
}
t.Run("empty configured action uses fallback", func(t *testing.T) {
ws := workspaceWithShutdownAction(config.ShutdownActionNone)
ws.LastDevContainerConfig.Config.ShutdownAction = ""
ws.Agent.Exec.Shutdown = types.StrArray{testEcho}
assert.True(t, shutdownActionEnabled(ws, config.ShutdownActionStopContainer))
})
}

func TestEffectiveActivity(t *testing.T) {
orig := activityFilePath
t.Cleanup(func() { activityFilePath = orig })
Expand Down
52 changes: 26 additions & 26 deletions cmd/internal/runusercommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,27 +487,33 @@ func (cmd *RunUserCommandsCmd) runLifecycleHooks(
})
}

type lifecycleHook struct {
name string
cmds []types.LifecycleHook
skip bool
}

func (cmd *RunUserCommandsCmd) runLifecycleHooksUnscoped(
params *workspace.LifecycleExecParams,
result *devcconfig.Result,
) error {
hooks := []struct {
name string
cmds []types.LifecycleHook
skip bool
}{
hooks := []lifecycleHook{
{"onCreateCommand", result.MergedConfig.OnCreateCommands, cmd.SkipOnCreate},
{updateContentCommand, result.MergedConfig.UpdateContentCommands, cmd.SkipUpdateContent},
{"postCreateCommand", result.MergedConfig.PostCreateCommands, cmd.SkipPostCreate},
{"postStartCommand", result.MergedConfig.PostStartCommands, cmd.SkipPostStart},
{"postAttachCommand", result.MergedConfig.PostAttachCommands, cmd.SkipPostAttach},
}

waitForBoundary := resolveWaitForBoundary(result)
waitForBoundary := resolveWaitForBoundary(hooks, result)
prebuildBoundary := 0
if cmd.Prebuild {
prebuildBoundary = resolveWaitForBoundary(hooks, nil)
}
boundaryName := hooks[waitForBoundary].name

for i, hook := range hooks {
if cmd.shouldStopLifecycle(i, waitForBoundary, boundaryName) {
if cmd.shouldStopLifecycle(i, prebuildBoundary, waitForBoundary, boundaryName) {
return nil
}
if hook.skip {
Expand All @@ -522,10 +528,10 @@ func (cmd *RunUserCommandsCmd) runLifecycleHooksUnscoped(
}

func (cmd *RunUserCommandsCmd) shouldStopLifecycle(
i, waitForBoundary int,
i, prebuildBoundary, waitForBoundary int,
boundaryName string,
) bool {
if cmd.Prebuild && i >= 2 {
if cmd.Prebuild && i > prebuildBoundary {
log.Debugf(
"stopping lifecycle execution (%s: after %s)",
names.Flag(names.Prebuild),
Expand Down Expand Up @@ -558,27 +564,21 @@ func execLifecycleHooks(
return nil
}

func resolveWaitForBoundary(result *devcconfig.Result) int {
if result == nil || result.MergedConfig == nil {
return 1
func resolveWaitForBoundary(hooks []lifecycleHook, result *devcconfig.Result) int {
waitFor := updateContentCommand
if result != nil && result.MergedConfig != nil && result.MergedConfig.WaitFor != "" {
waitFor = result.MergedConfig.WaitFor
}
hookNames := []string{
"onCreateCommand",
updateContentCommand,
"postCreateCommand",
"postStartCommand",
"postAttachCommand",
}
waitFor := result.MergedConfig.WaitFor
if waitFor == "" {
waitFor = updateContentCommand
}
for i, name := range hookNames {
if name == waitFor {
fallback := 0
for i, hook := range hooks {
if hook.name == updateContentCommand {
fallback = i
}
if hook.name == waitFor {
return i
}
}
return 1
return fallback
}

// runRecording is the fresh run opened for one run-user-commands invocation.
Expand Down
23 changes: 21 additions & 2 deletions cmd/internal/runusercommands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ func TestRunUserCommandsCmd_Validate(t *testing.T) {
}

func TestResolveWaitForBoundary(t *testing.T) {
hooks := []lifecycleHook{
{name: hookOnCreate},
{name: hookUpdateContent},
{name: hookPostCreate},
{name: hookPostStart},
{name: hookPostAttach},
}
tests := []struct {
name string
waitFor string
Expand All @@ -340,14 +347,26 @@ func TestResolveWaitForBoundary(t *testing.T) {
},
},
}
got := resolveWaitForBoundary(result)
got := resolveWaitForBoundary(hooks, result)
assert.Equal(t, tt.want, got)
})
}
}

func TestResolveWaitForBoundary_NilResult(t *testing.T) {
assert.Equal(t, 1, resolveWaitForBoundary(nil))
hooks := []lifecycleHook{{name: hookOnCreate}, {name: hookUpdateContent}}
assert.Equal(t, 1, resolveWaitForBoundary(hooks, nil))
assert.Equal(t, 1, resolveWaitForBoundary(hooks, &devcconfig.Result{}))
}

func TestResolveWaitForBoundary_UsesHookOrder(t *testing.T) {
hooks := []lifecycleHook{{name: hookUpdateContent}, {name: hookOnCreate}}
result := &devcconfig.Result{MergedConfig: &devcconfig.MergedDevContainerConfig{
DevContainerConfigBase: devcconfig.DevContainerConfigBase{WaitFor: hookOnCreate},
}}
assert.Equal(t, 1, resolveWaitForBoundary(hooks, result))
result.MergedConfig.WaitFor = "unknownHook"
assert.Equal(t, 0, resolveWaitForBoundary(hooks, result))
}

func TestBuildLifecycleEnvArgs_Nil(t *testing.T) {
Expand Down
Loading
Loading