dotnet / dotnet/fsharp

TypeLoadException when accessing CustomAttributeData.ConstructorArguments in fsi

Open
#18,922 2 comments 0 reactions 0 assignees View on GitHub
Area-FSI Bug Impact-Medium
Dominant language
F#
Stars
4.3k
Forks
876
Avg merge
4d 11h
Merged PRs (30d)
131

Description

**Repro steps**

Simple reproduction, in `dotnet fsi` with `multiemit` on (as by default)

```fsharp
// run this in one prompt:

type MyCustomLogic() =
member this.DoSomething() = ()

type MyAnnotation(typ: System.Type) =
inherit System.Attribute()

// run this in another prompt:

[)>]
let getValue() = 42

match <@ getValue() @> with
| FSharp.Quotations.Patterns.Call(_, methodInfo, _) ->
methodInfo.GetCustomAttributesData()
|> Seq.iter (fun a -> a.ConstructorArguments |> Seq.iter (printfn "%A")) // fails with System.TypeLoadException
| _ -> ()
```
**Expected behavior**

If the referenced type and the attribute usage is in the same prompt, then there is no error. IL generation should correctly recognize which transient assembly a type referenced via `typeof` used for an attribute constructor is defined in.

**Actual behavior**

Error is like `System.TypeLoadException: Could not resolve type 'FSI_0003+MyCustomLogic' in assembly 'FSI-ASSEMBLY, Version=0.0.0.32763, Culture=neutral, PublicKeyToken=null'.`
This shows that the `MyCustomLogic` type is tried to be resolved from the transient assembly where the attribute is used, not where it is properly defined.

**Known workarounds**

Using `--multiemit-` argument for starting fsi.

**Related information**

Tested on dotnet SDKs 9.0.305 and 10.0.100-rc.1.25451.107 on Windows 11, both within and outside of Visual Studio.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.