Skip to content

feat(vscode): add opt-in runInitHookOnReopen setting - #2975

Merged
mikeland73 merged 1 commit into
mainfrom
claude/vscode-run-init-hook-setting
Sep 16, 2026
Merged

mikeland73 merged 1 commit into
mainfrom
claude/vscode-run-init-hook-setting

Conversation

@mikeland73

Copy link
Copy Markdown
Collaborator

Stacked on #2874 (adds the --run-init-hook flag to devbox integrate vscode). Merge that first.

Summary

  • New extension setting devbox.runInitHookOnReopen (boolean, default false). When enabled, "Devbox: Reopen in Devbox shell environment" passes --run-init-hook to devbox integrate vscode, so the project's init_hook runs and any environment variables it exports are present in the reopened editor. See devbox integrate does NOT run init_hook #2703.
  • The flag is only appended when the setting is on, so users on an older CLI that doesn't know the flag keep working with default settings.
  • Bumps the extension to 0.1.9 so vsce publish --skip-duplicate in the release workflow actually publishes the change.
  • README and CHANGELOG updated.

How was it tested?

  • yarn compile and yarn lint pass in vscode-extension/.

🤖 Generated with Claude Code

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>
Base automatically changed from claude/focused-goldberg-6qy8fs to main September 15, 2026 21:25
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
mikeland73 force-pushed the claude/vscode-run-init-hook-setting branch from d91a987 to b586967 Compare September 15, 2026 21:33
@claude

claude Bot commented Sep 15, 2026

Copy link
Copy Markdown

Code review

No issues found. Checked for bugs and CLAUDE.md compliance.

@mikeland73
mikeland73 merged commit d106f7a into main Sep 16, 2026
28 checks passed
@mikeland73
mikeland73 deleted the claude/vscode-run-init-hook-setting branch September 16, 2026 00:01
@apgrucza

Copy link
Copy Markdown
Contributor

Looks like the vscode release workflow hasn't run yet: https://github.com/jetify-com/devbox/actions/workflows/vscode-ext-release.yaml

@mikeland73

Copy link
Copy Markdown
Collaborator Author

Looks like the vscode release workflow hasn't run yet: https://github.com/jetify-com/devbox/actions/workflows/vscode-ext-release.yaml

on it!

@mohsenari let me know if anything else I should do

@mohsenari

Copy link
Copy Markdown
Collaborator

I think we're good. Screenshot_20260916_232339_Firefox.png

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants