Undocumented ```delete process.env.DEBUG``` when variable is empty
I would just like to point out to the fact that when required, debug package will remove the DEBUG env variable if empty.
I think it would be nice if this is mentioned in the readme, as I feel its a non obvious behavior.
The use case I noticed this on is:
command:
npm install dotenv
DEBUG= npx mocha --require bootstrap.js
files:
.env
DEBUG=*
bootstrap.js
console.log(process.env.DEBUG);
require('dotenv').config();
console.log(process.env.DEBUG);
output:
undefined // we lost the DEBUG env already
'*' // we get .env file DEBUG setting, but we should have gotten '', because dotenv never overrides set env variables (* actually after version 4 I believe)
problem:
mocha uses the debug package which deletes the env variable. Our user code in bootstrap.js then has no way of knowing if DEBUG was set to an empty string or not supplied. Therefore we get full debug info instead of none.
More general - any code used before using process.env.DEBUG might delete the env variable through just depending on the debug package.
solution
I propose we either not delete the DEBUG variable or have a section in documentation to explain and suggest maybe using DEBUG=, to disable debug logging.
I agree, this is weird behavior. It's being deleted in the method that sets namespaces, which I believe is called implicitly upon importing debug. I'll mark it as a bug.
For reference, it's located here:
https://github.com/visionmedia/debug/blob/d177f2bc36d3b8b8e9b1b006727ef5e04f98eac7/src/node.js#L209
Unfortunately, the widespread use of debug means changing this would be a breaking change - at least, that's how I'd like to treat it.
The pressure on debug@5 is growing and I'll probably have to address it soon. I've already been discussing a few things with other maintainers about the future of debug and what it looks like, and I imagine v5 will be a complete rewrite at this point.
For now, this might work as an include stub (it's untested, so tread lightly).
// debug-patch.js
// XXX Remove when https://github.com/visionmedia/debug/issues/845 is resolved.
// Must happen before any other `require('debug')` in the application.
const oldEnv = process.env.DEBUG;
const debug = require('debug');
debug.save = namespaces => {
process.env.DEBUG = namespaces || '';
};
process.env.DEBUG = oldEnv;
module.exports = debug;
I realize this isn't ideal but I have to be very cautious with which manner I break people.
Sorry for the headache :/ But thanks for reporting this. Not often a real defect comes up here!
Great, thank you for looking into this and keeping the issue open. I think for my use case the patch won't work unfortunately (I start mocha first, which causes an issue, then my user code), but I got around it by using DEBUG=, env value.
GitHub Enterprise Server only: This is all possible on GitHub Enterprise through GitHub Connect. It may take up to an hour to refresh the alerts and make them visible. After waiting a reasonable amount of time, if you are still not seeing the yellow bar in the Dependency Graph, you may want to contact your administrator. In the mean time, to move along with the course, we'll give you a hint - the recommended upgraded version is 2.6.9.