nodejs / nodejs/node

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

Đang mở
#65,766 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

util worker
Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.3k
Merge trung bình
4 ngày 2 giờ
Pull request đã merge (30 ngày)
283

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Trước tiên, hãy tái hiện ví dụ worker_threads, sau đó kiểm tra lib/util.js và lib/internal/util/colors.js, những tệp được báo cáo xác định là heuristic liên quan. So sánh việc phát hiện màu trong main thread và worker, đồng thời xem xét thiết lập màu của tiến trình cha được mô tả trong hành vi mong đợi. Được xem là hoàn tất khi styleText() tạo ra đầu ra có màu như mong đợi trong một worker khi màu được tiến trình cha bật.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript, nodejs
Lĩnh vực
backend
Loại issue
Lỗi
Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
68/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.