<PublishTrimmed> broke the executable

Hello, i started to play around with monoGame in the last days, other than looking the feature it has to offer i tested different compilation arguments to see how small can a final publish folder become.

I read about this PublishTrimmed argument that use the IL Linker from what i’ve undertood.
Adding it to the csproj file or calling it from the terminal just work, the final folder is half the size of the previous compialtion, the only problem is that the game dosen’t start anymore.

I also tried doing the same on the samples project on github, and the result is the same, it work, but can’t start the game exe.
Opening it form the terminal doesn’t give any error or text.
Searching online haven’t helped me, someone only suggested to use and specify every DLL used in the project, but even doing so dosen’t work.

I’m working with .NET core 3.1 and MonoGame

Is this something related to monoGame? or to .NET? or to my project?
Can someone help me to figure this out?

You’ll need to add an rd.xml file to tell it not to trim out either your Content or the required file mscorlib.dll. You can use the one I linked or paste the following into a text editor and save it in the folder with your .csproj as rd.xml:

        <Assembly Name="MonoGame.Framework">
            <Type Name="Microsoft.Xna.Framework.Content.ListReader`1[[System.Char,mscorlib]]" Dynamic="Required All" />
        <Assembly Name="mscorlib" />

Then, add this to your .csproj file:

    <RdXmlFile Include="rd.xml" />

This works for MonoGame 3.8 and 3.8.1. You should find that your game and all your Content works when trimmed!

1 Like

I tried doing this with .NET 7, but it didn’t help. I did end up with more libraries being published than before adding rd.xml, though.

I did some more poking around and added this to my csproj:


This gives me the following output:

ILLink : Trim analysis warning IL2070: Microsoft.Xna.Framework.Content.ContentExtensions.GetDefaultConstructor(Type): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors', 'DynamicallyA
ccessedMemberTypes.NonPublicConstructors' in call to 'System.Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])'. The parameter 'type' of method 'Microsoft.Xna.Framework.Content.ContentExtensions.G
etDefaultConstructor(Type)' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to. 
ILLink : Trim analysis warning IL2057: Microsoft.Xna.Framework.Content.ContentTypeReaderManager.LoadAssetReaders(ContentReader): Unrecognized value passed to the parameter 'typeName' of method 'System.Type.GetType(Str
ing)'. It's not possible to guarantee the availability of the target type.

You might not be able to use PublishTrimmed with some libraries or extensions. Remember that it’s a general C# tool that may not work for every use case, like dynamically loaded content.

It might be possible to add nonworking libraries to the rd.xml so they won’t attempt to trim and return an error. The only errors in your message were related to Content, so you could start there. If adding the libraries to the XML works, it should still be better/smaller than having no trim at all.

For the record, adding this to your game’s csproj will make MonoGame to work with PublishTrimmed or PublishAot when building with .NET 7+:

    <TrimmerRootAssembly Include="MonoGame.Framework" />

The rd.xml formalism has been deprecated and won’t work anymore.

At some point we will update MonoGame to carry its own trimming information so that those directives are not necessary in your own game (this needs MonoGame to switch over .NET 8+).

If after adding this to your csproj your game still doesn’t work with trimming or AOT, it means that another dependency isn’t compatible or is missing trimming informations that you’ll have to figure out.

1 Like