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

Terraform + Tofu Plugins Bug: terraform fmt + tofu fmt erroneously run against **.tf.json files #941

Closed
Gowiem opened this issue Dec 27, 2024 · 1 comment · Fixed by #945

Comments

@Gowiem
Copy link

Gowiem commented Dec 27, 2024

Info

When committing / changing our backend.tf.json file (which is a standard file in both tofu + terraform), both plugins are trying to run their fmt command against those files. This results in the following error:

trunk_cli_version: 1.22.8
title: terraform exited with exit_code=2
report:
  - terraform exited with exit_code=2
  - linter:
      command: |
        /<MY_USER_PATH>/.cache/trunk/tools/terraform/1.1.0-6bc9ed0aa441bfb0bf13ee36e04a2bde/terraform fmt -no-color -
      stdin_path: |
        /private/var/folders/7l/fp_bz3gx6yzg24hw08ww9vp00000gn/T/trunk-501/GlML4Y/root-modules/kms/backend.tf.json
      run_from: /private/var/folders/7l/fp_bz3gx6yzg24hw08ww9vp00000gn/T/trunk-501/GlML4Y
      timeout: 10m
      rerun: (cd /private/var/folders/7l/fp_bz3gx6yzg24hw08ww9vp00000gn/T/trunk-501/GlML4Y; cat /private/var/folders/7l/fp_bz3gx6yzg24hw08ww9vp00000gn/T/trunk-501/GlML4Y/root-modules/kms/backend.tf.json | env -i PATH=/<MY_USER_PATH>/.cache/trunk/tools/terraform/1.1.0-6bc9ed0aa441bfb0bf13ee36e04a2bde:/<MY_USER_PATH>/.cache/trunk/tools/terraform/1.1.0-6bc9ed0aa441bfb0bf13ee36e04a2bde HOME=/<MY_USER_PATH> /<MY_USER_PATH>/.cache/trunk/tools/terraform/1.1.0-6bc9ed0aa441bfb0bf13ee36e04a2bde/terraform fmt -no-color -)
      affects_cache:
        []
      direct_configs:
        []
      exit_status: exited
      exit_code: 2
      stdout: (none)
      stderr: |

        Error: Argument or block definition required

          on <stdin> line 1:
           1: {

        An argument or block definition is required here.


    parser: |
      (none)

If you run terraform fmt against that file directly via terraform fmt -no-color backend.tf.json then you get the following:

Error: Only .tf, .tfvars, and .tftest.hcl files can be processed with terraform fmt

This goes to say that trunk should not be trying to run fmt against these ****.tf.json files and this now causing a false error.

We've been using these plugins for a while and this is the first that we're running into this. So we believe something must have changed upstream in trunk check itself?

trunk.yaml

version: 0.1
cli:
  version: 1.22.8
plugins:
  sources:
    - id: trunk
      ref: v1.6.6
      uri: https://github.com/trunk-io/plugins
lint:
  disabled:
    # TODO: Re-enable checkov once the below issue is fixed:
    # https://github.com/bridgecrewio/checkov/issues/6915
    # This is fixed in https://github.com/bridgecrewio/checkov/pull/6918, but that is not released yet.
    - checkov
  enabled:
    - [email protected]
    - [email protected]
    - [email protected]
    - [email protected]
    - [email protected]
    - [email protected]
    - [email protected]
    - git-diff-check
    - [email protected]
    - [email protected]
    - [email protected]
    - [email protected]
runtimes:
  enabled:
    - [email protected]
    - [email protected]
    - [email protected]
actions:
  disabled:
    - trunk-announce
  enabled:
    - trunk-cache-prune
    - trunk-check-pre-push
    - trunk-fmt-pre-commit
    - trunk-upgrade-available
@TylerJang27
Copy link
Collaborator

Hi! Thanks for flagging this. I have a repro and confirm the issue. This file config has been the case in Trunk for as long as I can remember, and we haven't made any recent changes that would have affected this, so I'm not sure why this would suddenly crop up for you.

Regardless, I will raise a fix to handle this. In the meantime, you can always add an ignore for those .tf.json files.

TylerJang27 added a commit that referenced this issue Dec 30, 2024
Fixes #941. Verified repro
with terraform and tofu format on `x.tf.json` files. Did a bit of
research as well to make sure our other security scanners don't scan
`.tf.json` files, and we should be fine.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

2 participants