Skip to content

runtime: probe user env before lifecycle hooks#55

Merged
bilby91 merged 1 commit into
mainfrom
runtime/probe-before-lifecycle
May 13, 2026
Merged

runtime: probe user env before lifecycle hooks#55
bilby91 merged 1 commit into
mainfrom
runtime/probe-before-lifecycle

Conversation

@bilby91
Copy link
Copy Markdown
Member

@bilby91 bilby91 commented May 13, 2026

Summary

  • Engine.Up was probing userEnvProbe only after running every lifecycle phase, so ws.probedEnv was empty during postCreateCommand (and earlier phases). Hooks that relied on PATH entries published via .bashrc / /etc/profile.d snippets — nvm-managed node, asdf, the official devcontainers-extra/pnpm feature — failed with command not found. This diverged from @devcontainers/cli, which awaits the probe promise before every lifecycle exec.
  • Fix: probe before runAllLifecycle so hooks see the rc-derived env, then re-probe after so any rc edits made by the hooks themselves are reflected in subsequent Exec calls. Probe failures stay non-fatal.
  • No changes to lifecycle.go / exec.go: Exec already merges ws.probedEnv, and lifecycle hooks route through Exec — updating ws.probedEnv at the right moment is sufficient.

Test plan

  • go test ./... (unit) passes
  • go test -tags=integration -run TestUserEnvProbe ./test/integration/ passes
  • New TestUserEnvProbe_LifecycleSeesBashrcPath regression test: builds an image that drops a binary in /opt/mytool/bin and a /etc/profile.d/mytool.sh that prepends it to PATH; postCreateCommand runs command -v mytool && mytool > /tmp/lifecycle-out. Fails on the old code (Up errors because the hook can't find the binary), passes after the fix.

🤖 Generated with Claude Code

Summary by CodeRabbit

Bug Fixes

  • Shell-derived environment variables are now accessible during lifecycle hook execution and are recaptured afterward to include any modifications made by the hooks.

Tests

  • Added integration test for environment variable handling during lifecycle execution.

Review Change Stack

… PATH

Engine.Up ran every lifecycle phase before probing the user's shell
env, so ws.probedEnv was empty during postCreateCommand. Hooks that
relied on PATH entries published via .bashrc / /etc/profile.d snippets
(nvm-managed node, asdf, devcontainers-extra/pnpm) failed with
"command not found", diverging from @devcontainers/cli, which awaits
the probe before each lifecycle exec.

Probe before runAllLifecycle so hooks see rc-derived env, then
re-probe after so rc edits made by the hooks themselves reach
subsequent Exec calls.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 13, 2026

📝 Walkthrough

Walkthrough

The PR reorders the user-environment probe in Engine.Up to execute before lifecycle hooks and again after them, ensuring lifecycle commands observe shell-derived PATH variables while subsequent operations reflect any environment changes made by those hooks. An integration test validates the visibility of probed PATH during lifecycle command execution.

Changes

Shell-environment probe and lifecycle hooks

Layer / File(s) Summary
User-environment probe timing in lifecycle
up.go
probeUserEnv is moved to execute before the lifecycle phase so lifecycle hooks see the probed shell environment, then re-runs after lifecycle to capture environment modifications made by lifecycle tools.
Lifecycle PATH visibility test
test/integration/userenvprobe_test.go
Integration test TestUserEnvProbe_LifecycleSeesBashrcPath verifies that shell-derived PATH is observable during postCreateCommand execution by creating a fixture with a mytool binary and /etc/profile.d/ export, running the lifecycle, and asserting tool output.

Sequence Diagram

sequenceDiagram
  participant EngineUp
  participant probeUserEnv
  participant LifecycleHooks
  participant Exec
  
  EngineUp->>probeUserEnv: Execute before lifecycle
  probeUserEnv->>EngineUp: ws.probedEnv updated with shell PATH
  EngineUp->>LifecycleHooks: Run postCreateCommand (sees probed env)
  LifecycleHooks->>EngineUp: Lifecycle completes (may modify env)
  EngineUp->>probeUserEnv: Re-execute after lifecycle
  probeUserEnv->>EngineUp: ws.probedEnv refreshed from rc changes
  EngineUp->>Exec: Subsequent Exec calls see updated env
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • crunchloop/devcontainer#19: Both PRs implement the userEnvProbe capture and propagation via ws.probedEnv and change Engine.Up behavior so PATH additions from bashrc-derived files are visible during lifecycle and exec calls.

Poem

🐰 In the time of probes before and after,
The PATH leaps forth with shell-sourced laughter,
Hooks now see the tools at hand,
While tests assert this working land.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'runtime: probe user env before lifecycle hooks' directly and concisely describes the main change: moving user environment probe execution to occur before lifecycle hooks run.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch runtime/probe-before-lifecycle

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

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 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 `@up.go`:
- Around line 226-245: The current code only updates ws.probedEnv before
runAllLifecycle and after it finishes, leaving lifecycle steps (e.g.,
onCreateCommand → postCreateCommand) using a stale env; modify the lifecycle
handling so the environment is refreshed between individual lifecycle execs:
either call e.probeUserEnv(ctx, ws, ws.Config.UserEnvProbe) and update
ws.probedEnv before each lifecycle exec inside runAllLifecycle (or have
runAllLifecycle invoke a helper that re-probes between phases), ensuring errors
remain non-fatal (keep the same err==nil check) and preserving existing
semantics of e.runAllLifecycle, e.probeUserEnv, and ws.probedEnv.
🪄 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: fd7272be-0d07-49c4-9735-16374fd07e8d

📥 Commits

Reviewing files that changed from the base of the PR and between e183522 and 29a35e6.

📒 Files selected for processing (2)
  • test/integration/userenvprobe_test.go
  • up.go

Comment thread up.go
@bilby91 bilby91 merged commit 75a8c84 into main May 13, 2026
5 checks passed
@bilby91 bilby91 deleted the runtime/probe-before-lifecycle branch May 14, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant