- 
                Notifications
    You must be signed in to change notification settings 
- Fork 834
Description
Succinct description
When collectible = true is passed as an argument to FsiEvaluationSession.Create, the FSI-ASSEMBLY is never unloaded, contrary to expectations.
Repro steps
The problem can be reproduced by following these steps:
- Run the provided collectionTest function in the Collectible code generation section of the FCS API documentation: https://fsharp.github.io/fsharp-compiler-docs/fcs/interactive.html#Collectible-code-generation
- Observe that the FSI-ASSEMBLY is never unloaded.
Expected behavior
When collectible = true is set, the FSI-ASSEMBLY should be unloaded when it's no longer needed.
Actual behavior
The FSI-ASSEMBLY is never unloaded, even when collectible = true.
Known workarounds
There are currently no known workarounds for this issue.
Potential solution
A potential fix might involve modifying the defineDynamicAssemblyAndLog function in ilreflect.fs to ensure that the assembly is created within a CollectibleAssemblyLoadContext. Here's the current implementation of the function:
let defineDynamicAssemblyAndLog (asmName, flags, asmDir: string) =
    let asmB = AssemblyBuilder.DefineDynamicAssembly(asmName, flags)
    if logRefEmitCalls then
        printfn "open System"
        printfn "open System.Reflection"
        printfn "open System.Reflection.Emit"
        printfn
            "let assemblyBuilder%d = System.AppDomain.CurrentDomain.DefineDynamicAssembly(AssemblyName(Name=\"%s\"), enum %d, %A)"
            (abs <| hash asmB)
            asmName.Name
            (LanguagePrimitives.EnumToValue flags)
            asmDir
    asmBRelated information
Operating system: Windows 10 Pro 21H2 (19044.3086)
.NET Runtime kind: .NET 7.0.306
Editing Tools: Visual Studio Code