Skip to content
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

Suggest adding core.setBooleanOutput function - specifying a boolean for core.setOutput does not work as expected #1368

Open
sounisi5011 opened this issue Mar 9, 2023 · 3 comments
Labels
enhancement New feature or request

Comments

@sounisi5011
Copy link

sounisi5011 commented Mar 9, 2023

Describe the enhancement

If a boolean is specified for core.setOutput, it will be converted to a JSON string and output.
For example, core.setOutput('result', false) sets the string "false" to output "result".

However, the string "false" evaluates to true within the conditional expression.
Because this is not an empty string, it is converted to 1, and then 1 is evaluated as true.
see https://docs.github.com/en/actions/learn-github-actions/expressions#operators

I do not think this behavior is correct.
The user would think that using core.setOutput('result', false) would set "result" to a value that evaluates to false.

Code Snippet

core.setOutput('result', false);

Currently, this code returns the string "false".
hus, the user must write the following to use the output "result" in a conditional expression:

steps:
  ...
  - run: echo "result is true"
    if: ${{ steps.custom-action-id.outputs.result == 'true' }}

It should return an empty string that evaluates to false or should not set output.
Then the user can use the output in a conditional expression in GitHub actions.

steps:
  ...
  - run: echo "result is true"
    if: ${{ steps.custom-action-id.outputs.result }}

My suggestion

This is not a bug. This behavior follows the documentation. Changing the behavior of the core.setOutput function would be a breaking change that would break backward compatibility.

However, few users would notice this problem with just the sentence "Non-string values will be converted to a string via JSON.stringify".

Therefore, I suggest adding a new core.setBooleanOutput('result', false) function.
This function returns the string value "true" if true is passed, or the empty string "" if false is passed.
It throws an error if a non-boolean value is passed. After adding this function, add a statement to the core.setOutput function documentation such as "If you want to specify a boolean, you may want to use the core.setBooleanOutput function instead".

Alternatively, I suggest adding a note to the core.setOutput function documentation about the trap regarding how to interpret expressions in the GitHub workflow.

see #1368 (comment)

@sounisi5011 sounisi5011 added the enhancement New feature or request label Mar 9, 2023
@twohlix
Copy link

twohlix commented May 11, 2023

This is working as expected based on the documentation - I'm gonna doubt your report will be addressed (i'm not a maintainer btw)

/**
 * Sets the value of an output.
 *
 * @param     name     name of the output to set
 * @param     value    value to store. Non-string values will be converted to a string via JSON.stringify
 */
JSON.stringify(true)
'true'

JSON.stringify(false)
'false'

@twohlix
Copy link

twohlix commented May 11, 2023

@sounisi5011 you just need to JSON.parse whatever is in the output is as all the inputs to setOutput are JSON stringified

@sounisi5011
Copy link
Author

sounisi5011 commented May 12, 2023

It is true that this is the expected behavior based on the documentation. Therefore, I have not labeled this issue as a bug.

What I am arguing is that the current specification, where the value passed to core.setOutput() is converted to a JSON string, needs to be improved.

This library is written in JavaScript and is intended to be used with JavaScript. Users would expect it to handle booleans in the same way as JavaScript. However, the GitHub workflow interprets values differently than JavaScript. If users do not read the documentation carefully, they may end up implementing custom JavaScript actions in the wrong way.


In my opinion, #370 is wrong in deciding to convert the value passed to the core.setOutput() function to JSON. However, this has already been released. To avoid breaking backwards compatibility, I suggest adding a core.setBooleanOutput() function specifically for boolean output.

Alternatively, I suggest adding a note to the core.setOutput() function documentation about the trap regarding how to interpret expressions in the GitHub workflow.

Few users would notice this problem with just the sentence "Non-string values will be converted to a string via JSON.stringify".

@sounisi5011 sounisi5011 changed the title Specifying a boolean for core.setOutput does not work as expected Suggest adding core.setBooleanOutput function - specifying a boolean for core.setOutput does not work as expected May 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants