crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.21k stars 1.61k forks source link

Representation of `Process` arguments #14773

Open straight-shoota opened 3 days ago

straight-shoota commented 3 days ago

Process.new (and .run) has two parameters, command for the command to be executed, and args for the arguments.

I'm not quite sure what's the reasoning behind this separation. It's been established since the early days of Crystal (https://github.com/crystal-lang/crystal/commit/0b35fa5d688110d898275c9ab145cce93a4faffc) and is probably inherited from Ruby's Kernel#exec.

In many (most?) other APIs, the equivalent method receives a single list of arguments, where the first one represents the command to execute. This is similar to the way process execution is modeled in Unix.

Process even uses this representation internally: The command and args parameters are merged into a single array in Process.prepare_args. Even on Windows. This internal representation as Array(String) is additional allocation which is actually just an intermediary: On Unix, the array is mapped to an array of pointers to those strings. On Windows it's mapped to an array of wide strings (Array(Slice(UInt16))). This intermediate representation should be avoidable.

However, we might also want to consider whether the public API could use the single array model where the command is just the first element. I'm not sure if this has any clear benefit expect being more similar to other APIs.

This was breviously mentioned in #9030 before. And I suppose this might have some relevancy the shell: true conundrum.

ysbaddaden commented 3 days ago

Well, on UNIX exec(2) distinguishes the executable path from the arg list.

Weirdly, CreateProcessW on Windows does the same, but in order to search path it must be NULL, and the command be merged with the rest of the args :confused: