fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Trimmed assemblies should not contain sig and opt data

Open kerams opened this issue 2 years ago • 8 comments

Is your feature request related to a problem? Please describe.

F# assemblies in a trimmed application contain signature and optimization data.

Describe the solution you'd like

These resources serve no purpose in a deployed application and should be stripped.

Describe alternatives you've considered

A post-build task that iterates over and modifies the emitted assemblies.

Additional context

8.0.100-preview.6.23330.14

kerams avatar Jul 17 '23 08:07 kerams

I don't think we want to be tied to specifics of trimming tasks implementation in FSharp.Build, what we can theoretically do, is generate the ILLink.Substitutions.xml file, however this can become very tricky very soon, if user wants a custom one.

So we probably want to: a. Generate default ILLink.Substituions.xml b. A way for user to opt-out, if they want a custom one.

@kerams does this sound feasible?

vzarytovskii avatar Jul 17 '23 09:07 vzarytovskii

No clue, I know little about the trimming infrastructure.

kerams avatar Jul 17 '23 09:07 kerams

In case anyone needs this, here's a snippet that removes the aforementioned resources on all F# assemblies using Mono.Cecil. Run it once trimming has completed.

for f in Directory.EnumerateFiles (serverDir, "*.dll") do
    use m = ModuleDefinition.ReadModule (f, ReaderParameters (ReadWrite = true))

    let toRemove =
        m.Resources
        |> Seq.filter (fun x -> x.Name.StartsWith "FSharpSignature" || x.Name.StartsWith "FSharpOptimization")
        |> Seq.toList

    if not toRemove.IsEmpty then
        for res in toRemove do
            m.Resources.Remove res |> ignore

        m.Write ()

kerams avatar Jul 17 '23 09:07 kerams

In case anyone needs this, here's a snippet that removes the aforementioned resources on all F# assemblies using Mono.Cecil. Run it once trimming has completed.

for f in Directory.EnumerateFiles (serverDir, "*.dll") do
    use m = ModuleDefinition.ReadModule (f, ReaderParameters (ReadWrite = true))

    let toRemove =
        m.Resources
        |> Seq.filter (fun x -> x.Name.StartsWith "FSharpSignature" || x.Name.StartsWith "FSharpOptimization")
        |> Seq.toList

    if not toRemove.IsEmpty then
        for res in toRemove do
            m.Resources.Remove res |> ignore

        m.Write ()

It is not needed, just create the xml file for your library, illink will trim it.

vzarytovskii avatar Jul 17 '23 10:07 vzarytovskii

For every single referenced assembly, every time I add a new one? I don't think so :).

kerams avatar Jul 17 '23 10:07 kerams

For every single referenced assembly, every time I add a new one? I don't think so :).

For your assemblies, if you want trimming to be supported. We don't want to be trimming resources from anyone else's assemblies for sure.

vzarytovskii avatar Jul 17 '23 10:07 vzarytovskii

I don't see why I would ever want to retain signature and optimization data when publishing, regardless of where an assembly comes from, or even if it is actually being trimmed. Feel free to close - I can just use that script.

kerams avatar Jul 17 '23 10:07 kerams

I will fix it, it is a bug not a feature request.

KevinRansom avatar Jul 17 '23 16:07 KevinRansom