Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[One .NET] exclude .dll/.pdb files from @(None) or @(Content) (#6514)
Context: https://github.com/jonathanpeppers/android-pipe/blob/c313259b782bff40204e1a1ca988659dc7d3180b/csharp/Benchmark.csproj#L25 When using BenchmarkDotNet in a .NET 6 Android app for the first time, I hit this build error: Microsoft.Android.Sdk.AssemblyResolution.targets(106,5): error XAPRAS7009: System.InvalidOperationException: PE image does not have metadata. at System.Reflection.PortableExecutable.PEReader.GetMetadataBlock() at System.Reflection.PortableExecutable.PEReader.GetMetadata() at System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(PEReader peReader, MetadataReaderOptions options, MetadataStringDecoder utf8Decoder) at System.Reflection.Metadata.PEReaderExtensions.GetMetadataReader(PEReader peReader) at Xamarin.Android.Tasks.ProcessAssemblies.DeduplicateAssemblies(List`1 output, Dictionary`2 symbols) at Xamarin.Android.Tasks.ProcessAssemblies.RunTask() at Microsoft.Android.Build.Tasks.AndroidTask.Execute() in C:\src\xamarin-android\external\xamarin-android-tools\src\Microsoft.Android.Build.BaseTasks\AndroidTask.cs:line 17 I could also reproduce the issue in a test. The problem is that `Microsoft.Diagnostics.Tracing.TraceEvent.props` includes Windows native `.dll` files: <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\x86\KernelTraceControl.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\x86\KernelTraceControl.dll"> <Link>x86\KernelTraceControl.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\x86\KernelTraceControl.Win61.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\x86\KernelTraceControl.Win61.dll"> <Link>x86\KernelTraceControl.Win61.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\x86\msdia140.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\x86\msdia140.dll"> <Link>x86\msdia140.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\x86\msvcp140.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\x86\msvcp140.dll"> <Link>x86\msvcp140.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\x86\vcruntime140.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\x86\vcruntime140.dll"> <Link>x86\vcruntime140.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\amd64\KernelTraceControl.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\amd64\KernelTraceControl.dll"> <Link>amd64\KernelTraceControl.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\amd64\msdia140.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\amd64\msdia140.dll"> <Link>amd64\msdia140.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\amd64\msvcp140.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\amd64\msvcp140.dll"> <Link>amd64\msvcp140.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\amd64\vcruntime140.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\amd64\vcruntime140.dll"> <Link>amd64\vcruntime140.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> <None Condition="Exists('$(MSBuildThisFileDirectory)..\lib\native\amd64\vcruntime140_1.dll')" Include="$(MSBuildThisFileDirectory)..\lib\native\amd64\vcruntime140_1.dll"> <Link>amd64\vcruntime140_1.dll</Link> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <Visible>False</Visible> </None> This isn't great; we don't really want users to be able to use `@(None)` to include random `.dll` files… I could workaround the problem by using this in the `.csproj`: <None Remove="@(None->WithMetadataValue('Extension', '.dll'))" /> Reviewing the `.binlog`, I found the only way to identify `%(CopyToOutputDirectory)` items was to do: <ResolvedFileToPublish Remove="@(_SourceItemsToCopyToPublishDirectory)" /> Even though `@(_SourceItemsToCopyToPublishDirectory)` has a private name, it seems like the only way to fix this? If it was ever renamed, we have a test and the above code would change to a no-op. The test now passes, excluding these files from the build.
- Loading branch information