Bug 55681 - System.Reflection.Emit.ModuleBuilder.build_metadata bug when running FAKE's test suite
Summary: System.Reflection.Emit.ModuleBuilder.build_metadata bug when running FAKE's t...
Alias: None
Product: Runtime
Classification: Mono
Component: Reflection ()
Version: 5.0 (2017-02)
Hardware: PC Mac OS
: --- normal
Target Milestone: 15.2
Assignee: Aleksey Kliger
Depends on:
Reported: 2017-04-27 10:45 UTC by Jason Imison
Modified: 2017-05-09 14:58 UTC (History)
4 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Jason Imison 2017-04-27 10:45:09 UTC
I see the following error when running ./build.sh from https://github.com/fsharp/FAKE using Mono Will try and come up with a smaller repro.

when running script
requested token for ConstructorBuilder


  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) System.Reflection.Emit.ModuleBuilder.build_metadata (System.Reflection.Emit.ModuleBuilder) [0x00000] in <5357e6452d21439088fd8589dfcf1134>:0
  at System.Reflection.Emit.ModuleBuilder.Save () [0x001ad] in <5357e6452d21439088fd8589dfcf1134>:0
  at System.Reflection.Emit.AssemblyBuilder.Save (string,System.Reflection.PortableExecutableKinds,System.Reflection.ImageFileMachine) [0x0022b] in <5357e6452d21439088fd8589dfcf1134>:0
  at System.Reflection.Emit.AssemblyBuilder.Save (string) [0x00000] in <5357e6452d21439088fd8589dfcf1134>:0
  at Fake.FSIHelper.handleCaching<a_REF> (bool,Yaaf.FSharp.Scripting.IFsiSession,a_REF,System.IO.DirectoryInfo,Fake.FSIHelper/CacheInfo) [0x00046] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Fake.FSIHelper.runScriptUncached (bool,string,System.Collections.Generic.IEnumerable`1<string>,bool,Fake.FSIHelper/CacheInfo,System.IO.TextWriter,System.IO.TextWriter) [0x00236] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Fake.FSIHelper.runFAKEScriptWithFsiArgsAndRedirectMessages (bool,Fake.FSIHelper/FsiArgs,System.Collections.Generic.IEnumerable`1<System.Tuple`2<string, string>>,Microsoft.FSharp.Core.FSharpFunc`2<string, Microsoft.FSharp.Core.Unit>,Microsoft.FSharp.Core.FSharpFunc`2<string, Microsoft.FSharp.Core.Unit>,bool) [0x0015d] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Fake.FSIHelper.executeBuildScriptWithArgsAndFsiArgsAndReturnMessages (string,string[],string[],bool) [0x00038] in <5901bfb57c20dbeda7450383b5bf0159>:0
  at Test.FAKECore.when_running_script.RunExplicitWithResult (string,string[],string[],bool) [0x00023] in <4e523b3abfd44163b8ac5d7c21429614>:0
  at Test.FAKECore.when_running_script/<>c.<.ctor>b__25_1 () [0x0006c] in <4e523b3abfd44163b8ac5d7c21429614>:0
Comment 1 matthi.d 2017-04-27 12:05:39 UTC
Whatever this bug is, it is related to emiting (via Reflection.Emit) the output of the F# compiler for this code. This might help while analysing this.

open System
type MyAttr() =
  inherit Attribute()
  do failwith "test"

type Test () =
  do ignore ()
[<assembly: MyAttr()>]
do ()

This is a test-case we have in FAKE test-suite.
Comment 2 Jason Imison 2017-04-27 12:19:27 UTC
FAKE has since been updated to detect Mono 5.0.0 and not do assembly cache writing https://github.com/fsharp/FAKE/pull/1536/files?w=1
Comment 3 Jason Imison 2017-04-27 13:22:36 UTC
@matthid: I can't repro with this isolated script. Any ideas?

#r "FSharp.Compiler.Service.12.0.5/lib/net45/FSharp.Compiler.Service.dll"
open Microsoft.FSharp.Compiler.Interactive.Shell

open System.IO
open System.Text

// Intialize output and input streams
let sbOut = new StringBuilder()
let sbErr = new StringBuilder()
let inStream = new StringReader("")
let outStream = new StringWriter(sbOut)
let errStream = new StringWriter(sbErr)

// Build command line arguments & start FSI session
let argv = [| "C:\\fsi.exe" |]
let allArgs = Array.append argv [|"--noninteractive"|]

let fsiConfig = FsiEvaluationSession.GetDefaultConfiguration()
let fsiSession = FsiEvaluationSession.Create(fsiConfig, allArgs, inStream, outStream, errStream)  
let script =
    open System
    type MyAttr() =
      inherit Attribute()
      do failwith "test"
    type Test () =
      do ignore ()
    [<assembly: MyAttr()>]
    do ()
fsiSession.EvalScript script
let builder =
    match fsiSession.DynamicAssembly with
    | :? System.Reflection.Emit.AssemblyBuilder as builder -> builder
    | _ -> failwith "The DynamicAssembly property is no AssemblyBuilder!"
Comment 4 Jason Imison 2017-04-27 17:08:36 UTC
I've tried and failed to come with a repro that doesn't involve using the full FAKE .exe

You should be able to repro with the following steps though

Create a file named build.fsx with the following contents

open System
type MyAttr() =
  inherit Attribute()
  do failwith "test"

type Test () =
  do ignore ()
[<assembly: MyAttr()>]
do ()

nuget install FAKE -Version 4.9.6
mono FAKE.4.9.6/tools/FAKE.exe build.fsx
Comment 5 Aleksey Kliger 2017-04-27 18:51:00 UTC
In mono_image_add_cattrs () we call mono_image_create_token on a ConstructorBuilder object, but that function expects to only be called on System.Reflection objects, not on System.Reflection.Emit objects. (It assumes calls are coming from managed code, and there everything is routed through ModuleBuilder.GetToken(MemberInfo) which handles all the Builders in managed code).

I should have C# testcases shortly.  (I think there are actually two bad cases here. The one like the F# code in Comment 4 where the custom attribute is applied to the assembly that it is defined in, and another one where we have two assembly builders going and one of them makes an attribute that's applied to the other).
Comment 6 Jason Imison 2017-04-29 14:20:17 UTC
Fixed by https://github.com/mono/mono/pull/4769 Thanks Aleksey!
Comment 8 Roshan Mankani 2017-05-09 09:40:59 UTC
Hello @Jason Imison,
Can you please provide proper steps or description with expected condition, for verify this bug.
Comment 9 Aleksey Kliger 2017-05-09 14:58:51 UTC

See Jason's Comment 4 for the reproduction steps.

The expected output is that the last step "mono FAKE.4.9.6/tools/FAKE.exe build.fsx" exits without an error.