feat(vscode): add opt-in runInitHookOnReopen setting - #2975
Merged
Merged
Conversation
mikeland73
added a commit
that referenced
this pull request
Sep 15, 2026
…ning an editor (#2874) ## Summary Addresses #2703. The VSCode extension side is in the stacked PR #2975. `devbox integrate vscode` powers the VSCode/Cursor **"Reopen in Devbox"** action. It computes the Devbox environment and relaunches the editor with it — but it uses `Devbox.EnvVars`, which **deliberately excludes the init hook**: ```go // internal/devbox/devbox.go func (d *Devbox) EnvVars(ctx context.Context) ([]string, error) { // this only returns env variables for the shell environment excluding hooks ... } ``` So any environment variables exported by a project's `init_hook` are missing from the reopened editor, even though they are present in a normal `devbox shell`. Editors like Cursor that open their own terminals — and don't get the `devbox shell` injection — are left without those variables. ## Fix - Add `Devbox.EnvVarsWithInitHook`, which sources the init hook in a subshell and captures the resulting environment. - Add an **opt-in** `--run-init-hook` flag to `devbox integrate vscode` (default off). When set, the command uses `EnvVarsWithInitHook` instead of `EnvVars`. Default behavior is unchanged, since init hooks can be slow or have side effects. Implementation details: - The init hook is sourced with its **stdout redirected to stderr** (`. "$1" 1>&2`) so the hook's own output can never corrupt the captured environment (the integrate command speaks an IPC protocol to the editor over a separate fd, so this is important). - The hooks path is passed as a positional parameter rather than interpolated into the script, so special characters in the path can't affect shell parsing. - The environment is dumped **NUL-separated** via `awk`'s `ENVIRON` (portable to macOS, unlike `env -0`) so values containing newlines survive intact. - If the init hook errors, it **falls back** to the hook-less environment rather than failing, so the integration keeps working. - The hooks file is written via the existing `shellgen.WriteScriptsToFiles`, and sourced the same way `EnvExports`/direnv already source it — so this matches existing behavior of running hooks outside an interactive shell. ## How was it tested? - `go build ./...` and `go vet` pass. - Unit tests in `internal/devbox/inithookenv_test.go`: - `TestParseNulEnv` — parsing of NUL-separated output, including a value with an embedded newline and an empty value. - `TestCaptureEnvWithInitHook` — a hook that sets a new var, overrides an existing one, and prints to stdout; asserts the new/overridden vars are captured and the hook's stdout does **not** leak into the env. - `TestCaptureEnvWithInitHook_NoHooksFile` — returns the base env unchanged when there is no hooks file. > Note: the `integrate vscode` command itself requires a Node parent process (`go2node`), so it isn't exercised by a testscript here; the new logic is covered by the unit tests above and CI runs the full suite. cc @tm-michael (issue reporter) — thanks for the clear repro. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude <noreply@anthropic.com>
Adds a devbox.runInitHookOnReopen setting (default off) that passes --run-init-hook to `devbox integrate vscode` so the project's init_hook runs and its environment variables are included when reopening the editor in the Devbox environment. Bumps the extension to 0.1.9. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
mikeland73
force-pushed
the
claude/vscode-run-init-hook-setting
branch
from
September 15, 2026 21:33
d91a987 to
b586967
Compare
Code reviewNo issues found. Checked for bugs and CLAUDE.md compliance. |
Contributor
|
Looks like the vscode release workflow hasn't run yet: https://github.com/jetify-com/devbox/actions/workflows/vscode-ext-release.yaml |
Collaborator
Author
on it! @mohsenari let me know if anything else I should do |
Collaborator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Stacked on #2874 (adds the
--run-init-hookflag todevbox integrate vscode). Merge that first.Summary
devbox.runInitHookOnReopen(boolean, defaultfalse). When enabled, "Devbox: Reopen in Devbox shell environment" passes--run-init-hooktodevbox integrate vscode, so the project'sinit_hookruns and any environment variables it exports are present in the reopened editor. Seedevbox integratedoes NOT run init_hook #2703.vsce publish --skip-duplicatein the release workflow actually publishes the change.How was it tested?
yarn compileandyarn lintpass invscode-extension/.🤖 Generated with Claude Code