Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process.stdout/stderr.isTTY undefined in worker_thread #26946

Closed
mdouglass opened this issue Mar 27, 2019 · 6 comments
Closed

process.stdout/stderr.isTTY undefined in worker_thread #26946

mdouglass opened this issue Mar 27, 2019 · 6 comments
Labels
worker Issues and PRs related to Worker support.

Comments

@mdouglass
Copy link
Contributor

  • Version: v11.12.0
  • Platform: macOS Mojave 10.14.4 (Darwin brewster.local 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64)
  • Subsystem: worker_threads

In worker threads, process.stdout.isTTY and process.stderr.isTTY are undefined.

const { Worker, isMainThread } = require('worker_threads')
if (isMainThread) {
  console.log('mainThread, process.stdout.isTTY', process.stdout.isTTY)
  console.log('mainThread, process.stderr.isTTY', process.stderr.isTTY)
  new Worker(__filename)
} else {
  console.log('workerThread, process.stdout.isTTY', process.stdout.isTTY)
  console.log('workerThread, process.stderr.isTTY', process.stderr.isTTY)
}
$ node ./main.js 
mainThread, process.stdout.isTTY true
mainThread, process.stderr.isTTY true
workerThread, process.stdout.isTTY undefined
workerThread, process.stderr.isTTY undefined

I am not sure if this is intended behavior or not, but I found it while looking into why supports-color reports that color is not supported from worker threads (see chalk/supports-color#97). If I ignore the isTTY flag, then color output does seem to work correctly.

@BridgeAR BridgeAR added the worker Issues and PRs related to Worker support. label Mar 27, 2019
@BridgeAR
Copy link
Member

// cc @addaleax

@bnoordhuis
Copy link
Member

IMO it make sense for .isTTY to be undefined because process.stdout and process.stderr in worker threads don't actually write to the TTY. They postMessage() the data to the main thread which then writes it out on the worker thread's behalf.

We could copy the .isTTY values from the main thread but that breaks the idiom where if (process.stdout.isTTY) guards are used around calls to process.stdout.setRawMode(true) or process.stdout.cursorTo() - and you can't reasonably support those APIs in worker threads because the TTY is a per-process resource.

@silverwind
Copy link
Contributor

Same also applies to child processes, fwiw: #2333.

@pi0
Copy link

pi0 commented May 7, 2019

@silverwind For child_process (cluster and fork) it is possible to pass current process stdio using the third argument ({ stdout: process.stdout }) and it preserves isTTY but for threads, we can't.

As a (temporary) workaround, we can set workerData: { isTTY: process.stdout.isTTY }, when creating Worker and in very beginning of worker script:

const { workerData } = require('worker_threads')

if (workerData.isTTY) {
  for (const stream of ['stdout', 'stdin', 'stderr']) {
    if (process[stream].isTTY === undefined) {
      process[stream].isTTY = true
    }
  }
}

@bnoordhuis
Copy link
Member

I'm going to close this out. It's working as expected as far as I'm concerned and 343ddff adds hints to the documentation.

@sindresorhus
Copy link

We could copy the .isTTY values from the main thread but that breaks the idiom where if (process.stdout.isTTY) guards are used around calls to process.stdout.setRawMode(true) or process.stdout.cursorTo()

@bnoordhuis It would be useful to be able to know whether the main thread is TTY though. When you know you don't need things that are unsupported. For example, it could be useful to decide whether to print ANSI colors from the worker thread. Could Node.js somehow expose to the worker thread whether stdout/stderr in the main thread is TTY?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
worker Issues and PRs related to Worker support.
Projects
None yet
Development

No branches or pull requests

6 participants