Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileLoadException: Assembly with same name is already loaded when multiple Fody-using projects are referenced #1268

Closed
andrewlock opened this issue Apr 22, 2024 · 4 comments 路 Fixed by #1269
Labels
Milestone

Comments

@andrewlock
Copy link

You should already be a Patron

We (Datadog) have sponsored Simon through GitHub - hopefully that's sufficient, if not, just let me know 馃檪

Preamble

Where relevant, ensure you are using the current stable versions of the following:

  • Fody - 6.8.0 (latest stable)
  • InlineIL.Fody - 1.8.0 (latest stable) and 1.7.4
  • MSBuild - 17.8.3+195e7f5a3
  • .NET Core SDK - (8.0.100)

Describe the issue

We have been using InlineIL.Fody without issue in the Datadog .NET Client library. We recently had to add it to another project. A third project then references those two:

Datadog.Trace.Runner
    |__Datadog.Trace (Uses Fody)
    |__Datadog.Trace.Coverage.Collector (Uses Fody)

Once we added Fody to the Datadog.Trace.Coverage.Collector project, the Datadog.Trace.Runner has started failing to build, with the following exception from MSBuild.

Fody: An unhandled exception occurred:
Exception: Assembly with same name is already loaded
Type: System.IO.FileLoadException
StackTrace:
   at System.Runtime.Loader.AssemblyLoadContext.InternalLoad(ReadOnlySpan`1 arrAssembly, ReadOnlySpan`1 arrSymbols)
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromStream(Stream assembly, Stream assemblySymbols)
   at IsolatedAssemblyLoadContext.LoadNotLocked(String assemblyPath) in C:\projects\fody\FodyCommon\IsolatedAssemblyLoadContext.cs:line 66
   at InnerWeaver.LoadFromFile(String assemblyPath) in C:\projects\fody\FodyIsolated\AssemblyLoader.cs:line 25
   at InnerWeaver.LoadWeaverAssembly(String assemblyPath) in C:\projects\fody\FodyIsolated\AssemblyLoader.cs:line 18
   at InnerWeaver.InitialiseWeaver(WeaverEntry weaverConfig) in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 159
   at InnerWeaver.InitialiseWeavers() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 147
   at InnerWeaver.Execute() in C:\projects\fody\FodyIsolated\InnerWeaver.cs:line 108
Source: System.Private.CoreLib
TargetSite: System.Reflection.Assembly InternalLoad(System.ReadOnlySpan`1[System.Byte], System.ReadOnlySpan`1[System.Byte])

This was breaking our build- (Public build in Azure Devops).

I subsequently noticed that we were accidentally referencing two different versions of InlineIL.Fody. Fixing both projects to use the same assembly seems to have solved the issue, but it still feels like there may be something to investigate here...

Minimal Repro

Unfortunately, this appears to be hard to reproduce as it appears to be a race condition. I have not managed to create a minimal repro yet; I can't reproduce with a simplified version of the setup.

Make an effort to fix the bug

For a while I thought that disabling MSBuild node reuse would work (as it seemed to in the PR and test build), but even with MSBUILDDISABLENODEREUSE=1 we were still seeing failures 馃檨 The only thing that fixed it was ensuring that all dependent projects used the same version of InlineIL.Fody

@andrewlock andrewlock changed the title FileLoadException: Assembly with same name is already loaded when multiple projects reference FileLoadException: Assembly with same name is already loaded when multiple Fody-using projects are referenced Apr 22, 2024
@ltrzesniewski
Copy link
Member

ltrzesniewski commented Apr 22, 2024

My guess would be that you didn't put the PrivateAssets="All" attribute on all the Fody and InlineIL.Fody PackageReference items, could you check that?

Datadog.Trace.Runner shouldn't be trying to run Fody at all.

@andrewlock
Copy link
Author

(Un) fortunately I checked that already.

Datadog.Trace has this:

<PackageReference Include="Fody" Version="6.8.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="InlineIL.Fody" Version="1.8.0" PrivateAssets="All" />

and Datadog.Trace.Coverage.Collector has the same (with the now-updated InlineIL.Fody version):

<PackageReference Include="Fody" Version="6.8.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="InlineIL.Fody" Version="1.8.0" PrivateAssets="All" />

@ltrzesniewski
Copy link
Member

Well, that would have been an easy explanation. In any case, Fody is trying to load two different versions of InlineIL I suppose, and that doesn't go very well.

The build log you posted seems to show the failure happens while building Datadog.Trace.Coverage.collector though, which may indeed point to an issue caused by node reuse.

While we probably should improve the error message, understanding the issue would be useful, since the assembly is loaded in a separate AssemblyLoadContext in order to avoid such problems in the first place... 馃槙

Currently, there's a separate load context for each solution directory, so maybe that could be a brittle approach? Note that when you're not using a solution, the project's parent directory is assumed to be the solution directory.

@andrewlock
Copy link
Author

andrewlock commented Apr 22, 2024

Currently, there's a separate load context for each solution directory, so maybe that could be a brittle approach? Note that when you're not using a solution, the project's parent directory is assumed to be the solution directory.

I think this could explain the issue then, both of these projects are in the same solution directory in our case 馃檪 And they're also in the same parent directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants