dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

[wasm] Templates don't pass the command line arguments to the app #76201

Open radical opened 1 year ago

radical commented 1 year ago

If we print the command line args:

using System;
using System.Runtime.InteropServices.JavaScript;

Console.WriteLine("Hello, Console!");
Console.WriteLine ($"command line args: {string.Join(',', args)}");

return 0;

public partial class MyClass
{
    [JSExport]
    internal static string Greeting()
    {
        var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}";
        return text;
    }

    [JSImport("node.process.version", "main.mjs")]
    internal static partial string GetNodeVersion();
}

.. and run:

$ dotnet run x y z
WasmAppHost --runtime-config /tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle/cc.runtimeconfig.json x y z
Running: node main.mjs x y z
Using working directory: /tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle
mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28
Hello, World! Greetings from node version: v14.18.2
Hello, Console!
command line args: dotnet,is,great!

The command line arguments x y z were ignored, and instead we got dotnet,is,great. And that's because the main.mjs has:

await runMainAndExit(config.mainAssemblyName, ["dotnet", "is", "great!"]);

This doesn't get caught by Wasm.Build.Tests because the tests are explicitly patching the generated code https://github.com/dotnet/runtime/blob/4bd3ee5c6648c8435df13cd9d2185b55fd022767/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs#L48-L58

And the same is done for the browser too.

We should instead:

cc @pavelsavara

ghost commented 1 year ago

Tagging subscribers to 'arch-wasm': @lewing See info in area-owners.md if you want to be subscribed.

Issue Details
If we print the command line args: ```csharp using System; using System.Runtime.InteropServices.JavaScript; Console.WriteLine("Hello, Console!"); Console.WriteLine ($"command line args: {string.Join(',', args)}"); return 0; public partial class MyClass { [JSExport] internal static string Greeting() { var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}"; return text; } [JSImport("node.process.version", "main.mjs")] internal static partial string GetNodeVersion(); } ``` .. and run: ``` $ dotnet run x y z WasmAppHost --runtime-config /tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle/cc.runtimeconfig.json x y z Running: node main.mjs x y z Using working directory: /tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28 Hello, World! Greetings from node version: v14.18.2 Hello, Console! command line args: dotnet,is,great! ``` The command line arguments `x y z` were ignored, and instead we got `dotnet,is,great`. And that's because the `main.mjs` has: ```js await runMainAndExit(config.mainAssemblyName, ["dotnet", "is", "great!"]); ``` This doesn't get caught by Wasm.Build.Tests because the tests are explicitly patching the generated code https://github.com/dotnet/runtime/blob/4bd3ee5c6648c8435df13cd9d2185b55fd022767/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs#L48-L58 And the same is done for the browser too. We should instead: - [ ] Pass the command line args for console, and browser to the app - [ ] Automatically set console forwarding if runArgs has forward console set - [ ] The tests should be updated to run the template exactly as it is generated. And a follow up test can patch it to print the arguments, and check that cc @pavelsavara
Author: radical
Assignees: -
Labels: `arch-wasm`, `area-Build-mono`
Milestone: 7.0.0
pavelsavara commented 1 year ago

we could have simpler await dotnet.run() instead of await runMainAndExit

maraf commented 1 year ago

we could have simpler await dotnet.run() instead of await runMainAndExit

Why do we have dotnet.run what implicitly reads main assembly and arguments, but runMainAndExit requires to pass them explicitly?

Using dotnet.run() after const runtime = dotnet.create() seems like "a break in code flow".

pavelsavara commented 1 year ago

Why do we have dotnet.run what implicitly reads main assembly and arguments, but runMainAndExit requires to pass them explicitly?

Because it's a lower level API which is not part of the builder pattern.

Using dotnet.run() after const runtime = dotnet.create() seems like "a break in code flow".

That's OK :)

pavelsavara commented 1 year ago

I believe this was fixed by https://github.com/dotnet/runtime/pull/76182 and https://github.com/dotnet/runtime/pull/76373 Is that right @radical @maraf ?

maraf commented 1 year ago

Partially. It passes command line args for the console template.

pavelsavara commented 1 year ago

I updated it for Net8 then

lewing commented 1 year ago

@radical is this resolved?

pavelsavara commented 1 year ago

I would like to see it resolved together with https://github.com/dotnet/runtime/issues/88760

lewing commented 6 months ago

cc @richlander

ghost commented 6 months ago

Tagging subscribers to this area: @vitek-karas, @agocke See info in area-owners.md if you want to be subscribed.

Issue Details
If we print the command line args: ```csharp using System; using System.Runtime.InteropServices.JavaScript; Console.WriteLine("Hello, Console!"); Console.WriteLine ($"command line args: {string.Join(',', args)}"); return 0; public partial class MyClass { [JSExport] internal static string Greeting() { var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}"; return text; } [JSImport("node.process.version", "main.mjs")] internal static partial string GetNodeVersion(); } ``` .. and run: ``` $ dotnet run x y z WasmAppHost --runtime-config /tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle/cc.runtimeconfig.json x y z Running: node main.mjs x y z Using working directory: /tmp/cc/bin/Debug/net7.0/browser-wasm/AppBundle mono_wasm_runtime_ready fe00e07a-5519-4dfe-b35a-f867dbaf2e28 Hello, World! Greetings from node version: v14.18.2 Hello, Console! command line args: dotnet,is,great! ``` The command line arguments `x y z` were ignored, and instead we got `dotnet,is,great`. And that's because the `main.mjs` has: ```js await runMainAndExit(config.mainAssemblyName, ["dotnet", "is", "great!"]); ``` This doesn't get caught by Wasm.Build.Tests because the tests are explicitly patching the generated code https://github.com/dotnet/runtime/blob/4bd3ee5c6648c8435df13cd9d2185b55fd022767/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs#L48-L58 And the same is done for the browser too. We should instead: - [x] Pass the command line args for console, and browser to the app - [ ] Automatically set console forwarding if runArgs has forward console set - [ ] The tests should be updated to run the template exactly as it is generated. And a follow up test can patch it to print the arguments, and check that cc @pavelsavara
Author: radical
Assignees: -
Labels: `arch-wasm`, `area-HostModel`, `area-Build-mono`
Milestone: 9.0.0
lewing commented 6 months ago

What area owns template argument parsing and where should this be redirected?

maraf commented 6 months ago

I think this is all WasmAppHost related

lewing commented 6 months ago

I think this is all WasmAppHost related

I think the app host doesn't get all the information it needs, but I hope I'm wrong