Skip to content

@actions/exec: add support for using a shell to run commands #285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

alexkappa
Copy link

This PR adds support for supplying a shell to use when running commands. This is useful with expanding wildcards among other things.

Fixes: #229

@alexkappa alexkappa changed the title Add support for using a shell to run commands @actions/exec: add support for using a shell to run commands Jan 6, 2020
@@ -12,6 +12,9 @@ export interface ExecOptions {
/** optional. defaults to false */
silent?: boolean

/** optional. Runs command inside of a shell. A different shell can be specified as a string. Defaults to false */
shell?: boolean | string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this affect the quoting rules on windows? linux?

Lots of logic in tool runner to accurately deal with arg quoting for .exe and different rules for .cmd/.bat.

Today not a problem on Linux because iirc node calls execv which takes an arg array. Different on Windows because everything gets joined into a single string. Does this introduce arg quoting challenges on Linux?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if so, are the quoting rules different depending on the shell?

Copy link
Author

@alexkappa alexkappa Jan 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ericsciple, from the documentation of child_process options:

  • shell <boolean> | <string> If true, runs command inside of a shell. Uses '/bin/sh' on Unix, and process.env.ComSpec on Windows. A different shell can be specified as a string. See Shell Requirements and Default Windows Shell. Default: false (no shell).
  • windowsVerbatimArguments <boolean> No quoting or escaping of arguments is done on Windows. Ignored on Unix. This is set to true automatically when shell is specified and is CMD. Default: false.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps on windows we could set windowsVerbatimArguments to true if shell is specified and is equal to 'CMD'. That way we match the behavior of the stdlib, and allow our quoting to work with that assumption.

On linux I don't know if there are any quoting differences, but I'd be happy to add more tests to find out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Other thoughts, spitballing...

If drastically different, I wonder whether it would make more sense to be linux/mac only feature.

If quoting issues on Linux, then existing workaround of tool: 'sh', args: ['-c', 'ls -l *.md'] is at least transparent what is happening

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not entirely sure how to proceed with this @ericsciple. I understand the workaround, but I thought since node supports it natively, it could be low hanging fruit with a more elegant solution.

Will more tests alleviate your concerns? I understand you are reluctant to accept this as there might be edge cases that break quoting. If so, I might need some help :)

if (IS_WINDOWS) {
tool = 'cmd'
args = ['/c', 'echo', 'hello']
opts = {shell: process.env.ComSpec}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest also adding a test for running bash shell within Windows. As bash is now supported by windows runners, it provides a universal target for actions wanting to execute shell commands.

@smorimoto
Copy link
Contributor

smorimoto commented Feb 28, 2020

What is the status of this PR? This change is required for actions/cache.

@timharris777
Copy link

Why hasn't this been supported yet? I see that @actions/exec is actually using child_process.spawn under the hood. I agree with @alexkappa that adding the shell option as an available option to pipe to the spawn command should be quite easy. I will be forking @alexkappa's fork as I have use of this.

@actions actions deleted a comment Apr 28, 2021
@thboop thboop added the exec label May 3, 2021
@thboop thboop changed the base branch from master to main May 27, 2021 15:13
@thboop thboop requested a review from a team as a code owner May 27, 2021 15:13
@actions actions deleted a comment Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

exec: specify shell to run the command in
6 participants