tunnelvisionlabs / ReferenceAssemblyAnnotator

IL weaver to add nullability annotations to .NET reference assemblies
MIT License
71 stars 8 forks source link

"AnnotatorBuildTask" task failed unexpectedly with Mono.Cecil.AssemblyResolutionException #37

Closed jnm2 closed 5 years ago

jnm2 commented 5 years ago

This is alpha 77 on Shouldly's Ubuntu build:

Mono.Cecil.AssemblyResolutionException: Failed to resolve assembly: 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj]

I'm not sure whether this is recoverable or whether it should just be turned into a nicer error.

Stack trace:

``` at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name, ReaderParameters parameters) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.BaseAssemblyResolver.Resolve(AssemblyNameReference name) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.DefaultAssemblyResolver.Resolve(AssemblyNameReference name) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.MetadataResolver.Resolve(TypeReference type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ModuleDefinition.Resolve(TypeReference type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.TypeReference.Resolve() [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.Mixin.CheckedResolve(TypeReference self) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.SignatureReader.ReadCustomAttributeEnum(TypeReference enum_type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.SignatureReader.ReadCustomAttributeElementValue(TypeReference type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.SignatureReader.ReadCustomAttributeElement(TypeReference type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.SignatureReader.ReadCustomAttributeFixedArgument(TypeReference type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.SignatureReader.ReadCustomAttributeNamedArgument(Collection`1& fields, Collection`1& properties) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.SignatureReader.ReadCustomAttributeNamedArguments(UInt16 count, Collection`1& fields, Collection`1& properties) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.MetadataReader.ReadCustomAttributeSignature(CustomAttribute attribute) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.CustomAttribute.b__35_0(CustomAttribute attribute, MetadataReader reader) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ModuleDefinition.Read[TItem](TItem item, Action`2 read) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.CustomAttribute.get_ConstructorArguments() [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ImmediateModuleReader.ReadCustomAttributes(ICustomAttributeProvider provider) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ImmediateModuleReader.ReadType(TypeDefinition type) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ImmediateModuleReader.ReadTypes(Collection`1 types) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ImmediateModuleReader.ReadModule(ModuleDefinition module, Boolean resolve_attributes) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ImmediateModuleReader.b__2_0(ModuleDefinition module, MetadataReader reader) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ModuleDefinition.Read[TItem](TItem item, Action`2 read) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ImmediateModuleReader.ReadModule() [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ModuleReader.CreateModule(Image image, ReaderParameters parameters) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ModuleDefinition.ReadModule(Disposable`1 stream, String fileName, ReaderParameters parameters) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Mono.Cecil.ModuleDefinition.ReadModule(String fileName, ReaderParameters parameters) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at TunnelVisionLabs.ReferenceAssemblyAnnotator.Program.Main(Nullable`1 log, String referenceAssembly, String annotatedReferenceAssembly, String outputAssembly) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at TunnelVisionLabs.ReferenceAssemblyAnnotator.AnnotatorBuildTask.Execute() [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute() [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask) [/home/appveyor/projects/shouldly/src/Shouldly/Shouldly.csproj] ```
sharwell commented 5 years ago

@jbevain Is there a way we can get Cecil to treat this as an opaque/unresolved error type that is simply preserved when we write the modified assembly back to disk?

sharwell commented 5 years ago

@jnm2 Worst case, we can make it report a warning like it does for other skipped assemblies. The build author can control whether the warning is treated as a build error.

jbevain commented 5 years ago

@sharwell I think this happens because you use the Immediate mode that creates the entire DOM, including custom attributes.

If you use the delayed one, it might be able to optimize the reading/writing of the custom attributes straight to bytes.

What happens here is that Cecil reads a custom attribute with an enum, and there's no way to know the size of the enum unless you resolve its type definition.

jnm2 commented 5 years ago

Interestingly, there are a ton of "Failed to resolve assembly" messages when building an empty net451 project on Windows. These are at the info level rather than the error caused by the uncaught AssemblyResolutionException when building on Ubuntu. I wonder why Cecil on Windows isn't throwing AssemblyResolutionException the same way.

[...]
  Cannot find a matching type for System.Configuration.IriParsingElement
  Failed to resolve assembly: 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
  Cannot find a matching type for System.Configuration.IdnElement
  Failed to resolve assembly: 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
  Cannot find a matching type for System.Net.Mail.SmtpAccess
  Failed to resolve assembly: 'System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
  Cannot find a matching type for System.Net.Mail.SmtpPermissionAttribute
  Failed to resolve assembly: 'System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
  Cannot find a matching type for System.Net.Mail.SmtpPermission
  Failed to resolve assembly: 'System.Security.Permissions, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
  Cannot find a matching type for System.Configuration.SettingsBase
  Failed to resolve assembly: 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
  Cannot find a matching type for System.Configuration.ApplicationSettingsBase
  Failed to resolve assembly: 'System.Configuration.ConfigurationManager, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
[...]

802 failures total: net451 build log.txt

jnm2 commented 5 years ago

That's produced by this code:

https://github.com/tunnelvisionlabs/ReferenceAssemblyAnnotator/blob/2563160c99a8c6b947cd5432d0304662609e5f2c/TunnelVisionLabs.ReferenceAssemblyAnnotator/Program.cs#L238-L246

Once ReadingMode.Deferred is used it will be interesting to see if/where we need to add another catch block.