serverless-offline icon indicating copy to clipboard operation
serverless-offline copied to clipboard

Node formating colors missing after upgrading from version 8.8.1 to 9 or above

Open Edweis opened this issue 3 years ago • 2 comments

Bug Report

Using node 16.x, console.log({ hello : 1 }) image

Using serverless-offline@8 it worked the same way: image

Upgrading to serverless-offline@8, serverless-offline@9, or serverless-offline@10, or serverless-offline@11 image

Sample Code

  • file: serverless.yml
service: my-service

plugins:
  - serverless-offline

provider:
  runtime: nodejs16.x
  stage: dev

functions:
  hello:
    events:
      - http:
          method: get
          path: hello
    handler: handler.hello
  • file: handler.js
'use strict'

exports.hello = async function hello() {
  console.log({ hello: 1 })
  return {    body: 'OK'    statusCode: 200,  }
}

Environment

  • serverless version: 3.24.1
  • serverless-offline version: 11.3.0
  • node.js version: v16.18.0
  • OS: Mac OS 13

Possible Solution

I believe serverless-offline is running in its own child_process and the colours are not appropriately passed (like there).

I'll be happy to do a PR is someone can point me here serverless-offline is instantiated.

Edweis avatar Nov 15 '22 01:11 Edweis

thank you for filing the issue @Edweis

I believe serverless-offline is running in its own child_process and the colours are not appropriately passed (like there).

serverless-offline switched to using worker threads from v9 onwards. that said, I didn't even notice that console.log is using colors in the main process. that might be something node.js itself has to fix. if you could do some research on that topic that would be much appreciated.

on a similar note, what I noticed, when developers are using third party color modules (e.g. chalk), that those most of the time don't work out of the box, as the terminal color detection does not seem to work for worker threads. one can force using colors with an environment variable FORCE_COLOR='true'. something we should add to the docs.

example:

// this should work

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');
// this probably does not, unless we set process.env.FORCE_COLOR
// (either by code, or setting the env in the serverless config,
// or in the local dev environment by using the flag `localEnvironment`)

import chalk from 'chalk';

console.log(chalk.blue('Hello world!'));

dnalborczyk avatar Nov 18 '22 13:11 dnalborczyk

Thank you. I had a quick look and I found that colors should be passed into the worker at src/lambda/handler-runner/worker-thread-runner/WorkerThreadRunner.js. However the colors from the workers are not set. My guess is that util.format used in console.xxx() does not detect the TTY (since the worker is technically not a TTY), hence not applying colors.

I might investigate more when I'll have more time.

Edweis avatar Nov 21 '22 11:11 Edweis