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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RepositoryTask's usage of IBuildEngine4.GetRegisteredTaskObject breaks under multiple AssemblyLoadContexts. #8478

Closed
teo-tsirpanis opened this issue Jan 12, 2023 · 6 comments
Assignees
Labels
needs-triage Have yet to determine what bucket this goes in.

Comments

@teo-tsirpanis
Copy link
Contributor

I enabled Central Package Management in my repository and put Microsoft.DotNet.ReproducibleBuilds in a GlobalPackageReference, which means that all my projects have Sourcelink enabled. Now when I build one of these projects, I get this error right here (also opened dotnet/runtime#80526 to track its unhelpfulness):

System.InvalidCastException:

[A]System.Runtime.CompilerServices.StrongBox`1[Microsoft.Build.Tasks.Git.GitRepository] cannot be cast to [B]System.Runtime.CompilerServices.StrongBox`1[Microsoft.Build.Tasks.Git.GitRepository].

Type A originates from 'System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' in the context 'Default' at location 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.2\System.Private.CoreLib.dll'.
Type B originates from 'System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e' in the context 'Default' at location 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\7.0.2\System.Private.CoreLib.dll'.

This error message tells us that the two GitRepositoryies are different. My guess is that MSBuild loads the task assembly once for each project, and they get the GitRepository from a previous invocation, which resides in a different ALC and fails to cast.

The approach of caching the repository information has to be revisited. For now I will limit SourceLink to just my library projects.

@rainersigwald
Copy link
Member

I haven't seen that error before in a RegisterTaskObject scenario. Do you by chance have a repro of this you could share?

MSBuild should cache ALCs by path to plugin assembly, so it shouldn't be the case that there are two GitRepository types. But that is definitely what the error is about, so something appears to have broken the "should".

@teo-tsirpanis
Copy link
Contributor Author

I created a repro in the https://github.com/teo-tsirpanis/Farkle/tree/msbuild-repro branch.

To run it you have to first run build.ps1 -t PrepareMSBuildTests, and then cd tests/Farkle.Tools.MSBuild.Tests and dotnet build.

Something I thought it might be the problem is that I'm using a globalPackagesFolder in nuget.config only for this test project and not its dependencies, but all Microsoft.Build.Tasks.Git.dll used in the tasks are the same. I also ran dotnet build-server shutdown before.

image

@tmat tmat transferred this issue from dotnet/sourcelink Feb 21, 2023
@rainersigwald rainersigwald added the needs-triage Have yet to determine what bucket this goes in. label Feb 21, 2023
@rainersigwald
Copy link
Member

Thanks for the repro. MSBuild Team: let's prioritize investigating this, since it appears to be a failure in a using-all-the-new-cool-stuff scenario.

@tmat
Copy link
Member

tmat commented May 23, 2023

Something I thought it might be the problem is that I'm using a globalPackagesFolder in nuget.config only for this test project and not its dependencies, but all Microsoft.Build.Tasks.Git.dll used in the tasks are the same.

@teo-tsirpanis So, yes, your build is misconfigured. You're loading two copies of Source Link - one from package cache in C:\Users\teo\.nuget\packages and the other from C:\Users\teo\code\Farkle\tests\packages. Don't do that :)

@tmat
Copy link
Member

tmat commented May 23, 2023

Fixed by dotnet/sourcelink#1034

@tmat tmat closed this as completed May 23, 2023
@teo-tsirpanis
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Have yet to determine what bucket this goes in.
Projects
None yet
Development

No branches or pull requests

5 participants