Fody/Costura

Dotnet build: could not load Mono.Cecil

wldevries opened this issue · 7 comments

Please check all of the platforms you are having the issue on (if platform is not listed, it is not supported)

  • WPF
  • UWP
  • iOS
  • Android
  • .NET Standard
  • .NET Core

Component

Not sure

Version of Library

Costura.Fody 4.1.0

Version of OS(s) listed above with issue

Windows 10 20H2
dotnet --version: 5.0.102
Visual Studio 2019 v16.8.4

Steps to Reproduce

  1. Create a WPF SDK csproj project targeting net45
  2. Add reference to Costura.Fody 4.1.0
  3. dotnet build

For reference, my csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net45</TargetFramework>
    <ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <AssemblyTitle>PackageManagerAkka</AssemblyTitle>
    <UseWPF>true</UseWPF>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
  </PropertyGroup>
</Project>

Expected Behavior

Builds a single exe just like Visual Studio 2019 does

Actual Behavior

A build error occurs

MSBUILD : error : Fody: An unhandled exception occurred: [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : Exception: [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : Could not load file or assembly 'Mono.Cecil, Version=0.11.0.0, Culture=neutral, PublicKeyToken=1ca091877d12ca03'. The system cannot find the file specified. [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : Type: [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : System.IO.FileNotFoundException [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : StackTrace: [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error :    at InnerWeaver.Execute() [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error :    at Processor.ExecuteInOwnAssemblyLoadContext() in C:\projects\fody\Fody\Processor.cs:line 148 [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error :    at Processor.Inner() in C:\projects\fody\Fody\Processor.cs:line 122 [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error :    at Processor.Execute() in C:\projects\fody\Fody\Processor.cs:line 50 [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : Source: [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : FodyIsolated [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : TargetSite: [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error : Void Execute() [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]
MSBUILD : error :  [D:\dev\tools\PackageManagerAkka\PackageManagerAkka\PackageManagerAkka.csproj]

I believe the minimum supported version for fody is 4.7.2 nowadays and this is not related to Costura?

Fody and Costura work fine when built from within Visual Studio using msbuild behind the scenes probably.

are u using the latest version of fody?

Thanks Simon, that fixed it. My project used an indirect reference and the PackageReference to Costura.Fody 4.1.0 pulled in Fody version 6.0.0. After explicitly referencing Fody 6.3.0 the error was fixed.

Small additional question: the references now look like this in my project. Are these correct?

    <PackageReference Include="Costura.Fody" Version="4.1.0">
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Fody" Version="6.3.0">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>

Yes, all should be set to private assets. Please care that in .NET Core / .NET 5, the runtime in "include assets" will still bite you so I recommend to remove it.

Will close this ticket. Feel free to re-open if something is unclear.

@wldevries

Re needing an explicit nuget reference: https://github.com/Fody/Home/blob/master/pages/usage.md#install-fody

Since NuGet always defaults to the oldest, and most buggy, version of any dependency it is important to do a NuGet install of Fody after installing any weaver.

Re <PrivateAssets>all</PrivateAssets> : https://github.com/Fody/Home/blob/master/pages/usage.md#adding-fody-to-a-project-that-generates-a-nuget-package

also note you should be a Patron on Open Collectiove to be using Fody https://github.com/Fody/Home/blob/master/pages/licensing-patron-faq.md