`styleText()` doesn't work within a worker thread
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 68/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, nodejs
- Lĩnh vực
- backend
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.
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
- 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
- Ngôn ngữ chính
- JavaScript
- Star
- 122k
- Fork
- 37.4k
- Merge trung bình
- 4 ngày 3 giờ
- Pull request đã merge (30 ngày)
- 273
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nodejs/node
-
doc
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
build
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
feature request
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Issue tương tự
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 76/100
avniproject/avni-client#2135 ·
-
automated broken-link
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
-
agent/security hive/hosted-available-lke648397-260827-5n31 security
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
babalae/bettergi-scripts-list#3674 ·
-
A-Release-Notes C-Editing D-Modest S-Ready-For-Implementation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
bevyengine/bevy-website#2595 ·