msbuild: Failure using 15.8 RoslynCodeTaskFactory

While trying to migrate off the NuGet package to the 15.8 RoslynCodeTask factory, I’m encountering issues getting the most basic tasks to work.

E.g. copying from the documentation https://docs.microsoft.com/en-us/visualstudio/msbuild/msbuild-roslyncodetaskfactory?view=vs-2017#hello-world to a test.proj:

<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">  
  <!-- This simple inline task displays "Hello, world!" -->  
  <UsingTask  
    TaskName="HelloWorld"  
    TaskFactory="RoslynCodeTaskFactory"  
    AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll" >  
    <ParameterGroup />  
    <Task>  
      <Reference Include="System.Xml"/>
      <Using Namespace="System"/>  
      <Using Namespace="System.IO"/>  
      <Code Type="Fragment" Language="cs">  
<![CDATA[  
// Display "Hello, world!"  
Log.LogError("Hello, world!");  
]]>  
      </Code>  
    </Task>  
  </UsingTask>  
    <Target Name="Hello">  
    <HelloWorld />  
  </Target>  
</Project>

Windows full framework (15.8.3):

"C:\Users\martin.ullrich\Downloads\testproj\test.proj" (Hello target) (1) ->
(Hello target) ->
  C:\Users\martin.ullrich\Downloads\testproj\test.proj(21,5): error MSB3755: Could not find reference "System.Xml". If this reference is required by your code, you may
 get compilation errors.
  C:\Users\martin.ullrich\Downloads\testproj\test.proj(21,5): error MSB4175: The task factory "RoslynCodeTaskFactory" could not be loaded from the assembly "C:\Program
 Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\Microsoft.Build.Tasks.Core.dll". The task factory must return a value for the "TaskType" property
.

MacOS dotnet 2.1.401:

/Users/martin/tmp/testsln/test.proj/test.proj(21,5): error MSB3755: Could not find reference "System.Xml". If this reference is required by your code, you may get compilation errors.
/Users/martin/tmp/testsln/test.proj/test.proj(21,5): error MSB3755: Could not find reference "mscorlib". If this reference is required by your code, you may get compilation errors.
/Users/martin/tmp/testsln/test.proj/test.proj(21,5): error MSB3755: Could not find reference "netstandard". If this reference is required by your code, you may get compilation errors.
/Users/martin/tmp/testsln/test.proj/test.proj(21,5): error MSB4175: The task factory "RoslynCodeTaskFactory" could not be loaded from the assembly "/usr/local/share/dotnet/sdk/2.1.401/Microsoft.Build.Tasks.Core.dll". The task factory must return a value for the "TaskType" property.

cc @jeffkl

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 5
  • Comments: 26 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I was also seeing this behavior with the same error message.

I think that’s a documentation bug, you shouldn’t need to reference System.Xml. Try removing:

<Reference Include="System.Xml"/>

I am still getting this problem using MSBuild bundled with the latest .NET Core release (downloaded using dotnet-install.ps1) using MSBuild (.NET Core) version 16.1.19!

Argh, still seeing this issue in VS 2019 (16.0.0). Any System.* <Reference>s that I try to add to an inline task in my .NET Standard project are yielding:

Could not find reference “System.*”. If this reference is required by your code, you may get compilation errors.

@jeffkl @rainersigwald Are there other reports of a regression with this?

@thomasvdb SDK 2.2.103 includes MSBuild 15.9, but this was fixed only in MSBuild 16.0 (in SDK 2.2.200 and higher).

Normal tasks in MSBuild are loaded in a way that their dependencies are also loaded from the same directory. Tasks created with a custom “task host” such as RoslynCodeTaskFactory do not get this logic for free. You’ll need to load your dependencies manually since the host executable is MSBuild.exe and .NET CLR assembly loading rules only load dependencies next to the running executable.

https://docs.microsoft.com/en-us/dotnet/standard/assembly/resolve-loads

@dasMulli Having the same issue as @DanVicarel but on macOS. It also throws errors on mscorlib and netstandard hence the error of RoslynCodeTaskFactory.

error MSB3755: Could not find reference "mscorlib". If this reference is required by your code, you may get compilation errors]
error MSB3755: Could not find reference "netstandard". If this reference is required by your code, you may get compilation errors.
error MSB4175: The task factory "RoslynCodeTaskFactory" could not be loaded from the assembly "/usr/local/share/dotnet/sdk/2.2.103/Microsoft.Build.Tasks.Core.dll". The task factory must return a value for the "TaskType" property.

@radical I’m also seeing this in the msbuild 16.0.42-preview+g804bde742b bundled with Mono 5.18.0.234

I am using msbuild version 15.9.20+g88f5fadfbe with .netcore 2.1 and I am still experiencing error below. Any idea when it is being fixed?

error MSB4175: The task factory “RoslynCodeTaskFactory” could not be loaded from the assembly “C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\bin\Microsoft.Build.Tasks.Core.dll”. The task factory must return a value for the “TaskType” property.

For now, my workaround is still to reference external dependency from https://github.com/jeffkl/RoslynCodeTaskFactory

The root cause of the bug is that the Microsoft.Build.Tasks project is copying the ref assemblies to its target framework output folder (net46 and netstandard2.0). The MSBuild project is then including them in the NuGet package for its target frameworks (net46, netcoreapp2.0, and netcoreapp2.1). So the files are in the correct place but aren’t being included in the NuGet package.

So an easy workaround is to copy the ref folder from the full framework location ( the files are the same ) and we can look into fixing this for the next release of .NET Core.

I have the same problem, the project builds fine when using visual studio, but it will throw error when running cli ‘dotnet build’.