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

local_file resource crashes when replacing file with empty content #15

Open
ghost opened this issue Jun 22, 2018 · 2 comments · May be fixed by #26
Open

local_file resource crashes when replacing file with empty content #15

ghost opened this issue Jun 22, 2018 · 2 comments · May be fixed by #26
Labels

Comments

@ghost
Copy link

ghost commented Jun 22, 2018

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


Terraform Version

Terraform v0.11.7
+ provider.local v1.1.0

Terraform Configuration Files

##############################################
# Function: chomp
##############################################
##############################################
# Variables
##############################################
variable "chomp" {
  default = "A string with newlines \n\n\n\n\n"
}

##############################################
# Resources
##############################################
resource "local_file" "chomp_file" {
  content  = "${chomp(var.chomp)}"
  filename = "output.txt"
}

##############################################
# Outputs
##############################################
output "chomp_output" {
  value = "${chomp(var.chomp)}"
}

Debug Output

Crash Output

Error: Error applying plan:

1 error(s) occurred:

  • local_file.chomp_file: local_file.chomp_file: diffs didn't match during apply. This is a bug with Terraform and should be reported as a GitHub Issue.

Please include the following information in your report:

Terraform Version: 0.11.7
Resource ID: local_file.chomp_file
Mismatch reason: attribute mismatch: content
Diff One (usually from plan): *terraform.InstanceDiff{mu:sync.Mutex{state:0, sema:0x0}, Attributes:map[string]*terraform.ResourceAttrDiff{"filename":*terraform.ResourceAttrDiff{Old:"output.txt", New:"output.txt", NewComputed:false, NewRemoved:false, NewExtra:interface {}(nil), RequiresNew:false, Sensitive:false, Type:0x0}, "content":*terraform.ResourceAttrDiff{Old:"String with two lines\n Second line with newline ", New:"", NewComputed:false, NewRemoved:false, NewExtra:interface {}(nil), RequiresNew:true, Sensitive:false, Type:0x0}}, Destroy:false, DestroyDeposed:false, DestroyTainted:false, Meta:map[string]interface {}(nil)}
Diff Two (usually from apply): *terraform.InstanceDiff{mu:sync.Mutex{state:0, sema:0x0}, Attributes:map[string]*terraform.ResourceAttrDiff{"filename":*terraform.ResourceAttrDiff{Old:"", New:"output.txt", NewComputed:false, NewRemoved:false, NewExtra:interface {}(nil), RequiresNew:true, Sensitive:false, Type:0x0}}, Destroy:false, DestroyDeposed:false, DestroyTainted:false, Meta:map[string]interface {}(nil)}

Expected Behavior

The local file should have been overwritten with new content, in this case an empty string was submitted, so no content in the file.

Actual Behavior

The file was removed.

Steps to Reproduce

terraform init
terraform apply -var "chomp=String with one newline \n" -auto-approve
terraform apply -var "chomp=" -auto-approve

Additional Context

This only seems to happen when there is content in the file and then I am submitting an empty string as the new content.

References

@kmoe
Copy link
Member

kmoe commented May 10, 2019

With Terraform 0.12 (at hashicorp/terraform@00cc578), this bug no longer causes unexpected behaviour from the user's perspective, but an invalid state file is still produced.

Terraform 0.11

With the reproduction steps above:

State file

{
    "version": 3,
    "terraform_version": "0.11.13",
    "serial": 2,
    "lineage": "fdddd919-d415-ebfd-d5c0-bbc4a19511fc",
    "modules": [
        {
            "path": [
                "root"
            ],
            "outputs": {
                "chomp_output": {
                    "sensitive": false,
                    "type": "string",
                    "value": ""
                }
            },
            "resources": {},
            "depends_on": []
        }
    ]
}

Terraform 0.12

With the reproduction steps above:

Actual behaviour

$ terraform12 apply -var "chomp=" -auto-approve
local_file.chomp_file: Refreshing state... [id=-]
local_file.chomp_file: Destroying... [id=-]
local_file.chomp_file: Destruction complete after 0s
local_file.chomp_file: Creating...
local_file.chomp_file: Creation complete after 0s [id=-]

Apply complete! Resources: 1 added, 0 changed, 1 destroyed.

Outputs:

chomp_output =

File output.txt exists with empty content.

State file

{
  "version": 4,
  "terraform_version": "0.11.13",
  "serial": 7,
  "lineage": "898965df-6eca-bfc8-301d-87d5c5d1175b",
  "outputs": {
    "chomp_output": {
      "value": "",
      "type": "string"
    }
  },
  "resources": [
    {
      "mode": "managed",
      "type": "local_file",
      "name": "chomp_file",
      "provider": "provider.local",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "content": null,
            "filename": "output.txt",
            "id": "-",
            "sensitive_content": null
          }
        }
      ]
    }
  ]
}

Log

2019/05/10 10:13:57 [WARN] Provider "local" produced an invalid plan for local_file.chomp_file, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .content: planned value cty.NullVal(cty.String) does not match config value cty.StringVal("")
2019/05/10 10:13:57 [TRACE] EvalDiff: local_file.chomp_file treating Create change as DeleteThenCreate change to match with earlier plan

@caiges
Copy link

caiges commented Oct 3, 2019

This isn't reproducible with Terrorm 0.12.9. output.txt and the state file are produced correctly.

@kmoe kmoe assigned aeschright and unassigned kmoe Aug 7, 2020
@aeschright aeschright removed their assignment May 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants