fsharp icon indicating copy to clipboard operation
fsharp copied to clipboard

Error using System.Drawing.Common with FSI

Open tarekgh opened this issue 2 years ago • 11 comments


Issue moved from dotnet/runtime#57922

  • Please respond to @auslavs.

From @auslavs on Monday, August 23, 2021 8:01:05 AM

When calling System.Drawing.Common within FSharp Interactive, the assembly cannot be loaded

error FS0193: Could not load file or assembly 'System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Repro fsx script for the error:

#r "nuget: System.Drawing.Common, 5.0.2"

open System.IO
open System.Drawing

let getBitmapWidth =
  use stream = new FileStream("image.jpg",FileMode.Open,FileAccess.Read)
  use bm = new Bitmap(stream)
  bm.Width

FSI version details:

Microsoft (R) F# Interactive version 11.4.2.0 for F# 5.0

tarekgh avatar Aug 24 '21 01:08 tarekgh


Issue moved from dotnet/runtime#57922

  • Please respond to @msftbot[bot].

From @msftbot[bot] on Monday, August 23, 2021 8:01:09 AM

Tagging subscribers to this area: @safern, @tarekgh See info in area-owners.md if you want to be subscribed.

Issue Details

When calling System.Drawing.Common within FSharp Interactive, the assembly cannot be loaded

error FS0193: Could not load file or assembly 'System.Drawing.Common, Version=5.0.0.2, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

Repro fsx script for the error:

#r "nuget: System.Drawing.Common, 5.0.2"

open System.IO
open System.Drawing

let getBitmapWidth =
  use stream = new FileStream("image.jpg",FileMode.Open,FileAccess.Read)
  use bm = new Bitmap(stream)
  bm.Width

FSI version details:

Microsoft (R) F# Interactive version 11.4.2.0 for F# 5.0

Author: auslavs
Assignees: -
Labels:

area-System.Drawing, untriaged

Milestone: -

tarekgh avatar Aug 24 '21 01:08 tarekgh


Issue moved from dotnet/runtime#57922

  • Please respond to @tarekgh.

From @tarekgh on Monday, August 23, 2021 4:20:20 PM

@ericstj any thoughts what can cause that?

tarekgh avatar Aug 24 '21 01:08 tarekgh


Issue moved from dotnet/runtime#57922

  • Please respond to @ericstj.

From @ericstj on Monday, August 23, 2021 4:42:10 PM

I'm guessing it's an issue with FSI's loader for nuget packages. The package works fine when consumed from an application. sdc.zip

tarekgh avatar Aug 24 '21 01:08 tarekgh


Issue moved from dotnet/runtime#57922

  • Please respond to @am11.

From @am11 on Monday, August 23, 2021 4:46:31 PM

Yup, dotnet fsi with #r "nuget: System.Drawing.Common, 5.0.2" seems to be (incorrectly?) picking up netcoreapp3.0, instead of netstandard2.0:

file: /var/folders/lt/lqsrf7fs78d93xwjzhj62bg40000gn/T/nuget/24947--bb21e9ae-ff7d-4b0c-a258-233acaf14353/Project.fsproj.fsx

// Generated from #r "nuget:Package References"
// ============================================
//
// DOTNET_HOST_PATH:(C:\Program Files\dotnet\dotnet.exe)
// MSBuildSDKsPath:(C:\Program Files\dotnet\sdk\3.1.200-preview-014883\Sdks)
// MSBuildExtensionsPath:(C:\Program Files\dotnet\sdk\3.1.200-preview-014883\)
//
// References
//
#r @"/Users/am11/.nuget/packages/microsoft.win32.systemevents/5.0.0/lib/netstandard2.0/Microsoft.Win32.SystemEvents.dll"
#r @"/Users/am11/.nuget/packages/system.drawing.common/5.0.2/runtimes/unix/lib/netcoreapp3.0/System.Drawing.Common.dll"

(note the netcoreapp3.0 reference). If we explicitly reference the netstandard2.0 assembly instead, it works fine:

$ dotnet fsi

