`styleText()` doesn't work within a worker thread
Personne n'a encore pris cette issue.
- Langage dominant
- JavaScript
- Étoiles
- 122k
- Forks
- 37.3k
- Merge moyen
- 4 j 2 h
- PR mergées (30 j)
- 283
Description
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
- https://github.com/nodejs/node/blob/main/lib/util.js
- https://github.com/nodejs/node/blob/main/lib/internal/util/colors.js
In chalk, this works out of the box, based on https://github.com/chalk/supports-color
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Reproduisez d’abord l’exemple de worker_threads, puis examinez lib/util.js et lib/internal/util/colors.js, que le rapport identifie comme l’heuristique pertinente. Comparez la détection des couleurs dans le thread principal et le worker, et tenez compte du paramétrage des couleurs du processus parent décrit dans le comportement attendu. C’est terminé lorsque styleText() produit la sortie colorée attendue dans un worker lorsque les couleurs sont activées par le processus parent.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- javascript, nodejs
- Domaine
- backend
- Type d'issue
- Bug
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Activité
- Active
- Clarté
- Plutôt claire
- Accessibilité débutants
- 68/100