jeffkl/RoslynCodeTaskFactory

Possible bug while loading external .dll

eduherminio opened this issue · 2 comments

Back to my original example, I still can't make this task work.

I'm not sure whether this represents an issue of RoslynCodeTaskFactory, but a different behaviour is observed when compiling from VS (it fails, trying to load net46 .dll) and from cli (it also fails, but trying to load netstandard1.5 .dll instead), while it works when using VS built-in CodeTaskFactory.

<UsingTask TaskName="ZipDir" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)" Condition=" '$(RoslynCodeTaskFactory)' != '' ">
    <ParameterGroup>
      <InputBaseDirectory ParameterType="System.String" Required="true" />
      <OutputFileName ParameterType="System.String" Required="true" />
      <OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
      <IncludeBaseDirectory ParameterType="System.Boolean" Required="false" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.IO.Compression" />
      <Reference Include="System.IO.Compression.FileSystem" />
      <Using Namespace="System.IO.Compression" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[        
        if (File.Exists(OutputFileName))
        {
            if (!OverwriteExistingFile)
            {
                return false;
            }
            File.Delete(OutputFileName);
        }
        ZipFile.CreateFromDirectory
        (
            InputBaseDirectory, OutputFileName, 
            CompressionLevel.Optimal, IncludeBaseDirectory
        );
      ]]>
      </Code>
    </Task>
  </UsingTask>

To invoke it:

  <Target Name="Bug_Reproduction" AfterTargets="Build">
    <ZipDir InputBaseDirectory="bin" OutputFileName="folder.zip" OverwriteExistingFile="true"></ZipDir>
  </Target>

Full example can be found here (tested with v1.2.1).

In order for .NET Framework and .NET Core to use the same references, the NuGet packages come with type forwards. So System.IO.Compression.ZipFile.dll has a typeforward assembly which is missing. I am adding these assemblies as people report them because I'm not entirely sure which ones need to be included.

You're also referencing the wrong assembly name:

<UsingTask TaskName="ZipDir" TaskFactory="CodeTaskFactory" AssemblyFile="$(RoslynCodeTaskFactory)" Condition=" '$(RoslynCodeTaskFactory)' != '' ">
    <ParameterGroup>
      <InputBaseDirectory ParameterType="System.String" Required="true" />
      <OutputFileName ParameterType="System.String" Required="true" />
      <OverwriteExistingFile ParameterType="System.Boolean" Required="false" />
      <IncludeBaseDirectory ParameterType="System.Boolean" Required="false" />
    </ParameterGroup>
    <Task>
      <Reference Include="System.IO.Compression" />
-      <Reference Include="System.IO.Compression.FileSystem" />
+      <Reference Include="System.IO.Compression.ZipFile" />
      <Using Namespace="System.IO.Compression" />
      <Code Type="Fragment" Language="cs">
        <![CDATA[        
        if (File.Exists(OutputFileName))
        {
            if (!OverwriteExistingFile)
            {
                return false;
            }
            File.Delete(OutputFileName);
        }
        ZipFile.CreateFromDirectory
        (
            InputBaseDirectory, OutputFileName, 
            CompressionLevel.Optimal, IncludeBaseDirectory
        );
      ]]>
      </Code>
    </Task>
  </UsingTask>

So I've added the assembly needed for .NET Framework and published a new package

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

With those changes I'm able to build your sample and the folder.zip is created for both .NET Framework via MSBuild.exe and .NET Core via dotnet build

Related to #13

Tested, works as expected.
Thanks again for your help and congrats on your project!