Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8ee2668

Browse files
authored
fix(agent): fix script filtering for devcontainers (#18635)
1 parent 59a6541 commit 8ee2668

File tree

2 files changed

+20
-23
lines changed

2 files changed

+20
-23
lines changed

agent/agent.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1158,15 +1158,13 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11581158
}
11591159
}
11601160

1161-
var (
1162-
scripts = manifest.Scripts
1163-
scriptRunnerOpts []agentscripts.InitOption
1164-
devcontainerScripts map[uuid.UUID]codersdk.WorkspaceAgentScript
1165-
)
1161+
scripts := manifest.Scripts
11661162
if a.containerAPI != nil {
1167-
scripts, devcontainerScripts = agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, scripts)
1163+
// Since devcontainer are enabled, remove devcontainer scripts
1164+
// from the main scripts list to avoid showing an error.
1165+
scripts, _ = agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, manifest.Scripts)
11681166
}
1169-
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted, scriptRunnerOpts...)
1167+
err = a.scriptRunner.Init(scripts, aAPI.ScriptCompleted)
11701168
if err != nil {
11711169
return xerrors.Errorf("init script runner: %w", err)
11721170
}
@@ -1187,10 +1185,11 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
11871185
if a.containerAPI != nil {
11881186
a.containerAPI.Init(
11891187
agentcontainers.WithManifestInfo(manifest.OwnerName, manifest.WorkspaceName, manifest.AgentName),
1190-
agentcontainers.WithDevcontainers(manifest.Devcontainers, scripts),
1188+
agentcontainers.WithDevcontainers(manifest.Devcontainers, manifest.Scripts),
11911189
agentcontainers.WithSubAgentClient(agentcontainers.NewSubAgentClientFromAPI(a.logger, aAPI)),
11921190
)
11931191

1192+
_, devcontainerScripts := agentcontainers.ExtractDevcontainerScripts(manifest.Devcontainers, manifest.Scripts)
11941193
for _, dc := range manifest.Devcontainers {
11951194
cErr := a.createDevcontainer(ctx, aAPI, dc, devcontainerScripts[dc.ID])
11961195
err = errors.Join(err, cErr)

agent/agentcontainers/api.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,14 @@ type API struct {
7979
containersErr error // Error from the last list operation.
8080
devcontainerNames map[string]bool // By devcontainer name.
8181
knownDevcontainers map[string]codersdk.WorkspaceAgentDevcontainer // By workspace folder.
82+
devcontainerLogSourceIDs map[string]uuid.UUID // By workspace folder.
8283
configFileModifiedTimes map[string]time.Time // By config file path.
8384
recreateSuccessTimes map[string]time.Time // By workspace folder.
8485
recreateErrorTimes map[string]time.Time // By workspace folder.
8586
injectedSubAgentProcs map[string]subAgentProcess // By workspace folder.
8687
usingWorkspaceFolderName map[string]bool // By workspace folder.
8788
ignoredDevcontainers map[string]bool // By workspace folder. Tracks three states (true, false and not checked).
8889
asyncWg sync.WaitGroup
89-
90-
devcontainerLogSourceIDs map[string]uuid.UUID // By workspace folder.
9190
}
9291

9392
type subAgentProcess struct {
@@ -935,12 +934,7 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
935934
return xerrors.Errorf("devcontainer not found")
936935
}
937936

938-
api.asyncWg.Add(1)
939-
defer api.asyncWg.Done()
940-
api.mu.Unlock()
941-
942937
var (
943-
err error
944938
ctx = api.ctx
945939
logger = api.logger.With(
946940
slog.F("devcontainer_id", dc.ID),
@@ -950,19 +944,23 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
950944
)
951945
)
952946

953-
if dc.ConfigPath != configPath {
954-
logger.Warn(ctx, "devcontainer config path mismatch",
955-
slog.F("config_path_param", configPath),
956-
)
957-
}
958-
959947
// Send logs via agent logging facilities.
960948
logSourceID := api.devcontainerLogSourceIDs[dc.WorkspaceFolder]
961949
if logSourceID == uuid.Nil {
962-
// Fallback to the external log source ID if not found.
950+
api.logger.Debug(api.ctx, "devcontainer log source ID not found, falling back to external log source ID")
963951
logSourceID = agentsdk.ExternalLogSourceID
964952
}
965953

954+
api.asyncWg.Add(1)
955+
defer api.asyncWg.Done()
956+
api.mu.Unlock()
957+
958+
if dc.ConfigPath != configPath {
959+
logger.Warn(ctx, "devcontainer config path mismatch",
960+
slog.F("config_path_param", configPath),
961+
)
962+
}
963+
966964
scriptLogger := api.scriptLogger(logSourceID)
967965
defer func() {
968966
flushCtx, cancel := context.WithTimeout(api.ctx, 5*time.Second)
@@ -981,7 +979,7 @@ func (api *API) CreateDevcontainer(workspaceFolder, configPath string, opts ...D
981979
upOptions := []DevcontainerCLIUpOptions{WithUpOutput(infoW, errW)}
982980
upOptions = append(upOptions, opts...)
983981

984-
_, err = api.dccli.Up(ctx, dc.WorkspaceFolder, configPath, upOptions...)
982+
_, err := api.dccli.Up(ctx, dc.WorkspaceFolder, configPath, upOptions...)
985983
if err != nil {
986984
// No need to log if the API is closing (context canceled), as this
987985
// is expected behavior when the API is shutting down.

0 commit comments

Comments
 (0)