dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.2k stars 793 forks source link

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

Open Edweis opened 1 year ago

Edweis commented 1 year ago

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

service: my-service

plugins:
  - serverless-offline

provider:
  runtime: nodejs16.x
  stage: dev

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

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

Environment

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.

dnalborczyk commented 1 year ago

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!'));
Edweis commented 1 year ago

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.