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

Changes are planned unnecessarily due to whitespace diffs when using file() function #5500

Open
ghost opened this issue Jan 23, 2020 · 2 comments

Comments

@ghost
Copy link

ghost commented Jan 23, 2020

This issue was originally opened by @nick-doyle as hashicorp/terraform#23928. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

0.12.20

Terraform Configuration Files

resource "azurerm_api_management_api_operation" "my-api-op" {
  operation_id        = "my-api-op"
  api_name            = "${azurerm_api_management_api.my-api.name}"
  api_management_name = "${azurerm_api_management_api.my-api.api_management_name}"
  resource_group_name = "${azurerm_api_management_api.my-api.resource_group_name}"
  display_name        = "Add Item"
  method              = "POST"
  url_template        = "/items"
  description         = "Add an item"

  request {
    representation {
      content_type = "application/json"
      sample       = file("${path.module}/add-item-request-sample.json")
    }
  }

  response {
    description = "Item added"
    status_code = 200

    representation {
      content_type = "application/json"
      sample       = file("${path.module}/add-item-response-sample.json")
    }
  }
}

Debug Output

Crash Output

Expected Behavior

Bearing in mind I am not actually making any Terraform config changes at this moment in time, when I run a 'terraform plan' I expect:

Plan: 0 to add, 0 to change, 0 to destroy.

Actual Behavior

'terraform plan' gives:

# module.api-app.azurerm_api_management_api_operation.my-api-op will be updated in-place
  ~ resource "azurerm_api_management_api_operation" "my-api-op" {
        api_management_name = "my-apim"
        api_name            = "my-api"
        description         = "Add an item"
        display_name        = "Add Item"
        id                  = "my-api-op"
        method              = "POST"
        operation_id        = "add-item"
        resource_group_name = "my-resource-group"
        url_template        = "/items"

      ~ request {
            description = ""

          ~ representation {
                content_type = "application/json"
              ~ sample       = jsonencode( # whitespace changes
                    {
                        <contents of file 'add-item-request-sample.json' appear here>
                    }
                )
            }
        }

      ~ response {
            description = "The item was added"
            status_code = 201

          ~ representation {
                content_type = "application/json"
              ~ sample       = jsonencode( # whitespace changes
                    {
                        <contents of file 'add-item-response-sample.json' appear here>
                    }
                )
            }
        }
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Note the lines which start with 'sample'. Those lines show '# whitespace change' when there have been no changes made at all.

Steps to Reproduce

This problem, whereby Terraform is unnecessarily applying changes due to perceived whitespace change, occurs when two different team members are working on two different operating systems (Unix and Windows).

  1. On a Unix/Mac machine, make some Terraform config change and terraform apply
  2. On the same Unix/Mac machine, do terraform plan and observe that no changes are detected - correctly.
  3. Switch to a Windows machine, check out the latest config used in steps 1&2 and run terraform plan. Observe that it wants to make a change due to whitespace differences, as per output shown above.

The same problem occurs if working the other way round (start with Windows, switch to Unix/Mac).

Additional Context

The problem appears to be down to the way the Unix/Windows versions of Terraform each handle whitespace slightly differently when the file() function runs and a 'jsonencode' statement is produced. It happens for any resource type.

This unnecessary output clutters everything when reviewing changes and results in many more 'apply' commands being run by team members and by CI.

References

@li3
Copy link

li3 commented Jan 11, 2021

Looks like the provider needs to provide a DiffSuppressFunc to suppress whitespace changes. I know this issue is a year old, but it'd be nice to see this fixed at some point.

@ElfoLiNk
Copy link

ElfoLiNk commented Feb 8, 2024

Any updates on this ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants