A stupid bug ruined my afternoon...
Tags: debugging, rant, coding, bugs
Published: 2020-02-25 22:48:05

---
title: A stupid bug ruined my afternoon...
published: true
cover_image: https://media.giphy.com/media/E2USislQIlsfm/giphy.gif
description: there is more than meets the eye - how to solve an apparently invisible bug
tags: #debugging, #rant, #coding, #bugs
---


As I wrote in [this](
https://dev.to/dvddpl/testing-agaist-different-environment-variables-and-deployment-stages-4hpl) and [this](https://dev.to/dvddpl/where-do-you-keep-credentials-for-your-lambda-functions-5dno) articles, we try to **not use hardcoded variables and credentials** and try to reduce at a minimum the .env files ( which we never push anyway).

The problem still, is that many times we need to rely on environment variables to run some tests or to bundle in the frontend. 
In such cases, I need to somehow retrieve some configurations from the Exports/Outputs in **CloudFormation**, or from **SecretsManager / Parameter Store** and save them to a _.env_ file that is being loaded by my tests or by the package bundler.

> Honestly - I still haven't found a satisfying solution for this ( if you have some suggestions, feel free to drop it in the comments)

This is what happened to me last week, I wanted to share this, as a reminder for my future self and for all the devs old and new:  **Evil is in the details.** 

During the first phases of the project, we had a bunch of vars in the .env file.  Then we decided to dynamically generate that file after the deployment of the serverless stack was completed, filling it with the values exported from Cloud Formation.

Just a matter of a 

```
npm script postdeploy: generateEnvs
```

and a very short node Js file which uses AWS-SDK and grabs some values from our Cloud Formation Stacks and writes them to file.

I run the script, and the file is there, with all the necessary stuff. 
Therefore I bundle my front-end app using that file ... and nothing works. All the env variables are _undefined._

![what? how? why?](https://media.giphy.com/media/iAYupOdWXQy5a4nVGk/giphy.gif)

At first, I thought I might have written the file in the wrong way, maybe some space, some typo..  but no: _everything looked exactly as it should_.

After many attempts, I also tried back and forth the original file and the generated - identical - one, but no. I was not working. I even asked a colleague for another pair of eyes, but he agreed:

- they **looked** identical
- they **seemed** exactly the same

> they looked...  but for some reason, they were NOT!

these words bounced in my brain until I had an idea: **let´s see the whitespace and non-printable characters!!**

And...  yes. the two files were not the same anymore.

![what...](https://media.giphy.com/media/YVPwi7L2izTJS/giphy.gif)
The working one was using `\n`  (_newline_) while in the generated one I was using a `\r` (_carriage return_) (and all the variables where actually inlined...)
I don´t know why I instinctively use that and not a new line (maybe because I actually learned typing on a typewriter...), 
![carriage return old typewriter](https://media.giphy.com/media/l0HlLWj5e25b5Ky76/giphy.gif)
but I did not know it could make such a difference - as soon as I changed my script to use the new line my script started working as intended.

Quite relieving even though quite disappointing having wasted so much time in something so _stupid_.  


![throw computer](https://media.giphy.com/media/lNubxCPAPvSUw/giphy.gif)

Do you know what is the difference? [Here](https://stackoverflow.com/a/12747850) are [some](https://www.quora.com/What-is-the-difference-between-newline-character-and-carriage-return) [links](https://en.wikipedia.org/wiki/Carriage_return)