2

I'm trying to build C# solutions automatically using GitLab's CI features. But whenever I try to compile code that contains get/set functions in class properties, the build fails:

Checking out 0c431438 as master...
Skipping Git submodules setup
$ echo "Release build..."
"Release build..."
$ "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" /consoleloggerparameters:ErrorsOnly /maxcpucount /nologo /property:Configuration=Release /verbosity:quiet "%PROJECT_NAME%.sln"
Program.cs(18,38): error CS1043: { oder ; erwartet. [C:\Gitlab\builds\734260e3\0\dransfeld\citest\CITest\CITest.csproj]
ERROR: Job failed: exit status 1

I used Jeff's .gitlab-ci.yml to call msbuild.exe in order to build the solution: https://stackoverflow.com/a/38211190/882746 My runner machine has Visual Studio Community 2017 installed, and building from within the GUI works without a problem.

Why does MSBuild throw syntax errors in the get/set functions when called from cmd? Are there ENV variables missing? I know VS uses some cmd/bat files to set up environments for specific tools.

This StackOverflow Answer suggests some dependencies could be out of date: Compile errors when using C# 7 features in new VS Studio 2017 ASP.NET MVC 5.2.3 project

But I don't use NuGet packets (at least I haven't configured any), the error occurs even in this very simple solution:

using System;

namespace CITest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            TestClass tc = new TestClass();
            Console.ReadLine();
        }
    }

    class TestClass
    {
        private string testVar1;
        public string TestVar1 { get => testVar1; set => testVar1 = value; }

        public TestClass()
        {
            Console.WriteLine("TestClass");
        }
    }
}

Am I simply calling the wrong version of MSBuild? The solution's target version is 4.5 - but there's no 4.5.x folder in C:\Windows\Microsoft.NET\Framework64

Edit: Case closed! For my German colleagues and completeness' sake, here's the output of msbuild.exe run with verbose output. For anyone not speaking German, the output states The project file contains ToolsVersion="15.0". This Toolset is unknown or unavailable. [...] Assuming ToolsVersion="4.0"

Die Projekte in dieser Projektmappe werden nacheinander erstellt. Um eine parallele Erstellung zu ermöglichen, müssen Sie den Schalter "/m" hinzufügen.
Der Buildvorgang wurde am 21.06.2018 17:15:07 gestartet.
Projekt "C:\Gitlab\builds\734260e3\0\dransfeld\citest\CITest.sln" auf Knoten "1" (Standardziele).
ValidateSolutionConfiguration:
  Die Projektmappenkonfiguration "Release|Any CPU" wird erstellt.
Das Projekt "C:\Gitlab\builds\734260e3\0\dransfeld\citest\CITest.sln" (1) erstellt "C:\Gitlab\builds\734260e3\0\dransfe
ld\citest\CITest\CITest.csproj" (2) auf Knoten "1" (Standardziele).
Die Projektdatei enthält ToolsVersion="15.0". Dieses Toolset ist möglicherweise nicht bekannt oder nicht vorhanden. In
diesem Fall können Sie das Problem möglicherweise beheben, indem Sie die entsprechende Version von MSBuild installieren
. Oder für den Build wurde aufgrund der Richtlinie eine bestimmte ToolsVersion erzwungen. Das Projekt wird behandelt, a
ls enthielte es ToolsVersion="4.0". Weitere Informationen finden Sie unter http://go.microsoft.com/fwlink/?LinkId=29133
3.
CoreCompile:
  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Csc.exe /noconfig /nowarn:1701,1702 /nostdlib+ /platform:anycpu32bitp
  referred /errorreport:prompt /warn:4 /define:TRACE /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assem
  blies\Microsoft\Framework\.NETFramework\v4.5.2\Microsoft.CSharp.dll" /reference:"C:\Program Files (x86)\Reference Ass
  emblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblie
  s\Microsoft\Framework\.NETFramework\v4.5.2\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\M
  icrosoft\Framework\.NETFramework\v4.5.2\System.Data.DataSetExtensions.dll" /reference:"C:\Program Files (x86)\Referen
  ce Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.Data.dll" /reference:"C:\Program Files (x86)\Reference
  Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\System.dll" /reference:"C:\Program Files (x86)\Reference Assembli
  es\Microsoft\Framework\.NETFramework\v4.5.2\System.Net.Http.dll" /reference:"C:\Program Files (x86)\Reference Assembl
  ies\Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\
  Microsoft\Framework\.NETFramework\v4.5.2\System.Xml.Linq.dll" /debug:pdbonly /filealign:512 /optimize+ /out:obj\Relea
  se\CITest.exe /subsystemversion:6.00 /target:exe /utf8output Program.cs Properties\AssemblyInfo.cs "C:\Users\Administ
  rator.SKIBAPRO\AppData\Local\Temp\3\.NETFramework,Version=v4.5.2.AssemblyAttributes.cs"
Program.cs(18,38): error CS1043: { oder ; erwartet. [C:\Gitlab\builds\734260e3\0\dransfeld\citest\CITest\CITest.csproj]
Die Erstellung des Projekts "C:\Gitlab\builds\734260e3\0\dransfeld\citest\CITest\CITest.csproj" ist abgeschlossen (Stan
dardziele) -- FEHLER.
NoMad
  • 702
  • 2
  • 11
  • 27

1 Answers1

3

Yes, that's the wrong version of MSBuild to use. I strongly suspect it will be using the C# 5 compiler. The version of the .NET Framework you're targeting doesn't matter here - it's the language version that matters.

Use the version of MSBuild that's shipped with Visual Studio. For example, for me that's:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSBuild.exe

I don't know where it would be on a GitLab CI machine, but quite possibly the same place. You may even be able to rely on the right version just being in the path.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    The tool [vswhere](https://github.com/Microsoft/vswhere) can be used to locate msbuild / visual studio installations. – Mike Zboray Jun 22 '18 at 08:01
  • Spot on! Using VisualStudio's compiler works fine, thanks for your help. I'll also add output from execution of my previous cmdline for compiling with verbose output. It basically states the Toolset version required is 15.0, but the compiler assumes 4.0. – NoMad Jun 22 '18 at 08:36
  • FYI, the "GitLab CI machine" is just a Win2016 server with gitlab-runner service and VS2017 installed, so it uses the default paths any Windows machine would use. – NoMad Jun 22 '18 at 08:50