-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Throw BadImageFormatException when missing PE metadata #6270
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're checking this here, we should add the same check in MetadataReader.cs. This is what it looks like currently, which could also throw.
if (_peReader != null)
{
if (_peReader.HasMetadata)
{
_reader = _peReader.GetMetadataReader();
}
}
It seems MetadataReader already has a try/catch surrounding the HasMetadata check, which closes the reader, meaning |
This looks good although I wonder if pulling the code into a helper and calling it from both AssemblyNameExtension and AssemblyInformation wouldn't be slightly more elegant. |
So maybe an extension method |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the "prescribed / designed" way of referencing non-CLR projects is telling the sdk to not treat the ProjectReference output as a managed assembly. But I'm not 100% sure, I couldn't find any docs on the expected way to do this (@KathleenDollard @dsplaisted). Might be safer to go down that route than to allow native assemblies to flow into RAR which might have unknown, cascading consequences for the downstream code that reads RAR outputs.
How does RAR behave if you pass a non-PE file? |
Generally, a BadImageFormatException is thrown, which RAR catches and aborts with "File does not contain managed metadata". For example:
|
Not sure if relevant, but Microsoft.Build.Tasks.Core.dll has an Also here's a way to find out if an assembly is managed without throwing: |
@cdmihai the project reference stuff is certainly valid, but I'm questioning whether that should be done as a separate PR? This feels nicely self-contained and relatively isolated and it does fix the issue at hand? |
Thanks @FiniteReality! |
Fixes #6200
Context
InvalidOperationException
was being thrown byAssemblyInformation.CorePopulateMetadata
, causing the ResolveAssemblyReference task to fail rather than disregarding the file.Changes Made
I copied the code from AssemblyNameExtension to ensure there is metadata present before getting a metadata reader:
msbuild/src/Shared/AssemblyNameExtension.cs
Lines 214 to 231 in 6819f7a
Testing
I looked for unit tests for AssemblyInformation, but could not find any, so I didn't add a unit test for this case. However, I successfully built the project that was reproducing the error in #6200, indicating that the change did indeed fix the issue.