Skip to content

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

Open
@radical

Description

@radical

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

private void UpdateConsoleMainJs()
{
string mainJsPath = Path.Combine(_projectDir!, "main.mjs");
string mainJsContent = File.ReadAllText(mainJsPath);
mainJsContent = mainJsContent
.Replace(".create()", ".withConsoleForwarding().create()")
.Replace("[\"dotnet\", \"is\", \"great!\"]", "(await import(/* webpackIgnore: true */\"process\")).argv.slice(2)");
File.WriteAllText(mainJsPath, mainJsContent);
}

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions