36

I have a project that the framework is targeting .NET Framework 4.6.1, as part of the continuous integration process on the tfs we created a Build Solution task to ensure that the code compiles correctly.
Now the TFS server has the latest version of the .Net Famework 4.6.2. On the register this is the value for the Release key of the framework

On all other OS versions: 394806 => .NET Framework 4.6.2

But when the build runs it comes with this error:

Error CS1056: Unexpected character '$'

I don't want to replace the string interpolation with the string.Format to solve this issue, please provide another workaround to solve it.

Do I need to install something else on the TFS server?

VMAtm
  • 26,645
  • 17
  • 75
  • 107
Heinrich
  • 636
  • 2
  • 7
  • 21

7 Answers7

29

The problem can be fixed installing a Nuget package Microsoft.Net.Compilers. Below is the link of my highlighted answer: Project builds fine with Visual Studio but fails from the command line

That feature is a syntactic sugar for C#6, try to install the latest version of the framework 4.6.2 https://www.microsoft.com/en-us/download/details.aspx?id=53345

Then go to your Project properties and change on the Application option on Target framework to point to the latest. You don't need to change your code to replace the string interpolation with string.Format method to fix it. If you are still getting this error, is because, the compiler that is running your build is not the latest version of C#, try to add the Microsoft.Net.Compilers, from Nuget and compile again, that should resolve the issue. If you want to avoid to install this package, try to open your .csproj and take a look on the ToolsVersion.that should be pointing to the version 12, then change it to 14, but make sure you have installed the latest version of the MSBuild from https://www.microsoft.com/en-us/download/details.aspx?id=48159 or go to C:\Program Files (x86)\MSBuild\14.0\Bin, there you should have this folder with the csc.exe compiler. If even then that doesn't resolve the issue, then try to follow this steps https://msdn.microsoft.com/en-us/library/bb383985.aspx.

In my experience I solved this problem in 3 different ways:

1- just getting the package from Nuget

2- installing Microsoft Build Tools 2015 on the tfs server

3- The sledgehammer and last options but for me the best because you don't need to deal with the dependency on nuget, is installing the visual studio version on the tfs server where you run the process.

Hope this helps

Zinov
  • 2,956
  • 4
  • 26
  • 55
  • Installing the dependency for this Nuget package fixed my problem. Is there any way to avoid installing the dependency if I have 100 projects? – Heinrich Mar 24 '17 at 17:04
  • Take a look on my answer here http://stackoverflow.com/a/43072823/819153 – Zinov Mar 28 '17 at 14:50
14

After installing the MS Build tools 2015 into %ProgramFiles%\MSBuild\14.0\bin you need to override the MSBuild version for build server with new value (14.0).

You should read the MSDN article (or this answer), but TL;DR your options are:

  • Override version by using the /ToolsVersion switch (or /tv, for short) when you build the project or solution from the command line:

    msbuild.exe someproj.proj /tv:14.0 /p:Configuration=Debug
    
  • Override version by setting the ToolsVersion parameter on the MSBuild task:

    <MSBuild Projects="myProject.proj"  
        ToolsVersion="14.0"  
        Targets="go" />
    
  • Override version by setting the $(Project.ToolsVersion) property on a project within a solution. This lets you build a project in a solution with a ToolsetVersion that differs from that of the other projects:

    <Project ToolsVersion="14.0" ... </Project>  
    

The order of precedence, from highest to lowest, used to determine the ToolsVersion is:

  1. The ToolsVersion attribute on the MSBuild task used to build the project, if any.
  2. The /toolsversion (or /tv) switch that's used in the msbuild.exe command, if any.
  3. If the environment variable MSBUILDTREATALLTOOLSVERSIONSASCURRENT is set, then use the current ToolsVersion.
  4. If the environment variable MSBUILDTREATHIGHERTOOLSVERSIONASCURRENT is set and the ToolsVersion defined in the project file is greater than the current ToolsVersion, use the current ToolsVersion.
  5. If the environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION is set, or if ToolsVersion is not set, then the following steps are used:
    • The ToolsVersion attribute of the Project element of the project file. If this attribute doesn’t exist, it is assumed to be the current version.
    • The default tools version in the MSBuild.exe.config file.
    • The default tools version in the registry. For more information, see Standard and Custom Toolset Configurations.
  6. If the environment variable MSBUILDLEGACYDEFAULTTOOLSVERSION is not set, then the following steps are used:
    • If the environment variable MSBUILDDEFAULTTOOLSVERSION is set to a ToolsVersion that exists, use it.
    • If DefaultOverrideToolsVersion is set in MSBuild.exe.config, use it.
    • If DefaultOverrideToolsVersion is set in the registry, use it.
    • Otherwise, use the current ToolsVersion.
Community
  • 1
  • 1
VMAtm
  • 26,645
  • 17
  • 75
  • 107
  • 2
    I tried all of this changes, even I change the values on the register to the change the default version to 14.0, also I changed the MSBuild.exe.config but everything was unsuccessful. My project has the correct ToolsVersion – Heinrich Mar 24 '17 at 15:09
  • 2
    @Heinrich you also need to make sure that you're running msbuild.exe from "C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\15.0\Bin\" (not from C:\Windows\Microsoft.NET\Framework\v4.0.30319\) – Shrike May 30 '17 at 16:13
  • 1
    for me the path was `"%ProgramFiles(x86)%\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\msbuild.exe"` – knocte Oct 09 '17 at 11:12
8

There is a chance you are building with the wrong MSbuild.exe; do the compile in Visual Studio (where it works) and check the logs in Output. There should be something like:

1>Target "GetReferenceAssemblyPaths" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets" 

Make sure you are using the MSBuild.exe in that Bin directory, in my case;

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\MSbuild.exe
CharlesS
  • 1,252
  • 1
  • 14
  • 30
5

Microsoft.Net.Compilers didnt work, but installing DotNetCompilerPlatform from Nuget did.

Mike Flynn
  • 21,905
  • 50
  • 167
  • 308
2

I'm just guessing that you have code with string interpolations and don't have the proper build tools.

Paulo Morgado
  • 11,247
  • 2
  • 24
  • 48
2

My solution was 2 part:

1) Using Visual Studio 2015, select your web project, click the Project menu, select "Enable C# 6"

2) I needed to add the following to the end of my production web.config, just before the configuration closing tag. Note that version numbers may change in the future, but the key is to look for similar text in your development web.config after enabling in step 1 and make sure it gets transferred to production.

  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
Tyler Forsythe
  • 1,361
  • 15
  • 21
2

You must use msbuild version 15.

motodiver
  • 121
  • 3