Skip to content

Commit 7e99fb7

Browse files
authored
fix(agent): delay containerAPI init to ensure startup scripts run before (#18630)
1 parent 3cb9b20 commit 7e99fb7

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
lines changed

agent/agent.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,13 +1163,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11631163
scriptRunnerOpts []agentscripts.InitOption
11641164
devcontainerScripts map[uuid.UUID]codersdk.WorkspaceAgentScript
11651165
)
1166-
if a.devcontainers {
1167-
a.containerAPI.Init(
1168-
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
1169-
agentcontainers.WithDevcontainers(manifest.Devcontainers, scripts),
1170-
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
1171-
)
1172-
1166+
if a.containerAPI != nil {
11731167
scripts, devcontainerScripts = agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, scripts)
11741168
}
11751169
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted, scriptRunnerOpts...)
@@ -1190,9 +1184,17 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11901184
// autostarted devcontainer will be included in this time.
11911185
err := a.scriptRunner.Execute(a.gracefulCtx, agentscripts.ExecuteStartScripts)
11921186

1193-
for _, dc := range manifest.Devcontainers {
1194-
cErr := a.createDevcontainer(ctx, aAPI, dc, devcontainerScripts[dc.ID])
1195-
err = errors.Join(err, cErr)
1187+
if a.containerAPI != nil {
1188+
a.containerAPI.Init(
1189+
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
1190+
agentcontainers.WithDevcontainers(manifest.Devcontainers, scripts),
1191+
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
1192+
)
1193+
1194+
for _, dc := range manifest.Devcontainers {
1195+
cErr := a.createDevcontainer(ctx, aAPI, dc, devcontainerScripts[dc.ID])
1196+
err = errors.Join(err, cErr)
1197+
}
11961198
}
11971199

11981200
dur := time.Since(start).Seconds()

agent/agentcontainers/api.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,6 @@ func (api *API) updateContainers(ctx context.Context) error {
530530
// will clear up on the next update.
531531
if !errors.Is(err, context.Canceled) {
532532
api.mu.Lock()
533-
api.containers = codersdk.WorkspaceAgentListContainersResponse{}
534533
api.containersErr = err
535534
api.mu.Unlock()
536535
}
@@ -947,7 +946,7 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
947946
slog.F("devcontainer_id", dc.ID),
948947
slog.F("devcontainer_name", dc.Name),
949948
slog.F("workspace_folder", dc.WorkspaceFolder),
950-
slog.F("config_path", configPath),
949+
slog.F("config_path", dc.ConfigPath),
951950
)
952951
)
953952

agent/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (a *agent) apiHandler() http.Handler {
3636
cacheDuration: cacheDuration,
3737
}
3838

39-
if a.devcontainers {
39+
if a.containerAPI != nil {
4040
r.Mount("/api/v0/containers", a.containerAPI.Routes())
4141
} else {
4242
r.HandleFunc("/api/v0/containers", func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)