jeffkl/RoslynCodeTaskFactory

Not working in .NET Core v2.0 project

eduherminio opened this issue · 14 comments

I'm trying to create a .zip file using MSBuild following this example in a .csproj targetting netcoreapp2.0.

Since MSBuild on .NET Core does not support CodeTaskFactory, I tried modifying the task by using RoslynCodeTaskFactory in the following way:

<UsingTask TaskName="Zip" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)">
    <ParameterGroup>
      <OutputFilename ParameterType="System.String" Required="true" />
      <Files ParameterType="Microsoft.Build.Framework.ITaskItem[]" Required="true" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.IO.Compression" />
      <Using Namespace="System.IO.Compression" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[
      try
      {
        using (Stream zipStream = new FileStream(Path.GetFullPath(OutputFilename), FileMode.Create, FileAccess.Write))
        using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create))
        {
            foreach (ITaskItem fileItem in Files)
            {
                string filename = fileItem.ItemSpec;
                using (Stream fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read))
                using (Stream fileStreamInZip = archive.CreateEntry(new FileInfo(filename).Name).Open())
                    fileStream.CopyTo(fileStreamInZip);
            }
        }
        return true;
      }
      catch (Exception ex)
      {
        Log.LogErrorFromException(ex);
        return false;
      }
    ]]>
      </Code>
    </Task>
  </UsingTask>

However, I'm getting the following error:
The value "" resulting from evaluating the value of "$(RoslynCodeTaskFactory)" from attribute "AssemblyFile" of element <UsingTask> is not valid.

Am i doing something wrong?

The $(RoslynCodeTaskFactory) property is set by an import inside the NuGet package. You must include a <PackageReference /> in your project and do a restore. This should create a file named obj\MyProject.g.nuget.props which contains an import to the task factory file that creates that property. Verify you have things set up correctly.

I've managed to reproduce the issue, keeping it as simple as possible, in this repo.

Okay sorry you're just missing a condition on your <UsingTask />. Before packages have been restored, the package isn't imported and $(RoslynCodeTaskFactory) isn't set yet.

Add this condition: https://github.com/jeffkl/RoslynCodeTaskFactory/blob/master/src/Samples/Directory.Build.targets#L8

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="wwwroot\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.3" />
    <PackageReference Include="RoslynCodeTaskFactory" Version="1.1.7" />
  </ItemGroup>

  <Target Name="Bug_Reproduction" AfterTargets="Build">
    <Sample />
  </Target>

  <UsingTask TaskName="Sample"
        TaskFactory="CodeTaskFactory"
        AssemblyFile="$(RoslynCodeTaskFactory)"
+       Condition=" '$(RoslynCodeTaskFactory)' != '' ">
    <Task>
      <Code Type="Fragment" Language="cs">
        <![CDATA[        
       ;
      ]]>
      </Code>
    </Task>
  </UsingTask>

</Project>

I have the condition in my samples but not in the main readme. I'll update that!

mm that makes sense, thanks!
In fact I added the condition at some point of my trials, but later removed it since it didn't make it fully work (I was trying out from command line).

It works like a charm when compiling/publishing from VS.
However, I am not being able to make tasks work from command line (neither directly, with dotnet msbuild /t:my_task or after a dotnet build or dotnet build --force --no-incremental).
I'm getting the following error message (translated):
"CodeTaskFactory" task generator couldn't be loaded from assembly file "<Route to netstandard1.5\RoslynCodeTaskFactory.dll>" Task generator must return a value for "TaskType" property.

Now you mentioned it, I suppose it has to do with package importing/restoring, but no real idea.
Any clues?

It may be related to https://github.com/dotnet/cli/issues/7510
Does your sample project compile from cli?

I have investigated and figured out that in .NET Core 2.0/MSBuild 15.5, Roslyn moved csc.exe. I'm working on an update to my task factory so please stand by...

Thanks for reporting the issue, I've fixed this in v1.2.1 of the package. Please try it out and re-open if you're still having issues.

https://www.nuget.org/packages/RoslynCodeTaskFactory/1.2.1

I'm still getting this problem mentioning the TaskType property with 1.2.2 of your package. Using dotnet 2.1.2 and its using msbuild 15.5.179.9764. My project is targeting the net45 TFM. Repros even if I retarget to net46 or net461.

@heaths can you supply a repro or have the ability to run the task factory under a debugger? I'd like to get to the bottom of the issue

Sure thing. See https://github.com/heaths/RestartManager/tree/migrate. Just clone, dotnet restore, and build. Thanks.

@heaths I was only getting a compiler error with your code base and have sent a PR: heaths/RestartManager#2

Did you run dotnet build or MSBuild? IT was repro'ing for me (same as above) with MSBuild at least.

I can do both:

D:\Temp\RestartManager\src\RestartManager.PowerShell>dotnet --version
15.5.0-preview-007044

D:\Temp\RestartManager\src\RestartManager.PowerShell>dotnet build /clp:v=n
Build started 1/12/2018 1:32:27 PM.
     1>Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" on node 1 (Restore target(s)).
     1>Restore:
         Committing restore...
         Assets file has not changed. Skipping assets file writing. Path: D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\project.assets.json
         Restore completed in 15 ms for D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj.

         NuGet Config files used:
             C:\Users\jeffkl\AppData\Roaming\NuGet\NuGet.Config
             C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

         Feeds used:
             C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
             https://api.nuget.org/v3/index.json
     1>Done Building Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" (Restore target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.25
Microsoft (R) Build Engine version 15.6.0.0 for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 1/12/2018 1:32:28 PM.
     1>Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" on node 1 (Build target(s)).
     1>GetBuildVersion:
         Building version 1.0.25.2241 from commit c10889fb5d844fd3a8942c21b1d0d8af0bc44e8d
       CoreResGen:
         No resources are out of date with respect to their source files. Skipping resource generation.
       Transform:
         Transforming: "RestartManager.psd1" -> "bin\Debug\RestartManager.psd1"
       CoreGenerateAssemblyInfo:
       Skipping target "CoreGenerateAssemblyInfo" because all output files are up-to-date with respect to the input files.
       CoreCompile:
         C:\Program Files\dotnet\dotnet.exe "C:\Program Files\dotnet\sdk\15.5.0-preview-007044\Roslyn\bincore\csc.dll" /noconfig /unsafe- /checked- /nowarn:1701,1702,1705,1701,1702 /nostdlib+ /errorreport:prompt /warn:4 /doc:obj\Debug\RestartManager.PowerShell.xml /define:TRACE;DEBUG;NET46 /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Drawing.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.IO.Compression.FileSystem.dll" /reference:C:\Users\jeffkl\.nuget\packages\microsoft.powershell.3.referenceassemblies\1.0.0\lib\net4\System.Management.Automation.dll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Numerics.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Xml.Linq.dll" /debug+ /debug:portable /filealign:512 /nologo /optimize- /out:obj\Debug\RestartManager.PowerShell.dll /ruleset:..\..\inc\Common.ruleset /subsystemversion:6.00 /resource:obj\Debug\RestartManager.PowerShell.Properties.Resources.resources /target:library /warnaserror+ /utf8output /deterministic+ /analyzer:C:\Users\jeffkl\.nuget\packages\stylecop.analyzers\1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll /analyzer:C:\Users\jeffkl\.nuget\packages\stylecop.analyzers\1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.dll /additionalfile:..\..\inc\stylecop.json ApplicationStatus.cs ApplicationType.cs Extensions.cs IProcess.cs IProcessInfo.cs IRestartManagerService.cs IServiceContainer.cs IServiceProvider.cs NativeMethods.cs PowerShell\ActiveSessionException.cs PowerShell\Extensions.cs PowerShell\GetProcessCommand.cs PowerShell\ITestableHost.cs PowerShell\IVariableService.cs PowerShell\NoSessionException.cs PowerShell\Nouns.cs PowerShell\RegisterResourceCommand.cs PowerShell\RestartProcessCommand.cs PowerShell\RunspaceVariableService.cs PowerShell\SessionCommand.cs PowerShell\SessionManager.cs PowerShell\StartSessionCommand.cs PowerShell\StopProcessCommand.cs PowerShell\StopSessionCommand.cs ProcessAdapter.cs ProcessComparer.cs ProcessInfo.cs ProgressEventArgs.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs RebootReason.cs RestartManagerSession.cs ServiceContainer.cs Validate.cs WindowsRestartManagerService.cs obj\Debug\\RestartManager.PowerShell.Version.cs "C:\Users\jeffkl\AppData\Local\Temp\.NETFramework,Version=v4.6.AssemblyAttributes.cs" obj\Debug\RestartManager.PowerShell.AssemblyInfo.cs /warnaserror+:NU1605
       _CopyFilesMarkedCopyLocal:
         Touching "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.csproj.CopyComplete".
       CopyFilesToOutputDirectory:
         Copying file from "obj\Debug\RestartManager.PowerShell.dll" to "bin\Debug\RestartManager.PowerShell.dll".
         RestartManager.PowerShell -> D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\RestartManager.PowerShell.dll
         Copying file from "obj\Debug\RestartManager.PowerShell.pdb" to "bin\Debug\RestartManager.PowerShell.pdb".
         Copying file from "obj\Debug\RestartManager.PowerShell.xml" to "bin\Debug\RestartManager.PowerShell.xml".
     1>Done Building Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" (Build target(s)).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:05.04
