bflattened / bflat

C# as you know it but with Go-inspired tooling (small, selfcontained, and native executables)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Program compiled by `bflat build-il` can't be run on .NET runtime

opened this issue · comments

I tried .NET runtime, .NET Desktop runtime. Both doesn't work. The only thing I didn't try is the full .NET SDK. But I don't think I need to install a whole SDK to just run a program.

My .NET runtime version match bflat, which is 8.0.1.

The program crashed. What printed on the console is:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

If target .NET Framework 4.0 with --stdlib=none -r:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\mscorlib.dll, then everything is fine.

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

How did you execute the program? Did you just try to launch the EXE that bflat generated? If you launch the EXE directly, the OS will launch it under .NET Framework and then fail to find System.Runtime 8.0 because the framework is 4.0.

To launch a .NET 5+ executable, you either need to use the apphost shim (the EXE that the regular .NET SDK places next to the DLL that has actual .NET code), or launch it as dotnet exec c:\path\to\the\exe.exe. Note .NET 8 will look for a *.runtimeconfig.json and *.deps.json file next to the executable to tell it which version of the .NET runtime to activate. You need to make those or copy them from a hello world .NET app compiled using the full .NET SDK.

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

How did you execute the program? Did you just try to launch the EXE that bflat generated? If you launch the EXE directly, the OS will launch it under .NET Framework and then fail to find System.Runtime 8.0 because the framework is 4.0.

To launch a .NET 5+ executable, you either need to use the apphost shim (the EXE that the regular .NET SDK places next to the DLL that has actual .NET code), or launch it as dotnet exec c:\path\to\the\exe.exe. Note .NET 8 will look for a *.runtimeconfig.json and *.deps.json file next to the executable to tell it which version of the .NET runtime to activate. You need to make those or copy them from a hello world .NET app compiled using the full .NET SDK.

Why don't bflat build-il generate these files for me? Btw, should bflat build-il support .NET style apphost shim?

*.runtimeconfig.json is easy to write, at least for the most simple case, but *.deps.json is absolutely a nightmare to write manually. I still think bflat build-il should generate these files for me.

*.runtimeconfig.json is easy to write, at least for the most simple case, but *.deps.json is absolutely a nightmare to write manually. I still think bflat build-il should generate these files for me.

bflat build-il is pretty much the equivalent of csc.exe. If you open a Visual Studio command prompt and type csc /noconfig /nostdlib myfile.cs /r:path\to\bflat\ref\netstandard.dll you'll get an executable that works with .NET 8 (same as build-il with bflat), but not runtimeconfig or deps. That's because csc is just a compiler. A different tool generates these even in the .NET SDK.

Sample *.runtimeconfig.json if someone needed:

{
  "runtimeOptions": {
    "tfm": "net8.0",
    "framework": {
      "name": "Microsoft.NETCore.App",
      "version": "8.0.1"
    }
  }
}