toolkit icon indicating copy to clipboard operation
toolkit copied to clipboard

getInput empty returned value

Open y-nk opened this issue 1 year ago • 1 comments
trafficstars

Describe the bug

When using core.getInput in a composite action, the returned value is empty.

To Reproduce Steps to reproduce the behavior:

  1. Create a composite action with an input
  2. Use github-script action and try core.getInput on the input
  3. Use the composite action in a workflow
  4. Set the input with a value
  5. See error

Expected behavior

The value should be returned as provided

Additional context

The root cause is known and is on actions/runner side (here) but i think we should so something about it in here too. A warning about missing env var could be a quick win, and avoid people not knowing why it failed.

Proposed implementation

https://github.com/actions/toolkit/blob/1b5a6e26f42769414fe2302e6a6e8b5ac7bf9600/packages/core/src/core.ts#L117-L138

export function getInput(name: string, options?: InputOptions): string {
  const inputKey = `INPUT_${name.replace(/ /g, '_').toUpperCase()`
    
  const val: string =
    process.env[inputKey] || ''

  if (!(inputKey in process.env)) {
    warn(`the input '${inputKey}' is missing from env`)
  }

  if (options && options.required && !val) {
    throw new Error(`Input required and not supplied: ${name}`)
  }

  if (options && options.trimWhitespace === false) {
    return val
  }

  return val.trim()
}

y-nk avatar Jan 11 '24 03:01 y-nk

If you're trying to get an input while using actions/github-script, as I am, you can work around this by just using normal expression substitutions (orwhatever they're called):

// const workingDirectory = core.getInput("working-directory");
const workingDirectory = "${{ inputs.working-directory }}";

vincerubinetti avatar Sep 19 '24 20:09 vincerubinetti