Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

direnv exec not unloading previous environment #383

Closed
smancill opened this issue Jul 31, 2018 · 1 comment · Fixed by #695
Closed

direnv exec not unloading previous environment #383

smancill opened this issue Jul 31, 2018 · 1 comment · Fixed by #695

Comments

@smancill
Copy link

Issue description

It should unload, right? That's what I get from this wiki page about tmux: direnv exec / tmux would ensure direnv is unloaded, but is not happening.

Steps to reproduce

I wrote a small test case:

#!/bin/bash

mkdir -p foo
echo "export FOO=foo" > foo/.envrc

mkdir -p bar
echo "export BAR=bar" > bar/.envrc

cat << EOF > check
#!/bin/sh
echo "FOO: \$FOO"
echo "BAR: \$BAR"
EOF
chmod +x check

direnv allow foo
direnv allow bar

echo -e ">> direnv exec . ./check"
direnv exec . ./check

echo -e "\n>> direnv exec foo ./check"
direnv exec foo ./check

echo -e "\n>> cd bar"
cd bar && eval "$(direnv export bash)"

echo -e "\n>> ../check"
../check

echo -e "\n>> direnv exec ../foo ../check    # BAR is not unset"
direnv exec ../foo ../check

echo -e "\n>> direnv exec .. ../check        # BAR is not unset"
direnv exec .. ../check

echo -e "\n>> cd ../foo"
cd ../foo && eval "$(direnv export bash)"

echo -e "\n>> ../check"
../check

The output is:

$ ./test-direnv-exec
>> direnv exec . ./check
FOO:
BAR:

>> direnv exec foo ./check
direnv: loading foo/.envrc
FOO: foo
BAR:

>> cd bar
direnv: loading .envrc
direnv: export +BAR

>> ../check
FOO:
BAR: bar

>> direnv exec ../foo ../check    # BAR is not unset
direnv: loading ../foo/.envrc
FOO: foo
BAR: bar

>> direnv exec .. ../check        # BAR is not unset
FOO:
BAR: bar

>> cd ../foo
direnv: loading .envrc
direnv: export +FOO

>> ../check
FOO: foo
BAR:

It can be reproduced also if all commands are run interactively.

Technical details

  • direnv version: 2.17.0
  • OS release: Mac OS X (10.12.6)
@jason0x43
Copy link
Contributor

Possibly the issue is here:

direnv/cmd_exec.go

Lines 48 to 61 in 40a1cb9

// Restore pristine environment if needed
if previousEnv, err = config.Revert(env); err != nil {
return
}
previousEnv.CleanContext()
// Load the rc
if toLoad := findUp(rcPath, ".envrc"); toLoad != "" {
if newEnv, err = config.EnvFromRC(toLoad, previousEnv); err != nil {
return
}
} else {
newEnv = env
}

In the else block, newEnv is set to env (the current environment) rather than to the just-computed previousEnv.

jason0x43 added a commit to jason0x43/direnv that referenced this issue Oct 21, 2020
zimbatm pushed a commit that referenced this issue Oct 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants