Fody / Costura

Embed references as resources
MIT License
2.41k stars 275 forks source link

Fody.Costura on multi-target (.NET7 + .NET Framework 4.8.1) Class Library - Failure to Embed Dependencies #921

Closed fkitsantas closed 1 year ago

fkitsantas commented 1 year ago

Hello Fody.Costura Community,

I'm currently working on an open-source project called Reina.Cryptography, a .NET Core & .NET Framework encryption library integrating Azure Key Vault and employing a triple-layered encryption method. The project is available here: https://github.com/fkitsantas/Reina.Cryptography.

Reina Cryptography Library

Issue: While Fody.Costura works perfectly in embedding dependencies on both frameworks if you try it on separate projects, while multi-targeting:

The Fody.Costura configuration I'm using and seems to work on both (individually) is:

<?xml version="1.0" encoding="utf-8" ?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
    <Costura LoadAtModuleInit="true" Log="true">
        <IncludeAssemblies>
            Azure.Identity
            Azure.Security.KeyVault.Keys
            Azure.Security.KeyVault.Secrets
            Azure.Core
            Microsoft.Extensions.Configuration
            Microsoft.Identity.Client
            Microsoft.IdentityModel.Abstractions
            System.Memory.Data
            BouncyCastle.Cryptography
        </IncludeAssemblies>
    </Costura>
</Weavers>

Could you please help me out on what is the issue?

Kind regards, Fotis :)

tom-englert commented 1 year ago

If you need individual configurations per target framework, move the configuration to the project file, so you can make it conditional on the target framework: https://github.com/Fody/Home/blob/master/pages/configuration.md#an-msbuild-property-in-the-project-file

fkitsantas commented 1 year ago

If you need individual configurations per target framework, move the configuration to the project file, so you can make it conditional on the target framework: https://github.com/Fody/Home/blob/master/pages/configuration.md#an-msbuild-property-in-the-project-file

@tom-englert thanks a lot for the reply.

I don't need/want to use multiple configuration files according to my understanding, as the same configuration works for both .NET 7 and .NET Framework 4.8.1. if you use it on different separate solutions. My problem is that it doesn't work on my multi-target solution.

Take a look at my repository if you please. If you try to build the project, it will now build it properly for .NET7 but it will not embed the DLL dependencies for the .NET Framework 4.8.1

The FodyWeavers.xml configuration is not the problem, it's something you have to do on .csproj customization I presume, but that's what I'm looking for help with.

my current csproj is:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFrameworks>net481;net7.0</TargetFrameworks>
    <LangVersion>latest</LangVersion>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <GenerateDocumentationFile>true</GenerateDocumentationFile>
    <VersionPrefix>3.0.1</VersionPrefix>
    <PackageProjectUrl>https://fkitsantas.github.io/Reina.Cryptography</PackageProjectUrl>
    <RepositoryUrl>https://www.github.com/fkitsantas/Reina.Cryptography</RepositoryUrl>
    <RepositoryType>git</RepositoryType>
  </PropertyGroup>

  <!-- Common Package References -->
  <ItemGroup>
    <PackageReference Include="Azure.Core" Version="1.36.0" />
    <PackageReference Include="Azure.Identity" Version="1.10.3" />
    <PackageReference Include="Azure.Security.KeyVault.Keys" Version="4.5.0" />
    <PackageReference Include="Azure.Security.KeyVault.Secrets" Version="4.5.0" />
    <PackageReference Include="BouncyCastle.Cryptography" Version="2.2.1" />
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="7.0.0" />
    <PackageReference Include="Microsoft.Identity.Client" Version="4.57.0" />
    <PackageReference Include="Microsoft.IdentityModel.Abstractions" Version="7.0.3" />
    <PackageReference Include="System.Memory.Data" Version="7.0.0" />
    <PackageReference Include="Fody" Version="6.8.0" PrivateAssets="all" />
    <PackageReference Include="Costura.Fody" Version="5.7.0" PrivateAssets="all" />
  </ItemGroup>

  <!-- Add a suffix to the output assembly based on the target framework -->
  <Target Name="AddFrameworkSuffixToOutput" AfterTargets="Build">
    <Move SourceFiles="$(OutputPath)$(AssemblyName).dll" DestinationFiles="$(OutputPath)$(AssemblyName).$(TargetFramework).dll" Condition="Exists('$(OutputPath)$(AssemblyName).dll')" />
  </Target>
</Project>

as you can see directly here: https://github.com/fkitsantas/Reina.Cryptography/blob/main/Reina.Cryptography.csproj.

My FodyWeavers.xml:

<?xml version="1.0" encoding="utf-8" ?>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
    <Costura LoadAtModuleInit="true" Log="true">
        <IncludeAssemblies>
            Azure.Identity
            Azure.Security.KeyVault.Keys
            Azure.Security.KeyVault.Secrets
            Azure.Core
            Microsoft.Extensions.Configuration
            Microsoft.Identity.Client
            Microsoft.IdentityModel.Abstractions
            System.Memory.Data
            BouncyCastle.Cryptography
        </IncludeAssemblies>
    </Costura>
</Weavers>

Any help on how to make it work for both builds? Thanks a lot for your time and effort.

Kind regards, Fotis

tom-englert commented 1 year ago

Then I have no clue, theoretically multi-targeting should be the same as two distinct builds.

fkitsantas commented 1 year ago

I think I just figured out that this only happens on my work's laptop. Apparently GitHub Actions is able to compile both properly on the same configuration.. so.. ;p

Thanks a lot for your time Tom. Of course both you and Fody.Costura will be acknowledged on my readme. Thanks again!