Warm tip: This article is reproduced from stackoverflow.com, please click
f#

FSharp.Compiler.Service FSharpChecker fails to compile dynamic assembly when referencing IAsyncDispo

发布于 2020-04-22 11:09:30

Running the following in a dotnet core 3.1 console application fails to compile the assembly:

module Test

open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.Text

let source = """module Test

type TestType() = 
    interface System.IAsyncDisposable with
        member __.DisposeAsync() = 
            System.Threading.Tasks.ValueTask()
"""

let references = 
    [
        "Microsoft.Bcl.AsyncInterfaces"
    ]

[<EntryPoint>]
let main(args) =
    let checker = FSharpChecker.Create()

    let options = 
        { FSharpParsingOptions.Default with 
            SourceFiles = [|"empty.fs"|]
        }

    let sourceText = SourceText.ofString source   

    let parseResult = 
        checker.ParseFile("empty.fs", sourceText, options) |> Async.RunSynchronously

    let input = parseResult.ParseTree.Value

    let thisAssembly = 
        System.Reflection.Assembly.GetExecutingAssembly().GetName().Name

    let allReferences = 
        thisAssembly :: references 

    let errors, _, assemblyOpt =
        checker.CompileToDynamicAssembly([input], "TestProject", allReferences, None)
        |> Async.RunSynchronously

    0

assemblyOpt is None and the errors array contains the following messages:

The type 'IAsyncDisposable' is not defined in 'System'. Maybe you want one of the following:
   IDisposable
The type 'obj' is not an interface type
The type 'IAsyncDisposable' is not defined in 'System'. Maybe you want one of the following:
   IDisposable
The type 'IAsyncDisposable' is not defined in 'System'. Maybe you want one of the following:
   IDisposable
This type is not an interface type
No abstract or interface member was found that corresponds to this override
The value, constructor, namespace or type 'ValueTask' is not defined.
p_tyar_spec: from error
p_tyar_spec: from error

However, both Microsoft.Bcl.AsyncInterfaces, and System should contain IAsyncDisposable. What's causing the missing reference error?

Questioner
Durdsoft
Viewed
65
7sharp9 2020-02-16 01:30

I suspect its the assembly list is incomplete in some way, you can confirm this by altering the dynamic assembly generation function to the following:

checker.CompileToDynamicAssembly([input], "TestProject", allReferences, None, noframework = true)

noframework = true

Which works, (I just tested it)