Skip to content

Add direnv support#5660

Open
stefanhaller wants to merge 3 commits into
masterfrom
direnv-support
Open

Add direnv support#5660
stefanhaller wants to merge 3 commits into
masterfrom
direnv-support

Conversation

@stefanhaller
Copy link
Copy Markdown
Collaborator

Supports loading direnv's environment files (.envrc) when switching repos or worktrees, or when entering or exiting submodules.

There's not configuration for this; the functionality is automatically enabled when direnv is installed.

Closes #3653.

stefanhaller and others added 3 commits May 30, 2026 17:29
The for-loop here was a verbatim copy of openRecentRepo, so call that instead.
When a user opens a repo from the recent-repos menu or jumps between
worktrees inside lazygit, only the env vars present at process startup
reach subprocesses. That breaks pre-commit hooks and other tools whose
dependencies are pulled in by a per-repo .envrc — users were left with
read-only operations because the env their shell would normally load via
direnv never made it into lazygit's git invocations.

Shell out to `direnv export json` after each chdir and apply the JSON
delta via os.Setenv/Unsetenv. direnv tracks the previous load in its own
DIRENV_DIFF env var, so the delta also unloads vars from the old repo
when entering one without a matching .envrc. If direnv isn't on PATH the
call is a no-op, so users who don't use direnv pay nothing and users who
do need no config to opt in. Any stderr direnv emits (loading messages,
"blocked .envrc" errors, etc.) goes to the command log.

The integration test puts a fake direnv on PATH and asserts that a value
it exports reaches a custom command after switching repos. Wiring this
up needed runner.go to support `{{actualPath}}` placeholders in
ExtraEnvVars, mirroring the existing support for ExtraCmdArgs, so the
test can prepend a fixture-relative directory to PATH.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Direnv support in lazygit

1 participant