D:\Temp\RestartManager\src\RestartManager.PowerShell>msbuild /t:rebuild /restore
Microsoft (R) Build Engine version 15.6.13.61168 for .NET Framework
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 1/12/2018 1:33:08 PM.
Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" on node 1 (Restore target(s)).
Restore:
  Committing restore...
  Assets file has not changed. Skipping assets file writing. Path: D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\project.assets.json
  Restore completed in 50.28 ms for D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj.

  NuGet Config files used:
      C:\Users\jeffkl\AppData\Roaming\NuGet\NuGet.Config
      C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

  Feeds used:
      https://api.nuget.org/v3/index.json
      C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\
Done Building Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" (Restore target(s)).

Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" on node 1 (rebuild target(s)).
CoreClean:
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\RestartManager.psd1".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\RestartManager.PowerShell.dll".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\RestartManager.PowerShell.pdb".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\RestartManager.PowerShell.xml".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\System.Management.Automation.dll".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.Properties.Resources.resources".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.csproj.CoreCompileInputs.cache".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.AssemblyInfoInputs.cache".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.AssemblyInfo.cs".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.dll".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.xml".
  Deleting file "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.pdb".
GetBuildVersion:
  Building version 1.0.25.2241 from commit c10889fb5d844fd3a8942c21b1d0d8af0bc44e8d
GenerateAssemblyVersionInfo:
  Copying file from "obj\Debug\\RestartManager.PowerShell.Version.cs.new" to "obj\Debug\\RestartManager.PowerShell.Version.cs".
CoreResGen:
  "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\resgen.exe" /useSourcePath /r:"C:\Program Files (x86)\Reference Assemblies\Micros
  oft\Framework\.NETFramework\v4.6\mscorlib.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Core.dll" /r:"C:
  \Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\F
  ramework\.NETFramework\v4.6\System.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Drawing.dll" /r:"C:\Pro
  gram Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.IO.Compression.FileSystem.dll" /r:C:\Users\jeffkl\.nuget\packages\microso
  ft.powershell.3.referenceassemblies\1.0.0\lib\net4\System.Management.Automation.dll /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETF
  ramework\v4.6\System.Numerics.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Runtime.Serialization.dll" /
  r:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Xml.dll" /r:"C:\Program Files (x86)\Reference Assemblies\Microso
  ft\Framework\.NETFramework\v4.6\System.Xml.Linq.dll" /compile Properties\Resources.resx,obj\Debug\RestartManager.PowerShell.Properties.Resources.resources
  Processing resource file "Properties\Resources.resx" into "obj\Debug\RestartManager.PowerShell.Properties.Resources.resources".
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
Transform:
  Transforming: "RestartManager.psd1" -> "bin\Debug\RestartManager.psd1"
