Skip to content

Commit 249e5a6

Browse files
committed
adjust logic to support tests
1 parent 87fd887 commit 249e5a6

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
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. should never try to default to `.env` if 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-27
Original file line numberDiff line numberDiff line change
@@ -215,48 +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-
optionPathsThatExist = [_resolveHome(options.path)]
221+
optionPaths = [_resolveHome(options.path)]
222222
} else {
223+
optionPaths = [] // reset default
223224
for (const filepath of options.path) {
224-
optionPathsThatExist.push(_resolveHome(filepath))
225+
optionPaths.push(_resolveHome(filepath))
225226
}
226227
}
227-
228-
if (!optionPathsThatExist.length) {
229-
optionPathsThatExist = [dotenvPath]
230-
}
231228
}
232229

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

247-
let processEnv = process.env
248-
if (options && options.processEnv != null) {
249-
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
250245
}
246+
}
251247

252-
DotenvModule.populate(processEnv, parsed, options)
253-
} catch (e) {
254-
if (debug) {
255-
_debug(`Failed to load ${pathsToProcess} ${e.message}`)
256-
}
257-
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 }
258259
}
259-
return { parsed }
260260
}
261261

262262
// 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)