Skip to content

Commit

Permalink
Allow skipping .env autoload (#878)
Browse files Browse the repository at this point in the history
The feature to automatically load .env files in #845 is nice, but it's a little too greedy in certain situations. Allow skipping that behavior by default (going back to explicit dotenv calls in .envrc files) in the configuration toml file.

Co-authored-by: zimbatm <zimbatm@zimbatm.com>
  • Loading branch information
awarrenlove and zimbatm committed Jan 4, 2022
1 parent f597abf commit f68f741
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/cmd_exec.go
Expand Up @@ -52,7 +52,7 @@ func cmdExecAction(env Env, args []string, config *Config) (err error) {
previousEnv.CleanContext()

// Load the rc
if toLoad := findEnvUp(rcPath); toLoad != "" {
if toLoad := findEnvUp(rcPath, config.SkipDotenv); toLoad != "" {
if newEnv, err = config.EnvFromRC(toLoad, previousEnv); err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/cmd_export.go
Expand Up @@ -34,7 +34,7 @@ func exportCommand(currentEnv Env, args []string, config *Config) (err error) {

logDebug("loading RCs")
loadedRC := config.LoadedRC()
toLoad := findEnvUp(config.WorkDir)
toLoad := findEnvUp(config.WorkDir, config.SkipDotenv)

if loadedRC == nil && toLoad == "" {
return
Expand Down
3 changes: 3 additions & 0 deletions internal/cmd/config.go
Expand Up @@ -25,6 +25,7 @@ type Config struct {
TomlPath string
DisableStdin bool
StrictEnv bool
SkipDotenv bool
WarnTimeout time.Duration
WhitelistPrefix []string
WhitelistExact map[string]bool
Expand All @@ -50,6 +51,7 @@ type tomlGlobal struct {
BashPath string `toml:"bash_path"`
DisableStdin bool `toml:"disable_stdin"`
StrictEnv bool `toml:"strict_env"`
SkipDotenv bool `toml:"skip_dotenv"`
WarnTimeout tomlDuration `toml:"warn_timeout"`
}

Expand Down Expand Up @@ -128,6 +130,7 @@ func LoadConfig(env Env) (config *Config, err error) {
config.BashPath = tomlConf.BashPath
config.DisableStdin = tomlConf.DisableStdin
config.StrictEnv = tomlConf.StrictEnv
config.SkipDotenv = tomlConf.SkipDotenv
config.WarnTimeout = tomlConf.WarnTimeout.Duration
}

Expand Down
8 changes: 6 additions & 2 deletions internal/cmd/rc.go
Expand Up @@ -24,7 +24,7 @@ type RC struct {

// FindRC looks for ".envrc" and ".env" files up in the file hierarchy.
func FindRC(wd string, config *Config) (*RC, error) {
rcPath := findEnvUp(wd)
rcPath := findEnvUp(wd, config.SkipDotenv)
if rcPath == "" {
return nil, nil
}
Expand Down Expand Up @@ -287,7 +287,11 @@ func allow(path string, allowPath string) (err error) {
return ioutil.WriteFile(allowPath, []byte(path+"\n"), 0644)
}

func findEnvUp(searchDir string) (path string) {
func findEnvUp(searchDir string, skipDotenv bool) (path string) {
if skipDotenv {
return findUp(searchDir, ".envrc")
}

return findUp(searchDir, ".envrc", ".env")
}

Expand Down
4 changes: 4 additions & 0 deletions man/direnv.toml.1
Expand Up @@ -52,6 +52,10 @@ This allows one to hard-code the position of bash. It maybe be useful to set thi
.PP
If set to \fB\fCtrue\fR, stdin is disabled (redirected to /dev/null) during the \fB\fC\&.envrc\fR evaluation.

.SS \fB\fCskip_dotenv\fR
.PP
Don't look for \fB\fC\&.env\fR files, only \fB\fC\&.envrc\fR files.

.SS \fB\fCstrict_env\fR
.PP
If set to true, the \fB\fC\&.envrc\fR will be loaded with \fB\fCset -euo pipefail\fR\&. This
Expand Down
4 changes: 4 additions & 0 deletions man/direnv.toml.1.md
Expand Up @@ -42,6 +42,10 @@ This allows one to hard-code the position of bash. It maybe be useful to set thi

If set to `true`, stdin is disabled (redirected to /dev/null) during the `.envrc` evaluation.

### `skip_dotenv`

Don't look for `.env` files, only `.envrc` files.

### `strict_env`

If set to true, the `.envrc` will be loaded with `set -euo pipefail`. This
Expand Down
8 changes: 8 additions & 0 deletions test/direnv-test-common.sh
Expand Up @@ -47,6 +47,7 @@ test_start() {
}

test_stop() {
rm -f "${XDG_CONFIG_HOME}/direnv/direnv.toml"
cd /
direnv_eval
}
Expand Down Expand Up @@ -238,6 +239,13 @@ test_start "load-env"
test_eq "${HELLO}" "world"
test_stop

test_start "skip-env"
echo "[global]
skip_dotenv = true" > "${XDG_CONFIG_HOME}/direnv/direnv.toml"
direnv_eval
test -z "${SKIPPED}"
test_stop

# Context: foo/bar is a symlink to ../baz. foo/ contains and .envrc file
# BUG: foo/bar is resolved in the .envrc execution context and so can't find
# the .envrc file.
Expand Down
1 change: 1 addition & 0 deletions test/scenarios/skip-env/.env
@@ -0,0 +1 @@
SKIPPED=dotenv

0 comments on commit f68f741

Please sign in to comment.