dotnet / interactive

.NET Interactive combines the power of .NET with many other languages to create notebooks, REPLs, and embedded coding experiences. Share code, explore data, write, and learn across your apps in ways you couldn't before.
MIT License
2.88k stars 382 forks source link

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

Open WhiteBlackGoose opened 2 years ago

WhiteBlackGoose commented 2 years ago

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 ```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 ```xml Exe net6.0 enable enable true ```

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)

shadowmint commented 2 years ago

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.

jonsequitur commented 2 years ago

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

ironsm4sh commented 2 years ago

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