moon icon indicating copy to clipboard operation
moon copied to clipboard

[feature] Support nested execution in `platform: system` commands

Open shellscape opened this issue 2 years ago • 4 comments
trafficstars

Is your feature request related to a problem? Please describe.

While trying to get around https://github.com/moonrepo/moon/issues/888 without creating a separate bash script, I attempted to use the MOON_PROJECT_ROOT environment variable in a command with platform: system.

  test:
    command: node --title $projectAlias:test ../../node_modules/ava/entrypoints/cli.mjs $(echo $MOON_PROJECT_ROOT)
    inputs:
      - src
      - test
    platform: system

The result was:

[DEBUG 11:35:05] moon_process::command_inspector  Running command /bin/zsh -c 'node --title svc-test:test ../../node_modules/ava/entrypoints/cli.mjs '\''$(echo'\'' '\''$MOON_PROJECT_ROOT)'\''' 

I also tried backticks with a similar result:

  test:
    command: node --title $projectAlias:test ../../node_modules/ava/entrypoints/cli.mjs `echo $MOON_PROJECT_ROOT`
    inputs:
      - src
      - test
    platform: system

It looks like the argument parsing needs a bit more massaging to make it bash-compatible yet, but that's to be expected.

Describe the solution you'd like

I'd like to use backticks or the $() blocks for nested execution. This likely requires closer integration with the shell and probably not something that moon is setup for at the moment, but I'd still like to get it down as a want and nice-to-have.

Describe alternatives you've considered

Using a separate bash script works, but that's a detriment to DX.

Additional context

shellscape avatar Jun 05 '23 15:06 shellscape

@shellscape

Arg parsing comes from a third-party library, so I don't have a lot of control of it right now.

You can probably try the array form which bypasses arg parsing:

  test:
    command: 
      - node 
      - --title 
      - $projectAlias:test 
      - ../../node_modules/ava/entrypoints/cli.mjs 
      - $(echo $MOON_PROJECT_ROOT)
    inputs:
      - src
      - test
    platform: system

milesj avatar Jun 05 '23 16:06 milesj

Good call. Gave that a shot and looks like the parser is making it a string, which doesn't allow the nested execution. '\''$(echo $MOON_PROJECT_ROOT)'\''' - it's no worries, I get you're limited at the moment. Good for tracking though.

shellscape avatar Jun 05 '23 17:06 shellscape

Should this be considered a bug? I saw this example in the documentation:

tasks:
  native:
    command: 'echo $SHELL'
    options:
      shell: true

which I think implies that it will print the name of the shell used and not just the string $SHELL.

Can work around with:

tasks:
  native:
    command: bash -c "echo $SHELL"
    options:
      shell: true

Edit: apparently I'm talking about a different issue (shell variables vs nested execution), but both seem like a bug (bad UX) since shell features randomly don't work. I think users expect to be able to copy-paste their shell script file contents under command: |, and have it work with no hiccups.

ilyasotkov avatar Jun 19 '23 17:06 ilyasotkov

This PR (https://github.com/moonrepo/moon/pull/928) will fix the quoting, so $(echo $MOON_PROJECT_ROOT) should work once it lands.

milesj avatar Jun 20 '23 20:06 milesj

This should now be possible with task scripts https://moonrepo.dev/blog/moon-v1.27#new-task-scripts

milesj avatar Jul 15 '24 18:07 milesj