MSBuild: Build C#7 Project with GitLab
Since I started using C# 7's features like 'Local Functions', 'Inline out variables' or 'expression-bodied members' I ran into a problem when building projects using GitLab (more specifically Gitlab-Runner on Windows).
You can check which C# version your build is using by going:
Project -> Properties -> Build -> Advanced... -> Language Version
The default is to use the latest major C# version, which should be 7 in this case.
But when the Gitlab-Runner calls MSBuild you might see a failed build containing error messages like the following:
error CS1043: { or ; expected
error CS1513: } expected
To avoid this we have to tell MSBuild to use the a compiler that supports C# 7.
Luckily there are NuGet packages available for this.
The 'Compilers' package tells MSBuild to compile according to the selected C# version:
Install-Package Microsoft.Net.Compilers
If you want to use the new Roslyn compiler or have Rosyln references you might need:
Install-Package Microsoft.CodeDom.Providers.DotNetCompilerPlatform
Note: If you're using ValueTuples, you must have a the NuGet package 'System.ValueTuple' installed already, which should get picked up by MSBuild automatically.