nodejs / nodejs/node

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

オープン
#65,766 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

util worker
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず worker_threads の例を再現し、次に、報告で関連するヒューリスティックとして特定されている lib/util.js と lib/internal/util/colors.js を調べます。メインスレッドと worker での色の検出を比較し、期待される動作で説明されている親プロセスの色設定を考慮します。親プロセスによって色が有効になっているときに、worker で styleText() が期待される色付き出力を生成すれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, nodejs
領域
backend
issue の種類
バグ
難易度
3/5
見積もり時間
1〜2日
活発さ
活発
明瞭さ
おおむね明確
初心者へのやさしさ
68/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。