In this article, we are going to learn how to fix an error with the message “While scanning a literal block scalar, found extra spaces in first line”.
Scenario
Recently, we have noticed that ALL our YAML pipelines which has either script / bash tasks in shortcut syntax (an example below) have started failing with the error message “While scanning a literal block scalar, found extra spaces in first line”. The sample screenshot is shown below for your reference.

As shown below, the pipeline has just one script task (shortcut syntax). The script task’s first line is empty with two spaces (you can see two periods. Each period denotes a space)

Usually, the YAML editor show in-line errors if there are any syntax errors. However, in this case, the error is only shown when you try to run the Pipeline as shown in the above screen shot.
Solution
In order to solve this, we can follow the below two simple solutions.
Option1 – Remove the empty line
After removing the empty line, I was able to run the pipeline without any errors. As shown in the below screenshot, the pipeline get queues and runs successfully.

Option2 – Use Regular Syntax (instead of shortcut syntax)
In the above screenshots, I showed you the error for Script tasks. ADO Pipeline was also throwing an error for Bash scripts as well.
Let’s me use Bash Task using regular syntax for this Option to solve the error.
As shown in the below screenshot, use regular task and ensure there are no unnecessary spaces in the script (from line 20 to 22).

Note: We have hundreds of Pipelines with this additional line with few spaces. It was working fine since years. However, it started failing since a few days.
We are fixing this in all the pipelines one after the other. If you have also faced this issue and if you have a better solution that the ones specified on this article, please feel free to provide your feedback in the comment sections.
Thanks for reading the blog. Hope it helped.
Do you like this article? If you want to get more updates about these kind of articles, you can join my Learning Groups
Thank you for taking the time to write this. We had the same issue recently with spaces in our yaml files. Wonder if it has something to do with this: https://docs.microsoft.com/en-us/azure/devops/release-notes/2022/sprint-202-update#extended-yaml-pipelines-templates-can-now-be-passed-context-information-for-stages-jobs-and-deployments
LikeLiked by 1 person