interactive icon indicating copy to clipboard operation
interactive copied to clipboard

Published app in single file mode and in trimmed mode with embedded kernel crashes on creating kernel

Open WhiteBlackGoose opened this issue 4 years ago • 3 comments

Describe the bug

I use Microsoft.DotNet.Interactive.CSharp in the app and dotnet publish it. For repro I made a dummy app which just runs the user's code.

Command:

dotnet publish -c release -r win-x64 -o testpub --self-contained
Program.cs
using Microsoft.DotNet.Interactive.CSharp;
using Microsoft.DotNet.Interactive;
using Microsoft.DotNet.Interactive.Commands;
using Microsoft.DotNet.Interactive.Events;
using Microsoft.DotNet.Interactive.Formatting;

Formatter.SetPreferredMimeTypeFor(typeof(object), "text/plain");

var kernel = new CSharpKernel();

var currInput = 0;
while (true)
{
    var currInputLocal = currInput;
    var code = ReadBlock("Your input", currInputLocal);
    var computed = await kernel.SendAsync(new SubmitCode(code));
    computed.KernelEvents.Subscribe(e =>
    {
        switch (e)
        {
            case (CommandSucceeded s):
                WriteBlock("Outcome", currInputLocal, "Success");
                break;
            case (DisplayEvent display):
                WriteBlock("Result", currInputLocal, display.FormattedValues.First().Value);
                break;
            case (CommandFailed failed):
                WriteBlock("Error", currInputLocal, failed.Message);
                break;
        }
    });
    currInput++;
}

static void WriteBlock(string prefix, int id, string text)
{
    Console.WriteLine();
    Console.Write($"{prefix} [{id}]: ");
    Console.Write(text);
    Console.WriteLine();
}

static string ReadBlock(string prefix, int id)
{
    Console.WriteLine();
    Console.Write($"{prefix} [{id}]: ");
    var text = Console.ReadLine();
    Console.WriteLine();
    return text;
}
hw.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <PropertyGroup>
    <PublishReadyToRun>true</PublishReadyToRun>


    <!-- Uncomment this to get bug #1
    <PublishSingleFile>true</PublishSingleFile>
    -->
    
    <!-- Uncomment this to get bug #2
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>link</TrimMode>
    -->
    
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.DotNet.Interactive.CSharp" Version="1.0.0-beta.21330.2" />
  </ItemGroup>

</Project>

Single file

When publishing in single file, I get

Unhandled exception. System.ArgumentNullException: Value cannot be null. (Parameter 'path')
   at System.IO.Directory.InternalEnumeratePaths(String , String , SearchTarget , EnumerationOptions )
   at Microsoft.DotNet.Interactive.CSharp.InteractiveWorkspace.ResolveRefAssemblies()
   at Microsoft.DotNet.Interactive.CSharp.InteractiveWorkspace..ctor()
   at Microsoft.DotNet.Interactive.CSharp.CSharpKernel..ctor()
   at Program.<Main>$(String[] args) in D:\tmp\test\hw\Program.cs:line 9
   at Program.<Main>(String[] args)

(see Bug 1 in the csproj file from above)

Trimmed mode

And that's what I get for trimmed mode (link)

error CS0656: Отсутствует обязательный для компилятора член "System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitOnCompleted"

(fuck exception localisation. It says "missing required member for the compiler")

(see bug 2 in the csproj file)

  • OS
    • [x] Windows 10

WhiteBlackGoose avatar Nov 13 '21 14:11 WhiteBlackGoose

This issue persists in the latest (1.0.0-beta.22175.2) version as well, although the error has moved slightly, it remains in ResolveRefAssemblies.

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.DotNet.Interactive.CSharp.InteractiveWorkspace.TryParseVersion(String versionString, Version& v)
   at Microsoft.DotNet.Interactive.CSharp.InteractiveWorkspace.ResolveRefAssemblyPath()
   at Microsoft.DotNet.Interactive.CSharp.InteractiveWorkspace.ResolveRefAssemblies()
   at Microsoft.DotNet.Interactive.CSharp.InteractiveWorkspace..ctor()
   at Microsoft.DotNet.Interactive.CSharp.CSharpKernel..ctor(String name)
   at Microsoft.DotNet.Interactive.CSharp.CSharpKernel..ctor()

ie. Currently its not possible to use this package when in 'self-contained' mode.

shadowmint avatar Apr 03 '22 10:04 shadowmint

We don't currently plan to make these libraries trimmable and many of their dependencies are not yet trimmable.

jonsequitur avatar Apr 12 '22 17:04 jonsequitur

If the API allowed the user to supply the MetaDataReference's, like the CSharpCompilation.CreateScriptCompilation API, it could probably work in single file mode. Although it is still not pretty. See: https://github.com/dotnet/roslyn/issues/50719 and https://github.com/andersstorhaug/SingleFileScripting

ironsm4sh avatar Jun 19 '22 09:06 ironsm4sh