> #r "/Users/am11/.nuget/packages/system.drawing.common//5.0.2/lib/netstandard2.0/System.Drawing.Common.dll"
- open System.IO
- open System.Drawing
- 
- let getBitmapWidth =
-   use stream = new FileStream("image.jpg",FileMode.Open,FileAccess.Read)
-   use bm = new Bitmap(stream)
-   bm.Width;;

--> Referenced '/Users/am11/.nuget/packages/system.drawing.common//5.0.2/lib/netstandard2.0/System.Drawing.Common.dll' (file may be locked by F# Interactive process)

...

tarekgh avatar Aug 24 '21 01:08 tarekgh


Issue moved from dotnet/runtime#57922

  • Please respond to @ericstj.

From @ericstj on Tuesday, August 24, 2021 12:36:40 AM

netcoreapp3.0 is "better" than netstandard2.0 for a net5.0 project. See https://nugettools.azurewebsites.net/5.11.0/framework-precedence?framework=net5.0&exludedIdentifiers=

So choosing netcoreapp3.0 is correct, you can see that in the project I attached if you restore and examine the assets file you can see what an actual project will resolve. Choosing a runtime asset is dubious for reference, how I can see why this might be correct for interactive since here reference and runtime are the same thing.

That doesn't really explain why fsi couldn't load it though. Your workaround of using ns2.0 is probably just treating the symptom. Something in the runtime loader for fsi isn't consistent with how it's compiling.

This issue should be transferred to https://github.com/dotnet/fsharp. @KevinRansom @TIHan @cartermp can you help with that?

tarekgh avatar Aug 24 '21 01:08 tarekgh

Bump

Happypig375 avatar Jan 02 '22 07:01 Happypig375

I'm still getting this on .NET 6

C:\GitHub\dsyme\fsharp>artifacts\bin\fsi\Debug\net6.0\fsi.exe

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

For help type #help;;

> #r "nuget: System.Drawing.Common"
-
- open System.IO
- open System.Drawing
-
- let getBitmapWidth =
-   use stream = new FileStream("image.jpg",FileMode.Open,FileAccess.Read)
-   use bm = new Bitmap(stream)
-   bm.Width
- ;;
[Loading C:\Users\donsy\AppData\Local\Temp\43480--61af721a-f468-4be8-907f-412c233af999\Project.fsproj.fsx]
namespace FSI_0002.Project

Binding session to 'C:/Users/donsy/.nuget/packages/system.drawing.common/6.0.0/runtimes/win/lib/net6.0/System.Drawing.Common.dll'...


error FS0193: Could not load file or assembly 'System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'.

System.IO.FileNotFoundException: Could not load file or assembly 'System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
File name: 'System.Drawing.Common, Version=6.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
   at <StartupCode$FSI_0003>.$FSI_0003.main@()
Stopped due to error
>

dsyme avatar Mar 31 '22 02:03 dsyme

I will take a look shortly. Pretty sure it's not a setup issue.

KevinRansom avatar Mar 31 '22 17:03 KevinRansom

System.Drawing.Common is not a standard: Microsoft.NETCore.App assembly. It is in fact a : Microsoft.WindowsDesktop.App assembly

We don't probe there and fsi is not compiled: Microsoft.WindowsDesktop.App. So the runtime doesn't know to probe there either.

I'm not sure what to do about this. We haven't really considered the impact of Microsoft.WindowsDesktop.App or Microsoft.AspNetCore.App on fsi or fsc for that matter.

I imagine that we can probably do some stuff within #r "nuget: to allow the specification of these" framework extensions.

KevinRansom avatar Aug 23 '22 22:08 KevinRansom

Here is another example: https://github.com/dotnet/fsharp/issues/13473

KevinRansom avatar Aug 23 '22 22:08 KevinRansom

Okay, I think we may be able to do it using #r nuget: ... it's going to take some work though.

Nuget has packages with binaries, which look like they may work ...

If we do it right, I might be able to eliminate the preloading hack we do today although maybe not because of namespaces.

Prioritization remains to be seen.

KevinRansom avatar Aug 23 '22 22:08 KevinRansom