Skip to content

Commit 6924177

Browse files
authored
Merge pull request #814 from motdotla/dont-check-existance
do not check if exists
2 parents 1146910 + 3533420 commit 6924177

File tree

3 files changed

+34
-36
lines changed

3 files changed

+34
-36
lines changed

CHANGELOG.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5-
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.4.4...master)
5+
## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.4.5...master)
6+
7+
## [16.4.5](https://github.com/motdotla/dotenv/compare/v16.4.4...v16.4.5) (2024-02-19)
8+
9+
### Changed
10+
11+
- 🐞 fix recent regression when using `path` option. return to historical behavior: do not attempt to auto find `.env` if `path` set. (regression was introduced in `16.4.3`) [#814](https://github.com/motdotla/dotenv/pull/814)
612

713
## [16.4.4](https://github.com/motdotla/dotenv/compare/v16.4.3...v16.4.4) (2024-02-13)
814

lib/main.js

+27-31
Original file line numberDiff line numberDiff line change
@@ -215,52 +215,48 @@ function configDotenv (options) {
215215
}
216216
}
217217

218-
let optionPathsThatExist = []
218+
let optionPaths = [dotenvPath] // default, look for .env
219219
if (options && options.path) {
220220
if (!Array.isArray(options.path)) {
221-
if (fs.existsSync(options.path)) {
222-
optionPathsThatExist = [_resolveHome(options.path)]
223-
}
221+
optionPaths = [_resolveHome(options.path)]
224222
} else {
223+
optionPaths = [] // reset default
225224
for (const filepath of options.path) {
226-
if (fs.existsSync(filepath)) {
227-
optionPathsThatExist.push(_resolveHome(filepath))
228-
}
225+
optionPaths.push(_resolveHome(filepath))
229226
}
230227
}
231-
232-
if (!optionPathsThatExist.length) {
233-
optionPathsThatExist = [dotenvPath]
234-
}
235228
}
236229

237-
// If we have options.path, and it had valid paths, use them. Else fall back to .env
238-
const pathsToProcess = optionPathsThatExist.length ? optionPathsThatExist : [dotenvPath]
239-
240230
// Build the parsed data in a temporary object (because we need to return it). Once we have the final
241231
// parsed data, we will combine it with process.env (or options.processEnv if provided).
242-
243-
const parsed = {}
244-
try {
245-
for (const path of pathsToProcess) {
232+
let lastError
233+
const parsedAll = {}
234+
for (const path of optionPaths) {
235+
try {
246236
// Specifying an encoding returns a string instead of a buffer
247-
const singleFileParsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))
248-
DotenvModule.populate(parsed, singleFileParsed, options)
249-
}
237+
const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding }))
250238

251-
let processEnv = process.env
252-
if (options && options.processEnv != null) {
253-
processEnv = options.processEnv
239+
DotenvModule.populate(parsedAll, parsed, options)
240+
} catch (e) {
241+
if (debug) {
242+
_debug(`Failed to load ${path} ${e.message}`)
243+
}
244+
lastError = e
254245
}
246+
}
255247

256-
DotenvModule.populate(processEnv, parsed, options)
257-
} catch (e) {
258-
if (debug) {
259-
_debug(`Failed to load ${pathsToProcess} ${e.message}`)
260-
}
261-
return { error: e }
248+
let processEnv = process.env
249+
if (options && options.processEnv != null) {
250+
processEnv = options.processEnv
251+
}
252+
253+
DotenvModule.populate(processEnv, parsedAll, options)
254+
255+
if (lastError) {
256+
return { parsed: parsedAll, error: lastError }
257+
} else {
258+
return { parsed: parsedAll }
262259
}
263-
return { parsed }
264260
}
265261

266262
// Populates process.env from .env file

tests/test-config.js

-4
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,12 @@ t.test('takes URL for path option', ct => {
8888
})
8989

9090
t.test('takes option for path along with home directory char ~', ct => {
91-
const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true)
9291
const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo')
9392
const mockedHomedir = '/Users/dummy'
9493
const homedirStub = sinon.stub(os, 'homedir').returns(mockedHomedir)
9594
const testPath = '~/.env'
9695
dotenv.config({ path: testPath })
9796

98-
ct.equal(existsSyncStub.args[0][0], testPath)
99-
ct.ok(existsSyncStub.called)
100-
10197
ct.equal(readFileSyncStub.args[0][0], path.join(mockedHomedir, '.env'))
10298
ct.ok(homedirStub.called)
10399

0 commit comments

Comments
 (0)