CoreCompile:
  C:\Program Files (x86)\Microsoft Visual Studio 2017 Preview\MSBuild\15.0\bin\Roslyn\csc.exe /noconfig /unsafe- /checked- /nowarn:1701,1702,1705,1701,1702 /no
  stdlib+ /errorreport:prompt /warn:4 /doc:obj\Debug\RestartManager.PowerShell.xml /define:TRACE;DEBUG;NET46 /highentropyva+ /reference:"C:\Program Files (x86)
  \Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFr
  amework\v4.6\System.Core.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Data.dll" /reference:"C:\
  Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft
  \Framework\.NETFramework\v4.6\System.Drawing.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.IO.Co
  mpression.FileSystem.dll" /reference:C:\Users\jeffkl\.nuget\packages\microsoft.powershell.3.referenceassemblies\1.0.0\lib\net4\System.Management.Automation.d
  ll /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Numerics.dll" /reference:"C:\Program Files (x86)\Ref
  erence Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Runtime.Serialization.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\
  Framework\.NETFramework\v4.6\System.Xml.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6\System.Xml.Linq.d
  ll" /debug+ /debug:portable /filealign:512 /nologo /optimize- /out:obj\Debug\RestartManager.PowerShell.dll /ruleset:..\..\inc\Common.ruleset /subsystemversio
  n:6.00 /resource:obj\Debug\RestartManager.PowerShell.Properties.Resources.resources /target:library /warnaserror+ /utf8output /deterministic+ /analyzer:C:\Us
  ers\jeffkl\.nuget\packages\stylecop.analyzers\1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.CodeFixes.dll /analyzer:C:\Users\jeffkl\.nuget\packages\stylecop.a
  nalyzers\1.0.2\analyzers\dotnet\cs\StyleCop.Analyzers.dll /additionalfile:..\..\inc\stylecop.json ApplicationStatus.cs ApplicationType.cs Extensions.cs IProc
  ess.cs IProcessInfo.cs IRestartManagerService.cs IServiceContainer.cs IServiceProvider.cs NativeMethods.cs PowerShell\ActiveSessionException.cs PowerShell\Ex
  tensions.cs PowerShell\GetProcessCommand.cs PowerShell\ITestableHost.cs PowerShell\IVariableService.cs PowerShell\NoSessionException.cs PowerShell\Nouns.cs P
  owerShell\RegisterResourceCommand.cs PowerShell\RestartProcessCommand.cs PowerShell\RunspaceVariableService.cs PowerShell\SessionCommand.cs PowerShell\Sessio
  nManager.cs PowerShell\StartSessionCommand.cs PowerShell\StopProcessCommand.cs PowerShell\StopSessionCommand.cs ProcessAdapter.cs ProcessComparer.cs ProcessI
  nfo.cs ProgressEventArgs.cs Properties\AssemblyInfo.cs Properties\Resources.Designer.cs RebootReason.cs RestartManagerSession.cs ServiceContainer.cs Validate
  .cs WindowsRestartManagerService.cs obj\Debug\\RestartManager.PowerShell.Version.cs "C:\Users\jeffkl\AppData\Local\Temp\.NETFramework,Version=v4.6.AssemblyAt
  tributes.cs" obj\Debug\RestartManager.PowerShell.AssemblyInfo.cs /warnaserror+:NU1605
  Using shared compilation with compiler from directory: C:\Program Files (x86)\Microsoft Visual Studio 2017 Preview\MSBuild\15.0\bin\Roslyn
_CopyFilesMarkedCopyLocal:
  Copying file from "C:\Users\jeffkl\.nuget\packages\microsoft.powershell.3.referenceassemblies\1.0.0\lib\net4\System.Management.Automation.dll" to "bin\Debug\
  System.Management.Automation.dll".
  Touching "D:\Temp\RestartManager\src\RestartManager.PowerShell\obj\Debug\RestartManager.PowerShell.csproj.CopyComplete".
CopyFilesToOutputDirectory:
  Copying file from "obj\Debug\RestartManager.PowerShell.dll" to "bin\Debug\RestartManager.PowerShell.dll".
  RestartManager.PowerShell -> D:\Temp\RestartManager\src\RestartManager.PowerShell\bin\Debug\RestartManager.PowerShell.dll
  Copying file from "obj\Debug\RestartManager.PowerShell.pdb" to "bin\Debug\RestartManager.PowerShell.pdb".
  Copying file from "obj\Debug\RestartManager.PowerShell.xml" to "bin\Debug\RestartManager.PowerShell.xml".
Done Building Project "D:\Temp\RestartManager\src\RestartManager.PowerShell\RestartManager.PowerShell.csproj" (rebuild target(s)).


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.99

I'll clean my repo tonight and try again. Perhaps there's some intermediate files of some sort that are causing an issue.