Skip to content

fix(agent): fix unexpanded devcontainer paths for agentcontainers #17736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
fix(agent): fix unexpanded devcontainer paths for agentcontainers
Updates #16424
  • Loading branch information
mafredri committed May 9, 2025
commit 82d5bae4d41c3edcc9e183ff33bf5d175844c144
4 changes: 3 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,8 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
if err != nil {
return xerrors.Errorf("expand directory: %w", err)
}
// Normalize all devcontainer paths by making them absolute.
manifest.Devcontainers = agentcontainers.ExpandAllDevcontainerPaths(a.logger, expandPathToAbs, manifest.Devcontainers)
subsys, err := agentsdk.ProtoFromSubsystems(a.subsystems)
if err != nil {
a.logger.Critical(ctx, "failed to convert subsystems", slog.Error(err))
Expand Down Expand Up @@ -1127,7 +1129,7 @@ func (a *agent) handleManifest(manifestOK *checkpoint) func(ctx context.Context,
)
if a.experimentalDevcontainersEnabled {
var dcScripts []codersdk.WorkspaceAgentScript
scripts, dcScripts = agentcontainers.ExtractAndInitializeDevcontainerScripts(a.logger, expandPathToAbs, manifest.Devcontainers, scripts)
scripts, dcScripts = agentcontainers.ExtractAndInitializeDevcontainerScripts(a.logger, manifest.Devcontainers, scripts)
// See ExtractAndInitializeDevcontainerScripts for motivation
// behind running dcScripts as post start scripts.
scriptRunnerOpts = append(scriptRunnerOpts, agentscripts.WithPostStartScripts(dcScripts...))
Expand Down
13 changes: 11 additions & 2 deletions agent/agentcontainers/devcontainer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@
// initialize the workspace (e.g. git clone, npm install, etc). This is
// important if e.g. a Coder module to install @devcontainer/cli is used.
func ExtractAndInitializeDevcontainerScripts(
logger slog.Logger,

Check failure on line 39 in agent/agentcontainers/devcontainer.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'logger' seems to be unused, consider removing or renaming it as _ (revive)
expandPath func(string) (string, error),
devcontainers []codersdk.WorkspaceAgentDevcontainer,
scripts []codersdk.WorkspaceAgentScript,
) (filteredScripts []codersdk.WorkspaceAgentScript, devcontainerScripts []codersdk.WorkspaceAgentScript) {
Expand All @@ -47,7 +46,6 @@
// The devcontainer scripts match the devcontainer ID for
// identification.
if script.ID == dc.ID {
dc = expandDevcontainerPaths(logger, expandPath, dc)
devcontainerScripts = append(devcontainerScripts, devcontainerStartupScript(dc, script))
continue ScriptLoop
}
Expand Down Expand Up @@ -75,6 +73,17 @@
return script
}

// ExpandAllDevcontainerPaths expands all devcontainer paths in the given
// devcontainers. This is required by the devcontainer CLI, which requires
// absolute paths for the workspace folder and config path.
func ExpandAllDevcontainerPaths(logger slog.Logger, expandPath func(string) (string, error), devcontainers []codersdk.WorkspaceAgentDevcontainer) []codersdk.WorkspaceAgentDevcontainer {
expanded := make([]codersdk.WorkspaceAgentDevcontainer, 0, len(devcontainers))
for _, dc := range devcontainers {
expanded = append(expanded, expandDevcontainerPaths(logger, expandPath, dc))
}
return expanded
}

func expandDevcontainerPaths(logger slog.Logger, expandPath func(string) (string, error), dc codersdk.WorkspaceAgentDevcontainer) codersdk.WorkspaceAgentDevcontainer {
logger = logger.With(slog.F("devcontainer", dc.Name), slog.F("workspace_folder", dc.WorkspaceFolder), slog.F("config_path", dc.ConfigPath))

Expand Down
3 changes: 1 addition & 2 deletions agent/agentcontainers/devcontainer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,7 @@ func TestExtractAndInitializeDevcontainerScripts(t *testing.T) {
}
gotFilteredScripts, gotDevcontainerScripts := agentcontainers.ExtractAndInitializeDevcontainerScripts(
logger,
tt.args.expandPath,
tt.args.devcontainers,
agentcontainers.ExpandAllDevcontainerPaths(logger, tt.args.expandPath, tt.args.devcontainers),
tt.args.scripts,
)

Expand Down
Loading