basic-cli icon indicating copy to clipboard operation
basic-cli copied to clipboard

received command line args are empty when using --linker=legacy

Open Anton-4 opened this issue 2 years ago • 5 comments

$ ./target/release/roc run examples/args.roc --linker=legacy -- log -b 3 --num 81
args-example
A calculator example of the CLI platform argument parser

COMMANDS:
    div
        OPTIONS:
            --dividend, -n    the number to divide; corresponds to a numerator  (integer, 64-bit signed)
            --divisor, -d    the number to divide by; corresponds to a denominator  (integer, 64-bit signed)

    log
        OPTIONS:
            --base, -b    base of the logarithm  (integer, 64-bit signed)
            --num    the number to take the logarithm of  (integer, 64-bit signed)

The program name "args-example" was not provided as a first argument!

The weird thing is, this only happens when using a basic-cli release, not when building the platform from source. We use the rust args_os function under the hood, in the docs it is mentioned that "The first element is traditionally the path of the executable, but it can be set to arbitrary text, and might not even exist. This means this property should not be relied upon for security purposes."

Anton-4 avatar Jul 05 '23 15:07 Anton-4

This does not happen without --linker=legacy. So far we've seen this on NixOS and Ubuntu.

Anton-4 avatar Jun 15 '24 13:06 Anton-4

On linux args_os uses the init_array section of the executable (assembly), this section does not exist in the executable created by the legacy linker.

Anton-4 avatar Jun 15 '24 17:06 Anton-4

I believe this happens because we target musl for the release build, you can workaround this by building locally:

git clone https://github.com/roc-lang/basic-cli.git
cd basic-cli
# adjust 0.10.0 to your desired version
git checkout tags/0.10.0

Change the platform in your roc file from:

app [main] {
    pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
}

To (adjust path for your basic-cli folder):

app [main] {
    pf: platform "/home/username/gitfolders/basic-cli/platform/main.roc",
}

Build your app:

roc main.roc --linker=legacy

Now the platform is built, to prevent unnecessary rebuilding you can do:

roc main.roc --linker=legacy --prebuilt-platform

To pass args (e.g. fileA.txt and fileB.txt) the syntax is:

roc main.roc --linker=legacy --prebuilt-platform -- fileA.txt fileB.txt

Anton-4 avatar Jun 15 '24 18:06 Anton-4

The musl target was indeed the issue, this was fixed here and is available in rust 1.79, time to do some upgrading...

Anton-4 avatar Jun 18 '24 16:06 Anton-4

This still happens with the basic-cli 0.12.0 pre-release which was built using rust 1.79

Anton-4 avatar Jul 02 '24 12:07 Anton-4

This could possibly be what is going on: https://roc.zulipchat.com/#narrow/channel/302903-platform-development/topic/getting.20args.20when.20using.20.60roc.20run.60.3F/near/477689098

Anton-4 avatar Oct 18 '24 16:10 Anton-4

Note: once we figure out a fix for this there's a test in the roc compiler repo that we can un-ignore. See: https://github.com/roc-lang/roc/pull/7172

jwoudenberg avatar Oct 21 '24 20:10 jwoudenberg

This is fixed in basic-cli 0.18.0 :tada:

Anton-4 avatar Dec 30 '24 15:12 Anton-4