Skip to content

util: styleText() fast path ignores changes to util.inspect.colors #67980

Backlog

Description

@beratzenuni

Version

v28.0.0-pre (main @ 9873287), also reproduced on v26.12.1

Platform

Darwin 27.2.0 arm64 (macOS, Apple Silicon)

Subsystem

util

What steps will reproduce the bug?

const util = require('node:util');

// First call with validateStream: false builds the internal style cache
util.styleText('red', 'x', { validateStream: false });

// Customize a color, as documented in "Customizing util.inspect colors"
util.inspect.colors.red = [91, 39];

const a = util.styleText('red', 'x', { validateStream: false });
const b = util.styleText(['red'], 'x', { validateStream: false });

console.log(JSON.stringify(a));
console.log(JSON.stringify(b));
console.log(a === b);

How often does it reproduce? Is there a required condition?

Always. It only happens if styleText() was called with validateStream: false
before util.inspect.colors was modified. If the first call is removed, both
results are the same.

What is the expected behavior? Why is that the expected behavior?

Both calls should return the same string using the updated color:
"\u001b[91mx\u001b[39m"

The docs say util.inspect.colors can be customized globally, and the slow
path and array format already pick up the change.

What do you see instead?

"\u001b[31mx\u001b[39m"
"\u001b[91mx\u001b[39m"
false

Additional information

The fast path (single string format + validateStream: false) builds a cache
of util.inspect.colors once in getStyleCache() and never invalidates it:
https://github.com/nodejs/node/blob/987328738c4/lib/util.js#L144-L156

Is this caching behavior intended for performance? If it is, maybe it should
be documented. If not, I'd be happy to work on a fix.

Activity

  1. bunnysayzz commented on Dec 3, 2026

    @bunnysayzz
    Contributor

    Traced this to lib/util.js: getStyleCache() (lines 144-156) builds the style cache from util.inspect.colors once and never invalidates it, so the fast path at lines 250-258 keeps serving the old codes after a color is customized. The slow path reads the live colors, which is why the array format disagreed.

    Fix in #67989: the fast path now refreshes the cached entry for the requested format from the live colors on each call, and drops the entry if the color was removed so the slow path throws as before. Added a regression test covering the repro from this issue. Verified the fix logic locally against the repro and edge cases (in-place mutation, new color, deleted color); full suite needs CI since there is no node build on this machine.

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