简体   繁体   中英

MSBuild command line with C# 6.0 code fails to build

I am having an issue getting MSBuild to compile c# 6.0 code, it reports a failure when trying to build expression body syntax, etx.

So I've been creating build project like this for years. I have a powershell script that kicks of an msbuild with some parameters:

msbuild.exe _build\build.proj
        /p:Build_Number=1.2.0
        /p:Configuration=QA
        /p:SolutionName=MPGCS-Api.sln
        /ToolsVersion:14.0

I verified that msbuild.exe is coming from C:\\Program Files (x86)\\MSBuild\\14.0\\Bin , i did this by going into that folder and specifying the full physical path to the .proj file. I get the same exact errors.

I have been looking for an example on how to setup a MSBuild project file with C# 6.0 with no luck, so this is my basic setup (this is the build.proj file). If I had to guess I am not importing the correct targets, etc. But I am a little lost. Here is my proj file:

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Root>$(MSBuildStartupDirectory)</Root>
    <NugetExe>$(Root)\_build\lib\nuget\nuget.exe</NugetExe>
    <Build_Number>0.0.0</Build_Number>
    <SolutionName></SolutionName>
    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
    <VisualStudioVersion>14.0</VisualStudioVersion>
  </PropertyGroup>

  <Target Name="Clean">
    <!-- Clean up -->
    <ItemGroup>
      <FilesToDelete Include="$(Root)\_build\Artifacts\**\*.*" />
      <FilesToDelete Include="$(Root)\_build\Published\**\*.*" />
    </ItemGroup>
    <Delete Files="@(FilesToDelete)" ContinueOnError="false" />

    <!-- Ensure directories exists -->
    <MakeDir Directories="$(MSBuildProjectDirectory)\Artifacts" Condition="!Exists('$(MSBuildProjectDirectory)\Artifacts')" />
    <MakeDir Directories="$(MSBuildProjectDirectory)\Published" Condition="!Exists('$(MSBuildProjectDirectory)\Artifacts')" />
  </Target>

  <Target Name="Debug" AfterTargets="Clean">
    <!-- Diagnostics -->
    <Message Text="Diagnostics:"/>
    <Message Text="Build Number:    $(build_number)" />
    <Message Text="Configuration:    $(Configuration)" />
    <Message Text="VisualStudioVersion:    $(VisualStudioVersion)" />
    <Message Text="Project root:    $(Root)" />

    <!-- Restore Nuget Packages -->
    <Message Text="Restoring nuget..."/>
    <Exec Command="$(NugetExe) restore $(Root)\$(SolutionName)" />
  </Target>

  <Target Name="GenerateOctopackAPI" AfterTargets="Debug">
    <ItemGroup>
      <ProjectToBuild Include="$(Root)\MPG.CS.Api\MPG.CS.Api.csproj" />
    </ItemGroup>
    <MSBuild Projects="@(ProjectToBuild)" ContinueOnError="false" Targets="Rebuild" Properties="
      RunOctoPack=true;
      Configuration=$(ProjectBuildMode);
      Platform=AnyCpu;
      TargetFrameworkVersion=$(TargetFrameworkVersion);
      VisualStudioVersion=$(VisualStudioVersion);
      OctoPackPublishPackageToFileShare=$(Root)\_build\Artifacts;
      OctoPackPackageVersion=$(Build_Number);
      OctoPackProjectName=UI;
      OutputPath=bin\$(ProjectBuildMode)" />
    </Target>
</Project>

Here is some info from my "Debug" target, as you can see I am setting the VisualStudioVersion to 14.0 as per some suggestions I read online.

 Diagnostics:
  Build Number:    1.2.0
  Configuration:    QA
  VisualStudioVersion:    14.0
  Project root:    C:\dev\mpg\MPGCS-Api

Here is an example error, it's basically failing on c# 6.0 code, if I were to remove c# 6 code, everything will compile:

TicketType.cs(19,28): error CS1002: ; expected [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model.csproj]
  TicketType.cs(19,44): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model.cs proj]
  TicketStatus.cs(16,36): error CS1002: ; expected [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model.csproj]
  TicketStatus.cs(16,53): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model. csproj]
  TicketStatus.cs(16,82): error CS1519: Invalid token '(' in class, struct, or interface member declaration [C:\dev\mpg\MPGCS-Api\MPG.CS.Model\MPG.CS.Model. csproj]

Here is a line it's failing on (C# 6 code):

public bool IsAbbreviated  => (Title.ToLower() == "open" || Title.ToLower() == "closed");

I'm at a loss, I thought just using the proper msbuild.exe will allow me to take advantage of c# 6.0. Any help would be greatly appreciated.

If you're running the powershell script on a build server, separate from your dev environment, you will need to make sure that the machine itself can handle C# 6.0. Since it uses a different compiler all together, it wouldn't work if VS 2015 wasn't installed. Alternately, you can add the Microsoft.Net.Compilers nuget package as a dependency to allow VS 2012 and VS 2013 to compile it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM