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

do not check if exists #814

Merged
merged 3 commits into from Feb 20, 2024
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
8 changes: 7 additions & 1 deletion CHANGELOG.md
Expand Up @@ -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. 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)

Expand Down
58 changes: 27 additions & 31 deletions lib/main.js
Expand Up @@ -215,52 +215,48 @@ function configDotenv (options) {
}
}

let optionPathsThatExist = []
let optionPaths = [dotenvPath] // default, look for .env
if (options && options.path) {
if (!Array.isArray(options.path)) {
if (fs.existsSync(options.path)) {
optionPathsThatExist = [_resolveHome(options.path)]
}
optionPaths = [_resolveHome(options.path)]
} else {
optionPaths = [] // reset default
for (const filepath of options.path) {
if (fs.existsSync(filepath)) {
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
Expand Down
4 changes: 0 additions & 4 deletions tests/test-config.js
Expand Up @@ -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)

Expand Down