Visitar URL original
fix(cli): load the default .env when DOTENV_PATH is empty · motdotla/dotenv@bc6ef94 · GitHub
Skip to content

Commit bc6ef94

Browse files
committed
fix(cli): load the default .env when DOTENV_PATH is empty
1 parent 6d6e6c5 commit bc6ef94

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎cli.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ function resolveRunOptions (parsed) {
126126
defaultPath: true
127127
}
128128

129-
if (envOptions.path != null) {
129+
// Like config(), an empty path keeps the default .env.
130+
if (envOptions.path) {
130131
options.paths = [envOptions.path]
131132
options.defaultPath = false
132133
}

‎tests/test-cli.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,23 @@ t.test('file flags reject missing or empty lists', ct => {
8888
ct.end()
8989
})
9090

91+
t.test('empty path environment variables load the default .env like config()', ct => {
92+
const cwd = ct.testdir({ '.env': 'BASIC=default_basic\n' })
93+
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.toUpperCase().startsWith('DOTENV_')))
94+
delete env.BASIC
95+
for (const name of ['DOTENV_PATH', 'DOTENV_CONFIG_PATH']) {
96+
const result = cp.spawnSync(process.execPath, [
97+
path.resolve(__dirname, '../dist/index.cjs'), 'run', '-q',
98+
process.execPath, '-e', 'console.log(process.env.BASIC)'
99+
], { cwd, encoding: 'utf8', env: { ...env, [name]: '' } })
100+
101+
ct.equal(result.status, 0, `${name}: ${result.stderr}`)
102+
ct.equal(result.stdout, 'default_basic\n', `${name}: loads .env`)
103+
ct.equal(result.stderr, '', `${name}: no error`)
104+
}
105+
ct.end()
106+
})
107+
91108
t.test('preserves spaces, quotes, empty arguments, and shell metacharacters', ct => {
92109
const args = ['two words', 'a"quote', '', 'trailing\\', 'a&b', 'x|y', '<input>', '%PATH%', '!name!', '(value)', '^caret']
93110
const result = run([

0 commit comments

Comments
 (0)