Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Does 'debug' cache the DEBUG environment variable somewhere? Getting no output after changing namespace and DEBUG env var. #901

Open

Description

@KerimG

I am using 'debug' with TypeScript in an Electron project like so:

import debug from 'debug';
const log = debug('foo');

export default async function myFunction(name: string) {
  log('calling myFunction');
  ...
  log('successfully returning myFunction');
  return someVariable;
}

in my shell (zsh) I set the DEBUG env var like so export DEBUG=foo and then started running my application. Everything worked fine. Eventually, I changed the namespace from foo to bar and now no matter what I do, I cannot get any output. I tried resetting DEBUG, I restarted my shell and set the environment variable again with export DEBUG=bar, I tried using the [cross-env]{https://www.npmjs.com/package/cross-env} to set it like so:

"scripts": {
    "start": "tsc && cross-env DEBUG=bar NODE_ENV=development electron-forge start",
    ...
}

but nothing ever shows up.
I tried

import debug from 'debug';
const log = debug('bar');

console.log("DEBUG", process.env.DEBUG);

export default async function myFunction(name: string) {
  log('calling myFunction');
  ...
  log('successfully returning myFunction');
  return someVariable;
}

and the console.log output is indeed: DEBUG bar.

The only way I can get it to work now is by doing:

import debug from 'debug';
const log = debug('bar');
log.enabled = true;

The moment I change const log = debug('bar'); back to const log = debug('foo'); , I get output messages for the foo namespace no matter what's in the DEBUG env var.

Any idea what might be happening here?

Activity

  1. robigan commented on Jul 17, 2022

    @robigan

    Having a similar issue here, but just to help you, maybe try running export on it's own to print a debug output of all environment variables that will be exported to children processes

  2. Qix- commented on Jul 17, 2022

    @Qix-
    Member

    No idea, this seems like an environment issue to be honest. DEBUG is checked once at the first import of debug and cached.

  3. algernon commented on Jul 26, 2022

    @algernon

    I've been bitten by this the other day too. Turns out, DEBUG is cached in LocalStorage, and in subsequent runs, LocalStorage will override the environment. You can do localStorage.debug = null in a dev console to reset it.

  4. Qix- commented on Jul 27, 2022

    @Qix-
    Member

    @algernon that's a detail of the browser version. I believe OP is using the node side of things.

  5. abenhamdine commented on Aug 24, 2022

    @abenhamdine
    Contributor

    Same issue with Remix today, process.env shows the correct expected DEBUG string, but debugnever outputs anything.
    Very weird.

  6. GodBleak commented on Sep 18, 2022

    @GodBleak

    Are/were you using regex in your code somewhere? possibly passing it into debug?
    I think this issue was introduced with toNamespace() in 4.1.0

    When I had DEBUG = *, I saw express was able to debug fine. I noticed that they're using debug 2.6.9, so I went through the versions and found that debug works as expected until 4.1.0. Seeing that toNamespace converts regex into a namespace and realizing I was potentially passing regex into debug, I remembered Javascript's regex is weird. commenting out my regex returned the output in 4.3.4.

    However, at that point, the universe decided I was done, as uncommenting it did not reintroduce the issue and no matter what I do now I can't reproduce it again, I've rolled everything back to exactly as it was when I had the problem and it's just gone 🤷

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions