Skip to content

Undocumented delete process.env.DEBUG when variable is empty #845

Open

Description

@warriorGod

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.

Activity

  1. added
    bug
    change-major
    on Sep 17, 2021
  2. Qix- commented on Sep 17, 2021

    @Qix-
    Member

    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!

  3. warriorGod commented on Sep 21, 2021

    @warriorGod
    Author

    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.

  4. mosaic4real commented on Jan 7, 2022

    @mosaic4real
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugchange-major

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions