Skip to content

fix(direnv): set DEVBOX_SHELL_ENABLED when entering env via shellenv --init-hook#2861

Open
mikeland73 wants to merge 1 commit into
mainfrom
claude/focused-goldberg-39laj0
Open

fix(direnv): set DEVBOX_SHELL_ENABLED when entering env via shellenv --init-hook#2861
mikeland73 wants to merge 1 commit into
mainfrom
claude/focused-goldberg-39laj0

Conversation

@mikeland73

Copy link
Copy Markdown
Collaborator

Summary

Fixes #2746.

When using direnv to load a Devbox environment, $DEVBOX_SHELL_ENABLED was never set, even though the documentation states it is "automatically set to 1 when you start a shell, run a script, or start services."

The direnv integration loads the environment by evaluating devbox shellenv --init-hook (see internal/devbox/generate/tmpl/envrcContent.tmpl). That code path runs through Devbox.EnvExports, which — unlike Shell() and RunScript() — never set DEVBOX_SHELL_ENABLED. So echo $DEVBOX_SHELL_ENABLED printed nothing in a direnv-activated shell.

Fix

Set DEVBOX_SHELL_ENABLED=1 in EnvExports when init hooks are run, mirroring what Shell() and RunScript() already do.

The assignment is gated on RunHooks on purpose:

  • The direnv integration uses devbox shellenv --init-hook (RunHooks = true) → now sets the variable. ✅
  • The global integration devbox global shellenv runs in every shell and does not pass --init-hook (RunHooks = false) → still does not set the variable, so shell-inception detection (which relies on DEVBOX_SHELL_ENABLED) keeps working in every shell. ✅
  • Plain devbox shellenv is likewise unaffected.

How was it tested?

  • go build ./... and go vet ./internal/devbox/ pass.
  • Added a regression testscript testscripts/shell/shellenv_init_hook.test.txt asserting that:
    • devbox shellenv --init-hook exports DEVBOX_SHELL_ENABLED="1", and
    • plain devbox shellenv does not export it.

Note: the nix-backed testscripts could not be executed in the authoring environment (no nix available), but the change is small and the new test documents the expected behavior for CI.

cc @miklschmidt (issue reporter)


Generated by Claude Code

…--init-hook

The direnv integration loads the Devbox environment by evaluating
`devbox shellenv --init-hook`, but that path never set
DEVBOX_SHELL_ENABLED. As a result `$DEVBOX_SHELL_ENABLED` was empty in
direnv-activated shells, contradicting the documented behavior that it is
set to 1 whenever the environment is loaded.

Set DEVBOX_SHELL_ENABLED=1 in EnvExports when init hooks are run, mirroring
what Shell() and RunScript() already do. The flag is gated on RunHooks so
that the global integration (`devbox global shellenv`, which does not run
init hooks) does not set it in every shell and break shell-inception
detection.

Adds a regression testscript asserting that `devbox shellenv --init-hook`
exports the variable while plain `devbox shellenv` does not.

Fixes #2746
Copilot AI review requested due to automatic review settings June 10, 2026 14:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes the direnv activation path so that DEVBOX_SHELL_ENABLED is exported when entering a Devbox environment via devbox shellenv --init-hook, aligning behavior with the documented env-var semantics and resolving #2746.

Changes:

  • Set DEVBOX_SHELL_ENABLED=1 inside Devbox.EnvExports() when RunHooks is enabled (i.e., shellenv --init-hook), while leaving plain shellenv / global shellenv unchanged.
  • Add a regression testscript to assert --init-hook exports the variable and plain shellenv does not.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
internal/devbox/devbox.go Exports DEVBOX_SHELL_ENABLED when EnvExports is invoked with init hooks enabled (RunHooks=true).
testscripts/shell/shellenv_init_hook.test.txt Adds coverage to prevent regressions for shellenv --init-hook vs plain shellenv.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

[direnv] DEVBOX_SHELL_ENABLED is missing from devbox generate direnv --print-envrc

3 participants