Skip to content

refactor(devcontainer): to use better scoping of responsibilities#624

Merged
skevetter merged 4 commits into
mainfrom
golden-narwhal
Jul 8, 2026
Merged

refactor(devcontainer): to use better scoping of responsibilities#624
skevetter merged 4 commits into
mainfrom
golden-narwhal

Conversation

@skevetter

@skevetter skevetter commented Jul 8, 2026

Copy link
Copy Markdown
Contributor

Rebuilds pkg/devcontainer/run.go from the ground up using idiomatic Go, addressing its low-cohesion "grab-bag" structure and non-idiomatic patterns.

Structure

Split the single file into three cohesive units:

  • run.goRunner interface, runner type, Up dispatch, thin delegations (Command/Find/Logs), runDefaultContainer
  • initialize.go (new) — host-side initializeCommand hook execution
  • mount.go (new) — workspace mount / consistency helpers

Changes

  • Unexported the runner struct's fields (Driver, ID, WorkspaceConfig, LocalWorkspaceFolder, AgentPath, AgentDownloadURL, IDLabels) — they were exported on an unexported type. Applied via type-aware gopls rename across the package.
  • Removed initCmdContext, a struct that mimicked context.Context while only bundling args, and dropped the redundant serial-vs-parallel branch (the parallel path already covers the single-command case). Uses sync.WaitGroup.Go.
  • De-duplicated the /workspaces/<id> mount-folder defaulting (previously repeated three times) into containerMountFolder + withDefaultConsistency.

The exported package API is unchanged. All package tests pass; no new lint findings.

Summary by CodeRabbit

  • New Features

    • Dev container startup now runs initialization commands from configuration, with support for multiple commands and platform-aware shell handling.
    • Workspace mount handling is more flexible, including smarter defaults and improved consistency options on non-Linux systems.
  • Bug Fixes

    • Improved reliability across build, start, stop, delete, and inspect flows.
    • Fixed several cases where workspace and container settings were not applied consistently during dev container operations.

Split the grab-bag run.go into cohesive files, unexport the runner
struct fields, and simplify the initializeCommand and mount helpers.

- run.go: Runner interface, runner type, Up dispatch, delegations
- initialize.go: host-side initializeCommand hook execution
- mount.go: workspace mount/consistency helpers

Renames runner's exported fields (Driver, ID, WorkspaceConfig, ...) to
unexported equivalents via type-aware rename across the package. Removes
the context-mimicking initCmdContext struct and the redundant
serial/parallel split, and de-duplicates the workspace mount-folder
defaulting.
@netlify

netlify Bot commented Jul 8, 2026

Copy link
Copy Markdown

Deploy Preview for images-devsy-sh canceled.

Name Link
🔨 Latest commit f6deac5
🔍 Latest deploy log https://app.netlify.com/projects/images-devsy-sh/deploys/6a4e9abe4625860008c145ea

@coderabbitai

coderabbitai Bot commented Jul 8, 2026

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 61bdebdb-0e9b-4cbd-bdd0-ba6e278edc7f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@netlify

netlify Bot commented Jul 8, 2026

Copy link
Copy Markdown

Deploy Preview for devsydev canceled.

Name Link
🔨 Latest commit f6deac5
🔍 Latest deploy log https://app.netlify.com/projects/devsydev/deploys/6a4e9abe161a040008e3300c

@skevetter skevetter changed the title refactor(devcontainer): rebuild run.go with idiomatic Go structure refactor(devcontainer): to use better scoping of responsibilities Jul 8, 2026
@skevetter
skevetter marked this pull request as ready for review July 8, 2026 15:21

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@pkg/devcontainer/initialize.go`:
- Around line 76-94: The initializeCommand runner can panic when an entry like
an empty command slice leaves args empty and then indexes args[0] in
initializeCommand.run. Add an early guard in run, before the log/exec.Command
path, to detect len(args) == 0 and return a normal error that references the
command name rather than indexing into args; keep the fix localized to
initializeCommand.run and preserve the existing handling for single-argument
commands and shell wrapping.

In `@pkg/devcontainer/mount.go`:
- Around line 69-85: The mountSetConsistency helper currently turns an empty
workspace mount into a synthetic consistency entry, which incorrectly converts
“no mount” into a non-empty string when applyMountContext applies the CLI
consistency flag. Update mountSetConsistency to preserve an empty mount input
unchanged, and only add or replace the consistency token when there is an actual
mount string to modify; use the existing mountSetConsistency and
applyMountContext flow as the place to fix this behavior.
- Around line 38-55: The withDefaultConsistency helper is applying the default
consistency flag too broadly, including custom WorkspaceMount values that are
not bind mounts. Update withDefaultConsistency so it only appends the default
consistency for bind mounts, and leave other mount types unchanged; use the
existing mount parsing logic around config.ParseMount, workspaceMount handling,
and mountHasConsistency to detect the mount type before modifying it.

In `@pkg/devcontainer/run.go`:
- Around line 199-201: The fallback image warning in run.go is logging
user-controlled input from params.options.FallbackImage via string
concatenation, which can enable CRLF log injection. Update the warning in the
fallback-image branch to use formatted logging in the same place as log.Warn and
escape the value with %q or otherwise sanitize newlines before logging, while
keeping the conf.ImageContainer assignment unchanged.
- Around line 129-130: `runInitializeCommand` is not being interrupted by
cancellation/timeout, so the host-side hook can keep blocking even after `Up`
stops. Thread `ctx` from the `Up` flow into `runInitializeCommand`, and update
that function to use `exec.CommandContext` so the initialize command is
terminated when the context is canceled. Make sure the call site and the command
execution path both use the propagated context consistently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e9e5d194-20bf-404b-bb71-5a029fdb0734

📥 Commits

Reviewing files that changed from the base of the PR and between 4814ef3 and ad9b748.

📒 Files selected for processing (16)
  • pkg/devcontainer/build.go
  • pkg/devcontainer/compose.go
  • pkg/devcontainer/compose_test.go
  • pkg/devcontainer/compose_up.go
  • pkg/devcontainer/config.go
  • pkg/devcontainer/config_test.go
  • pkg/devcontainer/delete.go
  • pkg/devcontainer/delete_test.go
  • pkg/devcontainer/initialize.go
  • pkg/devcontainer/inspect.go
  • pkg/devcontainer/mount.go
  • pkg/devcontainer/prebuild.go
  • pkg/devcontainer/run.go
  • pkg/devcontainer/setup.go
  • pkg/devcontainer/setup_test.go
  • pkg/devcontainer/single.go

Comment thread pkg/devcontainer/initialize.go Outdated
Comment thread pkg/devcontainer/mount.go
Comment thread pkg/devcontainer/mount.go
Comment thread pkg/devcontainer/run.go Outdated
Comment thread pkg/devcontainer/run.go
skevetter added 3 commits July 8, 2026 13:33
…ed mounts

Address PR review findings:
- initializeCommand.run: return a named error for an empty command slice
  (e.g. `{"noop": []}`) instead of panicking on args[0].
- mountSetConsistency: keep a suppressed (empty) workspaceMount empty rather
  than synthesizing a malformed consistency-only mount string.

Adds regression tests for both.
…ed image

Address further PR review findings:
- Thread the Up context through runInitializeCommand into exec.CommandContext
  so a cancelled/timed-out Up interrupts a blocking host-side hook.
- Log the fallback image with %q to prevent CRLF log injection from
  CLI/config-derived values.
@skevetter
skevetter merged commit 46c5333 into main Jul 8, 2026
105 of 107 checks passed
@skevetter
skevetter deleted the golden-narwhal branch July 8, 2026 19:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant