nodejs / nodejs/node

`styleText()` doesn't work within a worker thread

Offen
#65,766 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

util worker
Vorherrschende Sprache
JavaScript
Sterne
122k
Forks
37.3k
Ø Merge
4 T. 2 Std.
Gemergte PRs (30 T.)
283

Beschreibung

Version

26.8.1

Platform
Darwin MacBook-Pro-de-Sebastien.local 24.6.0 Darwin Kernel Version 24.6.0: Tue Apr 21 20:16:56 PDT 2026; root:xnu-11417.140.69.710.16~1/RELEASE_ARM64_T6030 arm64
Subsystem

No response

What steps will reproduce the bug?
const {Worker} = require('node:worker_threads');
const {styleText} = require('node:util');
const tty = require('node:tty');

console.log('MAIN', {
  isTTY: process.stdout.isTTY,
  isatty: tty.isatty(1),
  styled: JSON.stringify(styleText('red', 'hello')),
});

await new Promise((resolve, reject) => {
  const worker = new Worker(`
    const {parentPort} = require('node:worker_threads');
    const {styleText} = require('node:util');
    const tty = require('node:tty');

    parentPort.postMessage({
      isTTY: process.stdout.isTTY,
      isatty: tty.isatty(1),
      styled: JSON.stringify(styleText('red', 'hello')),
    });
  `, {eval: true});

  worker.on('message', msg => {
    console.log('WORKER', msg);
    resolve();
  });

  worker.on('error', reject);
});

Output:

MAIN {
  isTTY: true,
  isatty: true,
  styled: '"\\u001b[31mhello\\u001b[39m"'
}

WORKER {
  isTTY: undefined,
  isatty: true,
  styled: '"hello"'
}

Even simpler repro:

const { Worker } = require('node:worker_threads');
const { styleText } = require('node:util');

console.log('main:', styleText('red', 'hello'));

new Worker(`
  const { styleText } = require('node:util');

  console.log('worker:', styleText('red', 'hello'));
`, { eval: true });
How often does it reproduce? Is there a required condition?

constantly

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

I think it's reasonable to expect styleText() to also work in worker threads by default if the parent has colors enabled. I'd rather have an opt-out than an opt-in through worker thread config/env.

What do you see instead?

The worker thread

Additional information

From what I understand, the heuristic used in styleText() is too limited and could be improved to support colors in worker threads better

In chalk, this works out of the box, based on https://github.com/chalk/supports-color

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduzieren Sie zuerst das worker_threads-Beispiel und untersuchen Sie anschließend lib/util.js und lib/internal/util/colors.js, die der Bericht als relevante Heuristik identifiziert. Vergleichen Sie die Farberkennung im Hauptthread und im Worker und berücksichtigen Sie die in der erwarteten Verhaltensweise beschriebene Farbeinstellung des übergeordneten Prozesses. Als erledigt gilt die Aufgabe, wenn styleText() in einem Worker die erwartete farbige Ausgabe erzeugt, sobald Farben durch den übergeordneten Prozess aktiviert sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, nodejs
Bereich
backend
Issue-Typ
Bug
Schwierigkeit
3/5
Geschätzter Aufwand
1-2 Tage
Aktivitätsstatus
Aktiv
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
68/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.