镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Open

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

Activity

  1. added this to the 7.0.0 milestone on Sep 26, 2022
  2. pavelsavara commented on Sep 26, 2022

    @pavelsavara
    Member

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

  3. maraf commented on Sep 27, 2022

    @maraf
    Member

    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".

  4. pavelsavara commented on Sep 27, 2022

    @pavelsavara
    Member

    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 :)

  5. pavelsavara commented on Sep 30, 2022

    @pavelsavara
    Member

    I believe this was fixed by #76182 and #76373
    Is that right @radical @maraf ?

  6. maraf commented on Sep 30, 2022

    @maraf
    Member

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

  7. removed this from the 7.0.0 milestone on Sep 30, 2022
  8. 9 remaining items

  9. modified the milestones: 8.0.0, 9.0.0 on Aug 14, 2023
  10. lewing commented on Feb 21, 2024

    @lewing
    Member
  11. lewing commented on Feb 21, 2024

    @lewing
    Member

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

  12. maraf commented on Feb 21, 2024

    @maraf
    Member

    I think this is all WasmAppHost related

  13. self-assigned this
    on Feb 21, 2024
  14. lewing commented on Feb 21, 2024

    @lewing
    Member

    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

  15. modified the milestones: 9.0.0, 10.0.0 on Jul 24, 2024
  16. modified the milestones: 10.0.0, Future on Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions