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

Allow skipping .env autoload #878

Merged
merged 2 commits into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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