Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,19 +156,19 @@ function loadEnvFiles (options) {
const resolvedPath = path.resolve(process.cwd(), resolveHome(filepath))
try {
const parsed = dotenv.parse(fs.readFileSync(resolvedPath, { encoding: options.encoding }), { fast: options.fast })
dotenv.populate(parsedAll, parsed, populateOptions)
dotenv.populate(parsedAll, parsed, populateOptions, console.error)
loadedPaths.push(filepath)
} catch (e) {
if (options.debug) {
console.log(`┆ failed to load ${filepath} ${e.message}`)
console.error(`┆ failed to load ${filepath} ${e.message}`)
}
if (!(options.defaultPath && e.code === 'ENOENT')) {
throw e
}
}
}

const injected = dotenv.populate(process.env, parsedAll, populateOptions)
const injected = dotenv.populate(process.env, parsedAll, populateOptions, console.error)
return { injected, loadedPaths }
}

Expand Down
10 changes: 5 additions & 5 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ function parse (src, options) {
return parseRegex(src)
}

function _debug (message) {
console.log(`┆ ${message}`)
function _debug (message, output = console.log) {
output(`┆ ${message}`)
}

function _log (message) {
Expand Down Expand Up @@ -359,7 +359,7 @@ function config (options) {
}

// Populate process.env with parsed values
function populate (processEnv, parsed, options = {}) {
function populate (processEnv, parsed, options = {}, debugOutput) {
const debug = parseBoolean(options && options.debug)
const override = parseBoolean(options && options.override)
const populated = {}
Expand All @@ -380,9 +380,9 @@ function populate (processEnv, parsed, options = {}) {

if (debug) {
if (override === true) {
_debug(`"${key}" is already defined and WAS overwritten`)
_debug(`"${key}" is already defined and WAS overwritten`, debugOutput)
} else {
_debug(`"${key}" is already defined and was NOT overwritten`)
_debug(`"${key}" is already defined and was NOT overwritten`, debugOutput)
}
}
} else {
Expand Down
19 changes: 19 additions & 0 deletions tests/test-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ t.test('help before the command still shows dotenv help', ct => {
ct.end()
})

for (const testCase of [
{ name: 'an existing environment variable', files: { '.env': 'BASIC=file\n' }, diagnostic: /"BASIC" is already defined and was NOT overwritten/ },
{ name: 'a missing default file', files: {}, diagnostic: /failed to load \.env/ }
]) {
t.test(`CLI debug logging for ${testCase.name} goes to stderr`, ct => {
const cwd = ct.testdir(testCase.files)
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.toUpperCase().startsWith('DOTENV_')))
const result = cp.spawnSync(process.execPath, [
path.resolve(__dirname, '../dist/index.cjs'), 'run', '--debug', '--quiet',
process.execPath, '-e', 'console.log(JSON.stringify({ basic: process.env.BASIC }))'
], { cwd, encoding: 'utf8', env: { ...env, BASIC: 'original' } })

ct.equal(result.status, 0, result.stderr)
ct.equal(result.stdout, '{"basic":"original"}\n')
ct.match(result.stderr, testCase.diagnostic)
ct.end()
})
}

const fileSelections = [
['-f', 'tests/.env.local,tests/.env'],
['--file', 'tests/.env.local,tests/.env'],
Expand Down