Simple redirecting can include too much

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the bash category.

Last Updated: 2024-04-13

I had a yarn script to generate JSON events.

It was executed with:

$ yarn generateEvents

And produced the following output

yarn run v1.17.3
$ tsc && node ./lib/generateEvents.js
  event: 'paymentComplete',
  id: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  payload: {
    amount: '129',
    coreTransactionData: {
      customerId: undefined,
      accountId: undefined,
      referenceId: undefined,
      lawyerId: undefined,
      stripeId: 'I-ANNHB1AEES4T',
      category: 'membership',
      productId: undefined
    customerId: null,
    eventCreatedAt: '2020-03-09T17:40:31.356Z',
    paymentReceivedAt: 2020-02-07T12:00:00.000Z
  revision: '1.0.0'

I wanted to capture all these events into a file to be send to the client. So I ran this:

yarn generateEvents > events.json

Later, when I tried to input this JSON, it failed, saying the JSON was invalidate. Why did this happen (hint: it's a silly reason)

Answer: Because the output also included the precursor logging crap like yarn run v1.17.3 output. This output was shown despite me never explicitly telling my program to print that info - i.e. it was automatically added by the programming environment.

I think this is a design flaw in this version of yarn, since it breaks simple redirection. But that's besides the point. I, as a consumer of other people's code, should be prepared for this and inspect my output before redirecting it.