refactor(devcontainer): to use better scoping of responsibilities#624
Conversation
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.
✅ Deploy Preview for images-devsy-sh canceled.
|
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
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. Comment |
✅ Deploy Preview for devsydev canceled.
|
There was a problem hiding this comment.
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
📒 Files selected for processing (16)
pkg/devcontainer/build.gopkg/devcontainer/compose.gopkg/devcontainer/compose_test.gopkg/devcontainer/compose_up.gopkg/devcontainer/config.gopkg/devcontainer/config_test.gopkg/devcontainer/delete.gopkg/devcontainer/delete_test.gopkg/devcontainer/initialize.gopkg/devcontainer/inspect.gopkg/devcontainer/mount.gopkg/devcontainer/prebuild.gopkg/devcontainer/run.gopkg/devcontainer/setup.gopkg/devcontainer/setup_test.gopkg/devcontainer/single.go
…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.
Rebuilds
pkg/devcontainer/run.gofrom 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.go—Runnerinterface,runnertype,Updispatch, thin delegations (Command/Find/Logs),runDefaultContainerinitialize.go(new) — host-sideinitializeCommandhook executionmount.go(new) — workspace mount / consistency helpersChanges
runnerstruct's fields (Driver,ID,WorkspaceConfig,LocalWorkspaceFolder,AgentPath,AgentDownloadURL,IDLabels) — they were exported on an unexported type. Applied via type-awaregopls renameacross the package.initCmdContext, a struct that mimickedcontext.Contextwhile only bundling args, and dropped the redundant serial-vs-parallel branch (the parallel path already covers the single-command case). Usessync.WaitGroup.Go./workspaces/<id>mount-folder defaulting (previously repeated three times) intocontainerMountFolder+withDefaultConsistency.The exported package API is unchanged. All package tests pass; no new lint findings.
Summary by CodeRabbit
New Features
Bug Fixes