Skip to content

fix: route _log/_debug to stderr, simplify LINE regex, add troubleshooting docs#938

Closed
sudip-kumar-prasad wants to merge 1 commit intomotdotla:masterfrom
sudip-kumar-prasad:fix-log-stderr-and-regex-cleanup
Closed

fix: route _log/_debug to stderr, simplify LINE regex, add troubleshooting docs#938
sudip-kumar-prasad wants to merge 1 commit intomotdotla:masterfrom
sudip-kumar-prasad:fix-log-stderr-and-regex-cleanup

Conversation

@sudip-kumar-prasad
Copy link
Contributor

Summary

This PR addresses three independent improvements to the dotenv codebase.


Fix 1: Route _log and _debug output to stderr

Problem: _log() and _debug() both used console.log, which writes to stdout. This causes diagnostic/status messages to pollute the standard output stream, breaking common shell patterns like:

node app.js | jq         # dotenv tip messages get mixed into the JSON stream
node app.js > output.txt # log messages end up written to the file

…eshooting docs

- Route _log() and _debug() output to console.error (stderr) instead of
  console.log (stdout). Diagnostic messages pollute stdout and break shell
  piping (e.g. node app.js | jq would include log output in the data stream).

- Simplify the LINE regex: replace redundant (?:^|^) / (?:$|$) alternations
  with plain ^ / $ anchors. The original groups were logically identical to
  their simplified form and served no functional purpose.

- Add a Troubleshooting section to README.md covering two very common user
  pain points:
  1. Existing env vars not being overwritten (by design) and how to use
     { override: true } to bypass this.
  2. .env file not loading and how to enable debug mode to diagnose it.

- Update all test stubs in test-config.js, test-config-vault.js, and
  test-populate.js from sinon.stub(console, 'log') to
  sinon.stub(console, 'error') to match the new stderr output.

All 194 tests pass.
Copilot AI review requested due to automatic review settings February 25, 2026 18:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves dotenv’s runtime diagnostics and documentation by ensuring status/debug output doesn’t pollute stdout, simplifying the line-parsing regex, and adding end-user troubleshooting guidance.

Changes:

  • Route _log() / _debug() output to stderr (via console.error) to keep stdout clean for piping/redirects.
  • Simplify the LINE regex used for parsing .env content by removing redundant anchors.
  • Add a new “Troubleshooting” section to the README with common resolution steps.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/test-populate.js Update tests to stub console.error (matching new stderr logging behavior).
tests/test-config.js Update logging-related tests to stub console.error instead of console.log.
tests/test-config-vault.js Update vault-config tests to stub console.error for warnings/logs.
lib/main.js Switch _debug/_log to console.error and simplify the LINE parsing regex.
README.md Add troubleshooting guidance for common dotenv configuration/loading issues.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

return supportsAnsi() ? `\x1b[2m${text}\x1b[0m` : text
}

const LINE = /(?:^|^)\s*(?:export\s+)?([\w.-]+)(?:\s*=\s*?|:\s+?)(\s*'(?:\\'|[^'])*'|\s*"(?:\\"|[^"])*"|\s*`(?:\\`|[^`])*`|[^#\r\n]+)?\s*(?:#.*)?(?:$|$)/mg
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like this part of the regex - because i like that it looks like a cat. i've mentioned this before and plan to keep it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(?:^|^)

@motdotla
Copy link
Owner

not significant enough of a change. doesn't add much value and could break things given historical artifact.

@motdotla motdotla closed this Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants