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

Aperta
#65,766 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
68/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, nodejs
Ambito
backend

Direzione di ricerca

Riproduci prima l'esempio di worker_threads, quindi esamina lib/util.js e lib/internal/util/colors.js, che il report identifica come l'euristica rilevante. Confronta il rilevamento dei colori nel thread principale e nel worker e considera l'impostazione dei colori del processo padre descritta nel comportamento previsto. Il lavoro è completo quando styleText() produce l'output colorato previsto in un worker quando i colori sono abilitati dal processo padre.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

util worker
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

Lingua principale
JavaScript
Stelle
122k
Fork
37.4k
Merge medio
4g 3h
PR unite (30g)
273

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nodejs/node

Tutte le issue di nodejs/node

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.