actions / actions/toolkit

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

Open
#1,368 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
5.9k
Forks
1.8k
PR merge metrics
No merged PRs in 30d

Description

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 https://github.com/actions/toolkit/issues/1368#issuecomment-1544934060

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in packages/core/src/core.ts at the existing core.setOutput implementation and its documentation. Define the new boolean-output behavior described in the issue, document when to use it, and verify that false produces an output suitable for a GitHub Actions conditional while non-boolean inputs are rejected.

Written by the indexing model from the issue text.

Assessment

Tech stack
github-actions, typescript
Domain
ci-cd, developer-experience
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.