hashicorp / setup-terraform

Sets up Terraform CLI in your GitHub Actions workflow.
https://developer.hashicorp.com/terraform/tutorials/automation/github-actions
Mozilla Public License 2.0
1.35k stars 237 forks source link

Hard to reach terraform_wrapper: false from CDKTF error #338

Closed LumaKernel closed 10 months ago

LumaKernel commented 1 year ago

Situation

> cdktf get

SyntaxError: Unexpected token c in JSON at position 1
    at JSON.parse (<anonymous>)
    at /home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:94:49074
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async KM (/home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:45:1221)
    at async c$ (/home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:94:48908)
    at async _$.generateTypescriptProvider (/home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:96:6837)
    at async _$.generateTypescript (/home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:96:8841)
    at async Promise.all (index 3)
    at async _$.generate (/home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:99:372)
    at async qDe (/home/runner/work/<REDACTED>/node_modules/cdktf-cli/bundle/bin/cmds/handlers.js:176:8163)
⠹ downloading and generating modules and providers...
SyntaxError: Unexpected token c in JSON at position 1
npm ERR! Lifecycle script `get` failed with error: 
npm ERR! Error: command failed 
npm ERR!   in workspace: <REDACTED> 
npm ERR!   at location: /home/runner/work/<REDACTED>

Here is no hint to know when it gets c character while it's parsing JSON.

I checked handlers.js:94:49074 and I figured out that the original code is this.

https://github.com/hashicorp/terraform-cdk/blob/49a7072948a115ef20ea5ebe62f8d04117de81c9/packages/%40cdktf/provider-generator/lib/get/generator/provider-schema.ts#L309-L313

    providerSchema = JSON.parse(
      await exec(terraformBinaryName, ["providers", "schema", "-json"], {
        cwd: outdir,
      })
    ) as ProviderSchema;

Then I tried terraform providers schema -json in GitHub Actions and get it's wrapping the CLI.

$ terraform providers schema -json
[command]/home/runner/work/_temp/e3701105-a0d4-426d-80e2-575b823dd917/terraform-bin providers schema -json
{"format_version":"1.0"}
::debug::Terraform exited with code 0.
::debug::stdout: {"format_version":"1.0"}%0A
::debug::stderr:
::debug::exitcode: 0
::set-output name=stdout::{"format_version":"1.0"}%0A
::set-output name=stderr::
::set-output name=exitcode::0

Now we get the character c is coming from [command]

Solution candidates

I have no right solution for this, but have ideas.

I like third solution. How about that?

bflad commented 1 year ago

Hi @LumaKernel 👋 Thank you for reporting this and sorry you are running into trouble here. Could you please show us the full workflow configuration that led to this particular error?

There have been some internal discussions previously about removing the Terraform wrapper as the default (leaving this action only responsible for installation and, potentially, another action for running commands in the GitHub Actions context with setting outputs, etc).

OJFord commented 1 year ago

Making it default-false at least sounds like an excellent idea - it's hard to discover that it's the cause even simply trying to use terraform output -json | jq.

iselo commented 12 months ago

Hi there. Is any updates on this? I've got the same error at GitHub Actions on calling toBeValidTerraform() with TypeScript.

const tempOutDir = Testing.fullSynth(terraformStack);
console.log(toBeValidTerraform(tempOutDir).message);
console.log(toPlanSuccessfully(tempOutDir).message);

terraform 1.5.7

  1. Ubuntu case (toPlan issue):
    
    Node 18.17.1
    Expected subject to be a valid terraform stack: SyntaxError: Unexpected token c in JSON at position 1.
    Expected subject not to plan successfully

Node 20.6.1 subject to be a valid terraform stack: SyntaxError: Unexpected token 'c', "[command]/h"... is not valid JSON. Expected subject not to plan successfully


2. macOS case (toBeValid issue):

Node 18.17.1 Expected subject to be a valid terraform stack: SyntaxError: Unexpected token c in JSON at position 1. Expected subject to plan successfully: Error: Command failed: terraform plan -input=false -lock=false .

Node 20.6.1 subject to be a valid terraform stack: SyntaxError: Unexpected token 'c', "[command]/U"... is not valid JSON. Expected subject to plan successfully: Error: Command failed: terraform plan -input=false -lock=false .



It works locally macOS Terraform v1.5.7 on darwin_amd64.
Danielku15 commented 12 months ago

This also leads for me to subsequent errors when using the CDK https://github.com/hashicorp/terraform-cdk-action

Was hunting down for hours this problem until I realized that this problem is there. I was able to workaround this problem with terraform_wrapper: false. I don't need the outputs.

austinvalle commented 10 months ago

This issue will be fixed in an upcoming v3.0.0 release next week. Please see this comment for more info

github-actions[bot] commented 3 months ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.