fsc-host icon indicating copy to clipboard operation
fsc-host copied to clipboard

fsc-host in WebAssembly

Open harrisse opened this issue 3 years ago • 10 comments

Is it possible to run fsc-host in WebAssembly?

I'm trying to invoke fsc-host with CompilerHost.getAssembly Options.Default (Inline text) but am hitting the following exception:

System.TypeInitializationException: The type initializer for '<StartupCode$FSharp-Compiler-Service>.$FSharpCheckerResults' threw an exception.
System.NullReferenceException: Object reference not set to an instance of an object.

Googling the error took me to https://github.com/dotnet/fsharp/issues/11966 which I saw you had left a comment on, so I figured you might know something one way or the other 😄

This project got fsc working in the browser a while back but the approach no longer works in .net 7. I was able to make some progress on their approach by copying dlls from the browser cache to the filesystem, but was never able to get fsc to recognize symbols from -r:'d dlls.

Thanks!

harrisse avatar Feb 20 '23 04:02 harrisse

Hi @harrisse, I'll take a look if anything can be done here.

queil avatar Feb 20 '23 19:02 queil

Hi @harrisse I know it has been ages but do you have any repro for this? I am not super familiar with WebAssembly so a test project would help.

queil avatar May 30 '23 08:05 queil

Closed due to lack of interest.

queil avatar Apr 15 '24 10:04 queil

I'm very interested in this one. However if fsc-host uses fsi under the cover, it will not work, there will be many unsupported operation errors while speaking to console in/out, file etc. I created a ticket to address that but I think it would be easier to write some FsiLight version. And with .NET 9 is coming real multi-threading (tentative) :)

PawelStadnicki avatar Apr 23 '24 14:04 PawelStadnicki

So, fsc-host doesn't use fsi. It uses FSharp.Compiler.Service which under the hood uses fsc.exe (i.e. it requires the dotnet sdk). Web assembly has never been a priority for me personally and I am not even sure how people use F# in web assembly. However, if fsc-host could add any value to that scenario I am open to suggestions.

queil avatar Apr 23 '24 16:04 queil

My interest in using some sort of fsc-host/fsi-light in webassembly is to enable writing and executing a custom DSL code by the application users to extend the application features for their personal needs.

For that I'm using .net interactive that has a F# kernel, however that uses fsi which is not wasm friendly.

It all works when Blazor (wasm for .NET) runs in server mode but not in a client mode. And wasm is getting real multi-threading support with .NET 9 :)

Why I need it? At least for 2 reasons:

  • I don't want to send the fragile data out of the user browser and it is a static website without a server so I even can't ...
  • I don't want to manage/create a sandboxes for the user where they can run code in isolation. With a server it would be hard and expensive, becoming some sort of the Github Workspaces instance.

So I see two options:

  • make fsi wasm friendly: I raised a ticket to F# team but don't know yet if it possible to do
  • create a lightweight version of fsi

The later could be done starting from scratch or extending fsc-host.

Currently when I run example of fsc-host in wasm I'm getting

Unhandled exception rendering component: Could not find a part of the path '/plugins/default/plugin.fsx'.

That most likely can be eliminated as I have seen it support plain string body as well. Not sure if wasm will allow to use fsc.exe. Even thought, I would still need some sort of interactivity to query the bounded values and run several snippets in a session mode to make it possible to mix with other languages/kernels of .net interactive (C#, AI).

I will explore more.

PawelStadnicki avatar Apr 24 '24 07:04 PawelStadnicki

run example of fsc-host in wasm I'm getting

Would you be able to share a minimum example of how you run it in wasm? A repo/Dockerfile would be great.

queil avatar Apr 24 '24 07:04 queil

Also I think fsc-host was never meant to be used in interactive contexts. So you may actually be better with an fsi-light approach.

queil avatar Apr 24 '24 08:04 queil

My interest in using some sort of fsc-host/fsi-light in webassembly is to enable writing and executing a custom DSL code by the application users to extend the application features for their personal needs.

@PawelStadnicki I'm also interested in this exact use case. Have you made any progress on achieving this?

evelant avatar Aug 31 '24 11:08 evelant

hi @evelant I did a more detailed research and ... we have to wait for MSFT

Fsi is currently not wasm-friendly, it has a lot dependencies on a local files etc.

Maybe there are other ways than using Fsi but I don't know them and I want to use .NET Interactive (to have a polyglot programming capabilities with JS and Python). .NET Interactive uses Fsi in the F# Kernel.

I asked the F# team to make Fsi warm-friendly, don't know when it is going to happen.

Moreover, real multithreading in wasm is postponed until .NET 10 (there is a great progress on that but the amount of changes and backward compatibility was a huge effort to make it with .NET 9).

Last but not least, when it is ready, it will require a vast amount of F# services to be loaded into wasm as well, making it slow to load - but only on a first load. Not that tragic, but important (still I'm not sure about the overall performance having so many stuff in the web cache)

Summary, I'm waiting for:

  • fsi that is wasm friendly
  • .NET 10 hopefully it can be the same time for both and it does not have to be the GA (Nov 2025), even one of the early previews (Spring 2025) should be fine to start the PoC's

PawelStadnicki avatar Sep 02 '24 19:09 PawelStadnicki