From c31b45b15b2879ca9c1a7542c20d9cd111605a75 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Tue, 30 Nov 2021 06:16:26 -0600 Subject: [PATCH] [One .NET] exclude .dll/.pdb files from @(None) or @(Content) (#6514) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: x86\KernelTraceControl.dll PreserveNewest False x86\KernelTraceControl.Win61.dll PreserveNewest False x86\msdia140.dll PreserveNewest False x86\msvcp140.dll PreserveNewest False x86\vcruntime140.dll PreserveNewest False amd64\KernelTraceControl.dll PreserveNewest False amd64\msdia140.dll PreserveNewest False amd64\msvcp140.dll PreserveNewest False amd64\vcruntime140.dll PreserveNewest False amd64\vcruntime140_1.dll PreserveNewest False 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`: Reviewing the `.binlog`, I found the only way to identify `%(CopyToOutputDirectory)` items was to do: 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. --- ...icrosoft.Android.Sdk.AssemblyResolution.targets | 14 +++++++------- .../Xamarin.Android.Build.Tests/XASdkTests.cs | 13 +++++++++++++ .../Tests/Xamarin.ProjectTools/Common/DotNetCLI.cs | 3 ++- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets index b34c5bbdd6e..f109180e464 100644 --- a/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets +++ b/src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.AssemblyResolution.targets @@ -42,12 +42,19 @@ _ResolveAssemblies MSBuild target. DependsOnTargets="_FixupIntermediateAssembly;ResolveReferences;ComputeFilesToPublish;_AndroidAot" Returns="@(ResolvedFileToPublish)"> + + @@ -95,14 +102,7 @@ _ResolveAssemblies MSBuild target. <_ResolvedAssemblyFiles Include="@(ResolvedFileToPublish)" Condition=" '%(ResolvedFileToPublish.Extension)' == '.dll' " /> <_ResolvedSymbolFiles Include="@(ResolvedFileToPublish)" Condition=" '%(ResolvedFileToPublish.Extension)' == '.pdb' " /> - <_UnusedConfigFiles Include="@(ResolvedFileToPublish)" Condition=" '%(ResolvedFileToPublish.Extension)' == '.config' " /> - GetDefaultCommandLineArgs (string verb, string target = null, strin if (string.IsNullOrEmpty (BuildLogFile)) BuildLogFile = Path.Combine (testDir, "build.log"); + var binlog = string.IsNullOrEmpty (target) ? "msbuild" : target; var arguments = new List { verb, $"\"{projectOrSolution}\"", "/noconsolelogger", $"/flp1:LogFile=\"{BuildLogFile}\";Encoding=UTF-8;Verbosity={Verbosity}", - $"/bl:\"{Path.Combine (testDir, $"{target}.binlog")}\"" + $"/bl:\"{Path.Combine (testDir, $"{binlog}.binlog")}\"" }; if (!string.IsNullOrEmpty (target)) { arguments.Add ($"/t:{target}");