Skip to content

Commit

Permalink
Migrate "Line Endings in C# Files"
Browse files Browse the repository at this point in the history
  • Loading branch information
skrysmanski committed Feb 4, 2024
1 parent 2701d32 commit 9a6edcb
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"pagefind",
"parameterless",
"routable",
"RSRP",
"shortcode",
"shortcodes",
"SLAAC",
Expand Down
52 changes: 52 additions & 0 deletions content/articles/dotnet/line-endings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Line Endings in C# Files
topics:
- csharp
- file-format
- visual-studio
- resharper
---

In my projects I prefer to use Linux/Unix line endings (`LF`) for all text files (via `.editorconfig`):

```.editorconfig
[*]
end_of_line = lf
```

So ideally, C# files (`.cs`) should also use `LF` as line ending type.

Unfortunately, this is currently not possible because Linux line endings are not *fully* supported by Visual Studio and ReSharper (my primary editors for C# code).

Without *full* support, when editing C# files you'd sometimes get Linux line endings (as defined by `.editorconfig`) and sometimes Windows line endings (for features that don't support Linux line endings or `.editorconfig`) - resulting in files with **mixed line endings**.

In other editors (like Visual Studio Code) this would not be a problem because they **normalize the line endings** when saving a file. Unfortunately, Visual Studio does *not* do this (see [feature request](https://developercommunity.visualstudio.com/idea/1296741/normalize-line-endings-on-save-according-to-editor.html) which has since been closed *without* actually fixing the problem).

So, for now, the pragmatic approach is to **define line endings for C# files as `CRLF`** (Windows line endings):

```.editorconfig
[*.cs]
end_of_line = crlf
```

{{< tip >}}
To prevent users from accidentally checking in mixed line endings, you should also enforce line endings via `.gitattributes`:

```.gitattributes
*.cs text eol=crlf
```

{{< /tip >}}

## Bugs

This section tracks the various bug reports regarding missing Linux line ending support.

### Visual Studio

None known.

### ReSharper

* [RSRP-478837: Mixed line endings in file templates](https://youtrack.jetbrains.com/issue/RSRP-478837)
* [RSRP-494722: Code insertion (templates) creates wrong line endings](https://youtrack.jetbrains.com/issue/RSRP-494722)
3 changes: 3 additions & 0 deletions content/topics/resharper/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
title: 'ReSharper'
---

0 comments on commit 9a6edcb

Please sign in to comment.