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

Can not use protobuf-net with ILRepack #1115

Open
dimsa opened this issue Dec 12, 2023 · 4 comments
Open

Can not use protobuf-net with ILRepack #1115

dimsa opened this issue Dec 12, 2023 · 4 comments

Comments

@dimsa
Copy link

dimsa commented Dec 12, 2023

I try to merge libraries into one file using ILRepack

ILRepack.exe /union /out:repack.dll"ProbufRepack.dll" "System.Collections.Immutable.dll" "protobuf-net.Core.dll" "protobuf-net.dll"

And get the following error:

Mono.Cecil.ResolutionException: Failed to resolve System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes
   at Mono.Cecil.Mixin.CheckedResolve(TypeReference self)
   at Mono.Cecil.MetadataBuilder.GetConstantType(TypeReference constant_type, Object constant)
   at Mono.Cecil.MetadataBuilder.AddConstant(IConstantProvider owner, TypeReference type)
   at Mono.Cecil.MetadataBuilder.AddField(FieldDefinition field)
   at Mono.Cecil.MetadataBuilder.AddFields(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddType(TypeDefinition type)
   at Mono.Cecil.MetadataBuilder.AddTypeDefs()
   at Mono.Cecil.MetadataBuilder.BuildTypes()
   at Mono.Cecil.MetadataBuilder.BuildModule()
   at Mono.Cecil.ModuleWriter.BuildMetadata(ModuleDefinition module, MetadataBuilder metadata)
   at Mono.Cecil.ModuleWriter.WriteModuleTo(ModuleDefinition module, Stream stream, WriterParameters parameters)
   at Mono.Cecil.ModuleDefinition.Write(String fileName, WriterParameters parameters)
   at ILRepacking.ILRepack.Repack()
   at ILRepacking.Application.Main(String[] args)

Please find attached simple project to reproduce it. ILRepack and all libraries can be found in bin/Release/net6.0 Protobuf and ILRepack have last versions.

ProbufRepack.zip

I also tried to download sources of Probuf-net.Core, I've found this types, but was not able to change this behavior.

@mgravell
Copy link
Member

The only weird thing about that type is that there's a type-forward in place, and honestly: I can't think of a valid reason for there to be one. I've dropped it here - I can't guarantee that it'll help, but it does seem a likely cause. However, this sounds like a bug/glitch in ILRepack

@dimsa
Copy link
Author

dimsa commented Dec 12, 2023

@mgravell thank you. Removing of forwarded type was the first thing I tried because these types can be found using dotPeek. But it didn't help. I created the similar bug in ILRepack repository gluck/il-repack#329 with the link to https://github.com/protobuf-net/protobuf-net/blob/main/src/protobuf-net.Core/Internal/DynamicallyAccessedMembersAttribute.cs

I'll put comment here after the replying of ILRepack developers. Thank you.

@dimsa
Copy link
Author

dimsa commented Dec 12, 2023

I made additional investigation. Firstly, I think ILRepack Developer will never reply to me :-) So, I downloaded ILRepack and found issue. The System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes can not be resolved because it's try to find it in System.Runtime. I think it's because of assemblies' order that it uses, but it's very hard to me to understand all in details in short time period. Maybe because of Scope that is System.Runtime, but real place of this type is Protobuf-net.
DebugPicture

Next, I've returned to Protobuf with new knowledge and make very quick and brutal fix that is quite enough for me at the moment. https://github.com/dimsa/protobuf-net/pull/1/files#diff-9e0c91d2aae978a7434c2f7d495b55dc790ae75230a0a142baa47cfbc1f89018 (Sorry, at the first time I made PR to your repository, but I really didn't want it)

So, the issue was in Namespace that was used. I renamed attribute only to be sure that it will not be exported from System.Diagnostics.CodeAnalysis. Then I removed all .NET except of net6 to be sure that tests work fine.

To summarize... I don't think that it is a bug of protobuf-net, but not sure if it's the bug of ILRepack. To fix this, it's possible to move DynamicallyAccessedMembersAttribute to another namespace (That was easier to me because your code and project structure much clearer to me than ILRepack).

But the best solution is an update of ILRepack if it's possible. FYI: @timotei @gluck

P.S. Hope my little investigation will help someone

@mgravell
Copy link
Member

mgravell commented Dec 12, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants