From 87fd887e31b913d334ef820f27737d80b28389ee Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Mon, 19 Feb 2024 19:05:23 -0800 Subject: [PATCH 1/3] do not check if exists --- lib/main.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/lib/main.js b/lib/main.js index 1a99afdf..7594dc2d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -218,14 +218,10 @@ function configDotenv (options) { let optionPathsThatExist = [] if (options && options.path) { if (!Array.isArray(options.path)) { - if (fs.existsSync(options.path)) { - optionPathsThatExist = [_resolveHome(options.path)] - } + optionPathsThatExist = [_resolveHome(options.path)] } else { for (const filepath of options.path) { - if (fs.existsSync(filepath)) { - optionPathsThatExist.push(_resolveHome(filepath)) - } + optionPathsThatExist.push(_resolveHome(filepath)) } } From 249e5a64f612d59aaac74f594f951024490d9765 Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Mon, 19 Feb 2024 19:27:17 -0800 Subject: [PATCH 2/3] adjust logic to support tests --- CHANGELOG.md | 8 ++++++- lib/main.js | 54 ++++++++++++++++++++++---------------------- tests/test-config.js | 4 ---- 3 files changed, 34 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b6e4d85..66642eda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,13 @@ 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. -## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.4.4...master) +## [Unreleased](https://github.com/motdotla/dotenv/compare/v16.4.5...master) + +## [16.4.5](https://github.com/motdotla/dotenv/compare/v16.4.4...v16.4.5) (2024-02-19) + +### Changed + +- 🐞 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) ## [16.4.4](https://github.com/motdotla/dotenv/compare/v16.4.3...v16.4.4) (2024-02-13) diff --git a/lib/main.js b/lib/main.js index 7594dc2d..314f49ad 100644 --- a/lib/main.js +++ b/lib/main.js @@ -215,48 +215,48 @@ function configDotenv (options) { } } - let optionPathsThatExist = [] + let optionPaths = [dotenvPath] // default, look for .env if (options && options.path) { if (!Array.isArray(options.path)) { - optionPathsThatExist = [_resolveHome(options.path)] + optionPaths = [_resolveHome(options.path)] } else { + optionPaths = [] // reset default for (const filepath of options.path) { - optionPathsThatExist.push(_resolveHome(filepath)) + optionPaths.push(_resolveHome(filepath)) } } - - if (!optionPathsThatExist.length) { - optionPathsThatExist = [dotenvPath] - } } - // If we have options.path, and it had valid paths, use them. Else fall back to .env - const pathsToProcess = optionPathsThatExist.length ? optionPathsThatExist : [dotenvPath] - // Build the parsed data in a temporary object (because we need to return it). Once we have the final // parsed data, we will combine it with process.env (or options.processEnv if provided). - - const parsed = {} - try { - for (const path of pathsToProcess) { + let lastError + const parsedAll = {} + for (const path of optionPaths) { + try { // Specifying an encoding returns a string instead of a buffer - const singleFileParsed = DotenvModule.parse(fs.readFileSync(path, { encoding })) - DotenvModule.populate(parsed, singleFileParsed, options) - } + const parsed = DotenvModule.parse(fs.readFileSync(path, { encoding })) - let processEnv = process.env - if (options && options.processEnv != null) { - processEnv = options.processEnv + DotenvModule.populate(parsedAll, parsed, options) + } catch (e) { + if (debug) { + _debug(`Failed to load ${path} ${e.message}`) + } + lastError = e } + } - DotenvModule.populate(processEnv, parsed, options) - } catch (e) { - if (debug) { - _debug(`Failed to load ${pathsToProcess} ${e.message}`) - } - return { error: e } + let processEnv = process.env + if (options && options.processEnv != null) { + processEnv = options.processEnv + } + + DotenvModule.populate(processEnv, parsedAll, options) + + if (lastError) { + return { parsed: parsedAll, error: lastError } + } else { + return { parsed: parsedAll } } - return { parsed } } // Populates process.env from .env file diff --git a/tests/test-config.js b/tests/test-config.js index a3780a6d..029d29ba 100644 --- a/tests/test-config.js +++ b/tests/test-config.js @@ -88,16 +88,12 @@ t.test('takes URL for path option', ct => { }) t.test('takes option for path along with home directory char ~', ct => { - const existsSyncStub = sinon.stub(fs, 'existsSync').returns(true) const readFileSyncStub = sinon.stub(fs, 'readFileSync').returns('test=foo') const mockedHomedir = '/Users/dummy' const homedirStub = sinon.stub(os, 'homedir').returns(mockedHomedir) const testPath = '~/.env' dotenv.config({ path: testPath }) - ct.equal(existsSyncStub.args[0][0], testPath) - ct.ok(existsSyncStub.called) - ct.equal(readFileSyncStub.args[0][0], path.join(mockedHomedir, '.env')) ct.ok(homedirStub.called) From 353342048cae3621f8abb3a23e00af880ab2c69f Mon Sep 17 00:00:00 2001 From: Scott Motte Date: Mon, 19 Feb 2024 19:29:15 -0800 Subject: [PATCH 3/3] =?UTF-8?q?changelog=20=F0=9F=AA=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 66642eda..e35152ae 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file. See [standa ### Changed -- 🐞 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) +- 🐞 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) ## [16.4.4](https://github.com/motdotla/dotenv/compare/v16.4.3...v16.4.4) (2024-02-13)