From f28aa14f39245a0d9fcf7363c3908f9bd89d7e2e Mon Sep 17 00:00:00 2001 From: Oliver Ford Date: Wed, 25 Oct 2023 17:31:47 +0100 Subject: [PATCH] Fix output malformed when wrapper enabled Presently using a command such as `terraform output -json | jq` does not work with the wrapper enabled, as it is by default. In order to consume terraform's output having set it up with this Action, it is necessary either to disable the wrapper (`with: terraform_wrapper: false`) or run it in its own Actions step with an explicit `id` (e.g. `id: foo`) so that it can be referred to and consumed (`${{steps.foo.outputs.stdout}}` et al.) in later steps. This seems to be the result of much confusion (issues passim) and is not at all easy (#338) to debug/diagnose and come to the realisation that it's due to the wrapper, or even that such a thing exists. @austinvalle identified the issue as being due to the `@actions/exec` package writing the spawned command to stdout (along with then its actual stdout). This has previously been reported upstream in actions/toolkit#649; I've proposed actions/toolkit#1573 to fix it. This commit aims to address the issue for `setup-terraform` in the meantime by silencing `@actions/exec` and then writing out to stdout & stderr from the listener buffers, which it writes to without this additional logging. Closes #20, #80, #85, #149, #338, and probably more. --- dist/index1.js | 8 +++++++- wrapper/terraform.js | 9 ++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/dist/index1.js b/dist/index1.js index 73147cfd..828854f7 100755 --- a/dist/index1.js +++ b/dist/index1.js @@ -4176,9 +4176,15 @@ async function checkTerraform () { const args = process.argv.slice(2); const options = { listeners, - ignoreReturnCode: true + ignoreReturnCode: true, + silent: true, // workaround github.com/actions/toolkit#649 }; const exitCode = await exec(pathToCLI, args, options); + + // Pass-through stdout/err as `exec` won't due to `silent: true` option + process.stdout.write(stdout.contents); + process.stderr.write(stderr.contents); + core.debug(`Terraform exited with code ${exitCode}.`); core.debug(`stdout: ${stdout.contents}`); core.debug(`stderr: ${stderr.contents}`); diff --git a/wrapper/terraform.js b/wrapper/terraform.js index eddcb35f..9a031521 100755 --- a/wrapper/terraform.js +++ b/wrapper/terraform.js @@ -33,9 +33,15 @@ async function checkTerraform () { const args = process.argv.slice(2); const options = { listeners, - ignoreReturnCode: true + ignoreReturnCode: true, + silent: true, // workaround github.com/actions/toolkit#649 }; const exitCode = await exec(pathToCLI, args, options); + + // Pass-through stdout/err as `exec` won't due to `silent: true` option + process.stdout.write(stdout.contents); + process.stderr.write(stderr.contents); + core.debug(`Terraform exited with code ${exitCode}.`); core.debug(`stdout: ${stdout.contents}`); core.debug(`stderr: ${stderr.contents}`); @@ -46,6 +52,7 @@ async function checkTerraform () { core.setOutput('stderr', stderr.contents); core.setOutput('exitcode', exitCode.toString(10)); + if (exitCode === 0 || exitCode === 2) { // A exitCode of 0 is considered a success // An exitCode of 2 may be returned when the '-detailed-exitcode' option