The Crafting Strider

Raman But-Husaim’s personal webblog. Ideas, tech articles born through the life of a software engineer

Visual Studio Code, line endings and a bash script

As part of the home project that I’m working on we actively use Azure DevOps to store the source code and run quite simple CI. Recently I’ve customized the pipeline to run API tests as part of the build process. In order to do this I had to create a bunch of bash-scripts and faced an issue with line endings for such files.

Visual Studio Code is an awesome editor created by Microsoft that could be used to work with almost any language in a more or less comfortable manner. Personally I’m using it for everything except dotnet as experience with JetBrains Rider is a way-way better.

Overview

As part of the pipeline I run integration tests on the dockerized version of the application. API tests are integration ones in essence and we launch a database and wiremock containers as well to make the service work correctly. It is quite convenient to use docker-compose for such tasks however there is one important thing - docker ensures that the container is running, not that is ready. See docs for more info. This means that applications developers should know better how measure this readiness for their services.

In our case a service is considered fully functional when the health endpoint returns 200. Only when service is up and running we could start testing it with xUnit. Here is where a bash script comes into play.

The Issue

A bash-script is quite straightforward by its nature however it took me a couple of hours to write it and verify. For curious the health check fragment of the script looks something like this:

echo "waiting for the API service to start"
ATTEMPT=1
ATTEMPT_LIMIT=30
while [ "$(curl -sL -o /dev/null -w ''%{http_code}'' localhost:7000/health)" != "200" ]
do
    if [ "$ATTEMPT" -gt "$ATTEMPT_LIMIT" ]; then
        echo "service is unhealthy, number of attempts=$ATTEMPT is exceeded"
        exit 1
    fi

    echo "attempt=$ATTEMPT, sleeping for 1 second"
    sleep 1
    ATTEMPT=$[$ATTEMPT + 1]
done

echo "service is healthy, attempts=$ATTEMPT"

Prior to commit the changes and verify them in the cloud I’ve decided to test the script locally. Due to the local zsh-by-default setup I’ve launched the following command bash test-health-check.sh. The output was test-health-check.sh: line 16: syntax error: unexpected end of file.

Interesting. Based on the previous experience I know that VSCode displays line endings in the status bar for convenience. In my case it was CRLF that made bash to display an error in the console.

VSCode Line Ending

The strange thing was that when I’ve tried to change it to LF and save the file it was automatically returned to CRLF. After some search I’ve found the issue inside VSCode repository regarding CRLF, end-of-line and its configuration. However this setting affects all files disregards their extension that is not acceptable for our multi-OS dev environments.

Solution

Given some more though on the topic the solution came to my mind. I’m fan of editorconfig and try to use it on every project that I’m working on. VSCode knows how to work with editorconfig properly with the help of official extension. The solution is to extend the .editorconfig file in the folder by adding the following lines:

[*.sh]

end_of_line = lf

Such config tells VSCode to automatically change the line endings in shell files to LF on save.