Skip to content

The runtime takes --threads, --gpu, --gpu-build and the first -- from anywhere in a compiled program's argv #1466

Closed

Description

@viridale

What you did

Built a program that prints its arguments, then ran it with arguments a typical command-line tool receives:

bend args.bend -o args && ./args grep -e --threads notes.txt

What happened

The runtime takes --threads, --gpu, --gpu-build and the first -- from anywhere in the program's argv, including where they are the values of the program's own options:

$ ./args grep -e --threads notes.txt
bend: expected a thread count of 1 or more after --threads        # exit 1; main never runs
$ ./args compress --threads auto
bend: expected a thread count of 1 or more after --threads        # exit 1
$ ./args build --threads 4
[<./args>, <build>]
$ ./args fetch --gpu off
[<./args>, <fetch>]
$ ./args make --gpu-build                                          # exit 0; main never runs
$ ./args grep -- -v
[<./args>, <grep>, <-v>]                                           # the program never sees --
$ ./args grep -- -- -v
[<./args>, <grep>, <-->, <-v>]
$ bun args.js build --threads 4
[<…/args.js>, <build>]

So a compiled program cannot:

  • take an option named --threads or --gpu;
  • accept either word as an option's value (grep -e --threads) without the runtime failing before main runs;
  • see --, so POSIX end-of-options handling costs its users -- --.

--threads matters most. It is the conventional name of the option that parallel tools expose: ripgrep -j, --threads, xz -T, --threads, zstd -T, --threads. A Bend program can read its worker count (IO.thread_count, #971), but it cannot offer the flag its users expect.

#934 moved the runtime's --help to --bend-help. This asks to finish that change for the remaining options.

Request

  1. IO.args() in a compiled program, native or JS, is exactly its argv, after the program name.
  2. Runtime settings come from the environment, read once at startup: BEND_THREADS=8, BEND_GPU=off|on|4GB. Bad values fail as they do today. Prior art: GOMAXPROCS, RAYON_NUM_THREADS, OMP_NUM_THREADS. GHC made the same change in 7.0: compiled programs stopped accepting most +RTS options unless linked with -rtsopts.
  3. --gpu-build and --bend-help move to bend itself (bend main.bend -o main --gpu-build) or to the environment (BEND_GPU_BUILD=1 ./main).
  4. Optional: a build-time default, bend main.bend -o main --threads 8.

Migration: ./main --threads 8 becomes BEND_THREADS=8 ./main. This touches the GUIDE's Tooling block, CLI_HELP and the C main in comp.ts, cli() in comp.ts, tests/io/args.bend, and FLAGS in gates/perf.ts. I'm happy to send the patch if this direction is acceptable.

The file

import Base

def main() -> IO(Unit):
  do IO<Unit>:
    xs : List<String> <- IO.args()
    IO.print(List.show(~&1, ~String, ~(s => "<" ++ s ++ ">"), xs))

bend --version

bend 2.0.36

uname -sm

Darwin arm64

clang --version (the first line)

Apple clang version 21.0.0 (clang-2100.1.1.101)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions