-
Notifications
You must be signed in to change notification settings - Fork 322
Call to Type.GetType for a missing type causes FSI on Mono to produce an erroneous error #483
Comments
It's worth mentioning if you compile the same code as an exe with fsc and run that on Mono, it works as expected with no errors. [<EntryPoint>]
let main _ =
let log4netType = System.Type.GetType("log4net.Config.XmlConfigurator, log4net")
let exists = log4netType <> null
do printfn "%A" exists
0
|
Looks like your missing a #r reference in the script. Presumably in the fsproj you will have a dll reference. |
The point is that there is no #r, and there should not be. Type.GetType returns null if the type was not found, but in FSI on Mono it also causes an error, which isn't right. We #r in a third party assembly, which uses Type.GetType internally to test for the presence of log4net; if it exists it uses it, if not, it doesn't. Their normally safe type test causes our code to crash in Mono FSI, which it shouldn't. |
Reference resolution differs between windows and mono in fsharpi, you need a #r or #i for mono. |
I'm afraid I don't understand what you mean. If the behaviour was actually different between Mono and Windows I would expect the exe version of the same code to fail in the same way on Mono. It does not; indeed it runs as expected, returning null and not failing. To be crystal clear, we are not referencing log4net in the exe version either (see above example using fsc instead of fsi). |
The behaviour of reference resolution in fsi differs between windows and mono. In Windows fsi will use msbuild to try and figure out a reference path, in mono it will use a simple path. |
I think we might be talking cross purposes here; it's my fault, I backwards named the "exists" variable in the examples above (fixed, you might want to look at them again, sorry). The reference is not loaded on Windows, nor on Mono as it (correctly) cannot be found on either platform. This is intentional; the library that tries to load log4net will simply not use it if it is not found (ie if GetType returns null). However, only on Mono FSI does it cause an error. It doesn't even seem to be a proper exception that causes the code to fail; the code runs just fine, but FSI prints an error and fails with an exit code after the code has successfully run just fine. |
I think you are both coming at the same issue from different angles. I agree with @daniel-chambers that a FSI installs a backup assembly resolution handler to be used if the standard resolution fails: https://github.com/fsharp/fsharp/blob/master/src/fsharp/fsi/fsi.fs#L1452 The exception is being thrown in there, because it does in fact fail. This explains the difference in behaviour between running via FSC and FSI. However, it's surprising that this isn't also seen on Windows. It may be down to a difference in the implementation of |
Labelling this as a behaviour divergence bug |
Added a potential fix for this. |
Not sure whether to post this here or over on the VisualFSharp repo, but it's to do with F# on Mono, so I'll try here first. Please let me know if you'd like me to move it.
Given the following
Reflection.fsx
script:on Mono if you run
fsharpi Reflection.fsx
you get the following output:whereas on Windows using .NET fsi (
fsi Reflection.fsx
) you get the expected behaviour and output:Interestingly, on Mono the error was printed and FSI returns an error exit code, but the code in the script actually did continue to execute and finish just fine, even though theoretically there was a fatal error at Line 1.
This is causing issues for us because we're trying to do builds on Mono using FAKE, and the FAKE builds reference third party assemblies that attempt to load other assemblies at runtime using Type.GetType, which causes our builds to fail because FSI spits this error, even though the builds run just fine. When the builds are run on Windows with .NET FSI, they work as expected (no error).
I've tried this with F# 3.1 on Mono 4.0.1, F# 4 on Mono 4.0.3, and F# 4 on Mono 4.2.0 Alpha; same behaviour in all cases.
The text was updated successfully, but these errors were encountered: