SterlingVix
SterlingVix

Reputation: 321

Why is my terminal output not identical when running a yarn script vs its bash equivalent?

**NOTE: I've added updates in order, just keep reading, thanks. :) **

I've been very curious about this -- please see this screenshot of me running:

bash_vs_yarn

Let me start by saying that this is a WIP build in webpack, so no need to tell me that a 31M bundle is less than optimal. :)

But why do I get the colors and the more detailed font with the native command and not when yarn executes the command? It may be relevant: this screen shot is: - Windows 10 - Webstorm terminal - logged in to a docker container running Ubuntu 14.4

Thanks! :)

** UPDATE: --color=always restores color ** As @Charles Duffy suggested, adding --color=always in the yarn script preserved the formatting:

color_always

If anyone has some specialized knowledge to share about what's going on here, I'm in the market to hear it! Thanks!

Upvotes: 3

Views: 1220

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295756

Short(ish) answer: What's actually going on?

The below answer assumes the GNU implementation of ls.

There are a few possibilities at play:

  • Your interactive terminal's options may be modified by a shell alias. Output from type ls will indicate whether this is true.
  • You may have ls --color=auto enabled, either via an alias or via an equivalent environment variable; regardless, this checks whether it's writing directly to a TTY, and only enables color if so.

If output is not direct to a TTY (for instance, if output is being captured by yarn before it's printed), ls --color=auto will not colorize.

To fix this, you can explicitly pass ls --color=always, or its equivalent, simply ls --color. This covers both cases: If you had an alias in use passing --color=auto on your behalf, passing it explicitly means you no longer need the alias. By contrast, if yarn is capturing content rather than passing it straight to the TTY, then --color=always tells ls to ignore isatty() returning false and colorize anyhow.


Background on what the above means:

A "TTY" is, effectively, a terminal. It provides bells and whistles (literally, for the bells) specialized for providing a device that a user is actually typing at. This means it has control sequences for inspecting and modifying cursor location, and -- pertinently for our purposes -- for changing the color with which output is rendered.

A "FIFO" is a pipe -- it moves characters from point A to point B, first-in, first-out. In the case of prog-one | prog-two, the thing that connects those two is a FIFO. It just moves characters, and has no concept of cursor location or colorization or anything else.

If ls tried to put color sequences in its output when that output is intended for any destination other than a terminal, those sequences wouldn't make any sense -- indeed, the very format in which colorization markers need to be printed is determined by the TERM variable specifying the currently active terminal type.

If you run ls --color, then, you're promising ls that its output really will be rendered by a terminal, or (at least) otherwise something that understands the color sequences appropriate to the currently configured TERM.

Upvotes: 1

Related Questions