fix: enabled returns undefined instead of false when DEBUG env var is absent#1039
Open
JSap0914 wants to merge 1 commit into
Open
fix: enabled returns undefined instead of false when DEBUG env var is absent#1039JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
When enable() is called with a non-string argument (including undefined, which happens at startup when process.env.DEBUG is absent), it assigns createDebug.namespaces = undefined. The per-instance enabled getter caches results using namespacesCache, which is initialised to undefined in the closure. The cache-miss guard if (namespacesCache !== createDebug.namespaces) therefore evaluates to false on the very first access (undefined !== undefined is false), so createDebug.enabled(namespace) is never invoked, and enabledCache stays undefined. Every subsequent read of instance.enabled returns undefined instead of false until a second, string-valued enable() call is made. Fix: normalise createDebug.namespaces to an empty string whenever the argument is not a string, consistent with how the split step already treats non-string input.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When
DEBUGis not set (orenable()is called with a non-string value such asundefined),instance.enabledincorrectly returnsundefinedinstead offalse.Root cause
Inside
enable(),createDebug.namespacesis assigned the raw argument:Each debug instance caches its enabled state with:
Because both
namespacesCacheandcreateDebug.namespacesstart asundefined, the cache-miss guard evaluates tofalseon the very first access,createDebug.enabled()is never called, andenabledCacheremainsundefined.Observable effect
The common guard
if (!debug.enabled) return;still works because!undefined === true, but strict comparisons and TypeScript type-checks fail.Fix
Normalise
createDebug.namespacesto an empty string whenenable()receives a non-string argument, consistent with how thesplitstep already handles non-string input:This ensures the cache-miss guard fires on the first read after startup, so
enabledCacheis properly initialised.Test
A new failing test is added that re-requires
debugwith noDEBUGenv var and assertstypeof log.enabled === 'boolean'andlog.enabled === false.