Fix ENDOFLINE Build Error in your Continuous Integration

May 13, 2024#Software Development
Article
Author image.

Scott DePouw, Senior Consultant

You’re being responsible! You want to ensure that merges into your stable source control branch are safe, so you gate any merges with a build, establishing continuous integration to validate changes. You build and test your code, and validate your code style guidelines are followed with dotnet format. You run this locally prior to your pull request and find no errors. You open a pull request, but there’s an error! What gives? Line endings are missing? You have them though!

Validating Your Build Across Multiple Environments

It’s not uncommon for you to develop in one environment or operating system, while your Continuous Integration builds in a different one entirely. I stumbled across this very situation, and error, when developing a GitHub Action for a new application, where my local dotnet format identified no changes, but the build was failing anyway. Let’s delve into why it happened, and how I fixed it. Shout out to Alex Sikilinda’s blog post that inspired this one, that I have visited on multiple occasions, based on the purple link in my searches.

Configurations for Editor and Continuous Integration

My .editorconfig has a snippet defining line ending rules:

# New line preferences
end_of_line = crlf
insert_final_newline = true

My GitHub Action that gates pull requests into our stable branch runs dotnet format to verify that any changes follow the style rules established in .editorconfig. Here’s a snippet of my GitHub Action’s yaml file:

name: Build and Test Application

on:
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - name: Format
      run: dotnet format --verify-no-changes

When this build runs, despite my files having proper line endings, I see a bunch of errors like this:

GitHub Action Failure Example

/home/runner/work/demo/src/Demo.UI/Program.cs(1,15): error ENDOFLINE: Fix end of line marker. Replace 2 characters with '\r\n\r\n'. [/home/runner/work/demo/src/Demo.UI/Demo.UI.csproj]

Note that we use ubuntu-latest, so the line endings are different! I’m developing in Windows. We could change our GitHub Action to use windows-latest, but that’s just a workaround. What if we can’t change the build environment? How do we tell our CI to use the same line endings?

.gitattributes to the Rescue

One line solves the problem! Include a .gitattributes file in the root of your repository’s files, and instruct it to use the line endings you desire. Recall inside our .editorconfig we specified line endings to be crlf. We’ll do the same in .gitattributes:

* text eol=crlf

With this in place, the GitHub action now successfully validates our changes with dotnet format --verify-no-changes.

Resources


Copyright © 2024 NimblePros - All Rights Reserved