Error CS1056: Unexpected character '$' running the msbuild on a tfs continuous integration process

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


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.

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