hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.49k stars 9.38k forks source link

Partial diff is not done for jsondecode #35123

Closed orgads closed 1 month ago

orgads commented 1 month ago

Terraform Version

Terraform v1.8.0
on windows_amd64
+ provider registry.terraform.io/hashicorp/random v3.6.1

Terraform Configuration Files

resource "random_string" "test" {
  length  = 8 # Change this to 9 and reapply
  special = false
}

resource "terraform_data" "test" {
  triggers_replace = jsondecode(jsonencode({
    constant = "constant"
    random   = random_string.test.result
  }))
}

Debug Output

https://gist.github.com/orgads/bddc77184dd63e8c176e0b5d4ea2bbd6

Expected Behavior

constant should remain unchanged, and only the changed attribute should be displayed as diff:

  # terraform_data.test must be replaced
-/+ resource "terraform_data" "test" {
      ~ id               = "cec378fc-caa3-04a1-2f95-24d7aa08c00a" -> (known after apply)
      ~ triggers_replace = {
          ~ random   = "txsdyUJC" -> (known after apply)
            # (1 unchanged attribute hidden)
        }
    }

Actual Behavior

  # terraform_data.test must be replaced
-/+ resource "terraform_data" "test" {
      ~ id               = "cca8891a-0e87-736c-ad5e-64b2723e16d6" -> (known after apply)
      ~ triggers_replace = {
          - constant = "constant"
          - random   = "txsdyUJC"
        } -> (known after apply) # forces replacement
    }

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. Change length to 9
  4. terraform apply

Additional Context

No response

References

No response

jbardin commented 1 month ago

Hi @orgads,

Thanks for filing the issue. The plan renderer is working as expected here, because you have create a single value which is entirely unknown. Passing the entire map through jsonencode then jsondecode leaves the final value entirely unknown, which is why the whole map is marked with -> (known after apply). The intermediate value after jsonencode is a single unknown string, the jsonedecode function has no way to determine which map values were originally unknown from that string.

We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help.

Thanks!

orgads commented 1 month ago

I thought that was a bug, but given your explanation I realize it is expected. Thanks!

github-actions[bot] commented 1 week 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.