IronLanguages / ironpython2

Implementation of the Python programming language for .NET Framework; built on top of the Dynamic Language Runtime (DLR).

Home Page:http://ironpython.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot add reference to library built with .NET 5.0.400

vsajip opened this issue · comments

On 64-bit Ubuntu (Linux Mint), I downloaded and installed IronPython 2.7.11 from the .deb linked on this page:

https://github.com/IronLanguages/ironpython2/releases/tag/ipy-2.7.11

Using it, I can't seem to add a reference to a test .dll created using .NET 5.0.400:

$ more lib/*
::::::::::::::
lib/lib.csproj
::::::::::::::
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>
</Project>
::::::::::::::
lib/Main.cs
::::::::::::::
using System;

namespace App
{
    public class Main
    {
        void Hello() {
            System.Console.WriteLine("Hello, world!");
        }
    }
}

It builds OK:

$ dotnet build -v q
Microsoft (R) Build Engine version 16.11.0+0538acc04 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.


Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.24

But adding a reference fails:

$ cd bin/Debug/net5.0/; ls -l
total 32
drwxrwxr-x 3 vinay vinay 4096 Aug 19 07:05 ./
drwxrwxr-x 3 vinay vinay 4096 Aug 19 07:05 ../
-rw-rw-r-- 1 vinay vinay  379 Aug 19 07:05 lib.deps.json
-rw-rw-r-- 1 vinay vinay 4096 Aug 19 07:05 lib.dll
-rw-rw-r-- 1 vinay vinay 9596 Aug 19 07:05 lib.pdb
drwxrwxr-x 2 vinay vinay 4096 Aug 19 07:05 ref/
$ ipy
IronPython 2.7.11 (2.7.11.1000)
[.NETFramework,Version=v4.5 on Mono 6.8.0.105 (Debian 6.8.0.105+dfsg-2 Wed Feb 26 23:23:50 UTC 2020) (64-bit)]
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys, clr
>>> sys.path.append(os.getcwd())
>>> import System.Runtime
>>> clr.AddReference('lib.dll')
Can't find custom attr constructor image: /disk2/projects/scratch/dotnet/lib/bin/Debug/net5.0/lib.dll mtoken: 0x0a00000a due to: Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
Traceback (most recent call last):
  Fi   "<stdin>", line 1, in <module>
IOError: [Errno 2] Could not load file or assembly 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.
>>>

Any workarounds?

I'm not sure if there's a Mono version that supports loading .NET 5 assemblies? You could try the latest Mono release (6.12) to see if it works. Otherwise you'll have to retarget your assembly to .NET Standard 2.1.

I tried, and neither approach worked, though the error message is slightly different:

$ more lib.csproj 
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
    <TargetFramework>netstandard2.1</TargetFramework>
  </PropertyGroup>
</Project>
$ dotnet build
Microsoft (R) Build Engine version 16.11.0+0538acc04 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  Restored /disk2/projects/scratch/dotnet/lib/lib.csproj (in 89 ms).
  lib -> /disk2/projects/scratch/dotnet/lib/bin/Debug/netstandard2.1/lib.dll

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:01.55
$ ipy
IronPython 2.7.11 (2.7.11.1000)
[.NETFramework,Version=v4.5 on Mono 6.12.0.122 (tarball Mon Feb 22 17:33:28 UTC 2021) (64-bit)]
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys, clr
>>> sys.path.append('bin/Debug/netstandard2.1')
>>> clr.AddReference('lib.dll')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: System.IO.IOException: Could not add reference to assembly lib.dll
  at IronPython.Runtime.ClrModule.AddReference (IronPython.Runtime.CodeContext context, System.String name) [0x00041] in <c6cd91ccc2d540129176cdbfb972971c>:0 
  at IronPython.Runtime.ClrModule.AddReference (IronPython.Runtime.CodeContext context, System.Object reference) [0x00022] in <c6cd91ccc2d540129176cdbfb972971c>:0 
  at IronPython.Runtime.ClrModule.AddReference (IronPython.Runtime.CodeContext context, System.Object[] references) [0x00032] in <c6cd91ccc2d540129176cdbfb972971c>:0 
  at Microsoft.Scripting.Interpreter.ActionCallInstruction`2[T0,T1].Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x00000] in <86e836e8b8fb48bdacd38b0361e05e66>:0 
  at Microsoft.Scripting.Interpreter.Interpreter.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x0004f] in <86e836e8b8fb48bdacd38b0361e05e66>:0 
  at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet] (T0 arg0, T1 arg1, T2 arg2, T3 arg3) [0x0006d] in <86e836e8b8fb48bdacd38b0361e05e66>:0 
  at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet] (System.Runtime.CompilerServices.CallSite site, T0 arg0, T1 arg1, T2 arg2) [0x00128] in <5b415632df1f4365ae2242b1a257bb5b>:0 
  at (wrapper dynamic-method) System.Object.CallSite.Target(System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,IronPython.Runtime.CodeContext,object,object)
  at IronPython.Compiler.Ast.CallExpression+Invoke1Instruction.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x00026] in <c6cd91ccc2d540129176cdbfb972971c>:0 
  at Microsoft.Scripting.Interpreter.Interpreter.Run (Microsoft.Scripting.Interpreter.InterpretedFrame frame) [0x0004f] in <86e836e8b8fb48bdacd38b0361e05e66>:0 
  at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet] (T0 arg0, T1 arg1) [0x0004d] in <86e836e8b8fb48bdacd38b0361e05e66>:0 
  at IronPython.Compiler.PythonScriptCode.RunWorker (IronPython.Runtime.CodeContext ctx) [0x0001b] in <c6cd91ccc2d540129176cdbfb972971c>:0 
  at IronPython.Compiler.PythonScriptCode.Run (Microsoft.Scripting.Runtime.Scope scope) [0x00023] in <c6cd91ccc2d540129176cdbfb972971c>:0 
  at IronPython.Hosting.PythonCommandLine+<>c__DisplayClass27_0.<RunOneInteraction>b__0 () [0x00027] in <c6cd91ccc2d540129176cdbfb972971c>:0 
>>> 
$ ls -l bin/Debug/netstandard2.1/
total 16
-rw-rw-r-- 1 vinay vinay  420 Aug 19 20:48 lib.deps.json
-rw-rw-r-- 1 vinay vinay 4096 Aug 19 20:48 lib.dll
-rw-rw-r-- 1 vinay vinay 7948 Aug 19 20:48 lib.pdb

OK - I think I found the problem. It needs the absolute path to the assembly in sys.path and I was using a relative path.