dotnet / fsharp

The F# compiler, F# core library, F# language service, and F# tooling integration for Visual Studio
https://dotnet.microsoft.com/languages/fsharp
MIT License
3.9k stars 782 forks source link

Can't use System.Data.Odbc in F# Interactive on Linux #8502

Closed akdor1154 closed 4 years ago

akdor1154 commented 4 years ago

System.Data.Odbc does not work in fsi, but does work when compiled with the same dotnet into a proper project.

Repro steps in.fsx:

let getSql (sql:string) =
    use connection = new OdbcConnection(connectionString)
    connection.Open()
    ()

printfn "%A" (getSql "select 1 as head from dummy")

Executing the above with dotnet fsi test.fsx gives the error

System.PlatformNotSupportedException: System.Data.ODBC is not supported on this platform.
   at System.Data.Odbc.OdbcConnection..ctor(String connectionString)
   at FSI_0002.getSql(String sql)
   at <StartupCode$FSI_0002>.$FSI_0002.main@()

However renaming test.fsx to test.fs, adding it to a netcoreapp3.1 fsproj, and running with dotnet run works fine (and I can pull data from the db as expected, I've omitted this from the above example as the code as the failure happens before any command.Execute happens).

Using Paket packages:

System.Data.Odbc 4.7
FSharp.Data 3.3.3

All of this is demonstrated in: repro.zip

Related information

Provide any related information (optional):

Runtime Environment: OS Name: ubuntu OS Version: 18.04 OS Platform: Linux RID: ubuntu.18.04-x64 Base Path: /usr/share/dotnet/sdk/3.1.101/

Host (useful for support): Version: 3.1.1 Commit: a1388f194c

.NET Core SDKs installed: 2.2.402 [/usr/share/dotnet/sdk] 3.1.101 [/usr/share/dotnet/sdk]

.NET Core runtimes installed: Microsoft.AspNetCore.All 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All] Microsoft.AspNetCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.AspNetCore.App 3.1.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App] Microsoft.NETCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App] Microsoft.NETCore.App 3.1.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs: https://aka.ms/dotnet-download


* Editing Tools (e.g. Visual Studio Version, Visual Studio)
cartermp commented 4 years ago

In .NET Interactive the exception you see doesn't happen, but there is a different exception thrown:

#r "nuget: System.Data.Odbc"

open System.Data.Odbc

let getSql (sql:string) =
    use connection = new OdbcConnection("doot")
    connection.Open()
    ()

printfn "%A" (getSql "select 1 as head from dummy")
System.BadImageFormatException: Could not load file or assembly 'System.Data.Odbc, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL'. Reference assemblies should not be loaded for execution.  They can only be loaded in the Reflection-only loader context. (0x80131058)
File name: 'System.Data.Odbc, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.

Indicates some other problem going on here, perhaps a bug.

cartermp commented 4 years ago

cc @KevinRansom @jonsequitur

dsyme commented 4 years ago

I now get this below which matches the bug report (I haven;t tried the repro zip etc.).

I would imagine this is something to do with how System.Data.Odbc probes for initialization config files etc. with respect to the launched executable. There may be a workaround

I suspect this isn't an F# bug though may be a System.Data.Odbc bug

jovyan@jupyter-dotnet-2dinteractive-2do59o9bjn:~/Notebooks$ dotnet fsi --langversion:preview

Microsoft (R) F# Interactive version 10.9.1.0 for F# 4.7
Copyright (c) Microsoft Corporation. All Rights Reserved.

For help type #help;;

> #r "nuget: System.Data.Odbc"
-     
- open System.Data.Odbc
- 
- let getSql (sql:string) =
-     use connection = new OdbcConnection("doot")
-     connection.Open()
-     ()
- 
- printfn "%A" (getSql "select 1 as head from dummy")
- ;;
[Loading /tmp/nuget/192--4d6ef77e-973a-4204-a796-adccfce6674f/Project.fsproj.fsx]
namespace FSI_0002.Project

System.PlatformNotSupportedException: System.Data.ODBC is not supported on this platform.
   at System.Data.Odbc.OdbcConnection..ctor(String connectionString)
   at FSI_0003.getSql(String sql)
   at <StartupCode$FSI_0003>.$FSI_0003.main@()
Stopped due to error
> 
cartermp commented 4 years ago

Yes, that exception implies it is not supported on Linux

akdor1154 commented 4 years ago

It works in in normal compiled fsharp project, though...

KevinRansom commented 4 years ago

I will investigate this when I get time.

zpodlovics commented 4 years ago

You have to have unixODBC library installed and configured data source in /etc/*/odbcinst.ini as of: https://github.com/dotnet/runtime/blob/master/src/libraries/System.Data.Odbc/src/DatabaseSetupInstructions.md

dsyme commented 4 years ago

Thanks @zpodlovics , I'll close as this just isn't an F# bug. Note some libraries can be sensitive to their host executable (e.g. application or F# interactive), the initialization files etc. There's no way to avoid that in genreal.

akdor1154 commented 3 years ago

FYI this seems to work now with dotnet/F# 5.0 and System.Data.Odbc 5.0 .