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

Unset variable in backend.tf causes crash #33622

Closed
voltagex opened this issue Aug 2, 2023 · 4 comments · Fixed by #33628
Closed

Unset variable in backend.tf causes crash #33622

voltagex opened this issue Aug 2, 2023 · 4 comments · Fixed by #33628
Assignees
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code

Comments

@voltagex
Copy link

voltagex commented Aug 2, 2023

Terraform Version

Terraform v1.5.4
on windows_amd64

Terraform Configuration Files

backend.tf

terraform {
    backend "local" {
        path = ${some.var}
    }
}

Debug Output

2023-08-02T20:56:57.949+1000 [INFO]  Terraform version: 1.5.4
2023-08-02T20:56:57.949+1000 [DEBUG] using github.com/hashicorp/go-tfe v1.26.0
2023-08-02T20:56:57.949+1000 [DEBUG] using github.com/hashicorp/hcl/v2 v2.16.2
2023-08-02T20:56:57.949+1000 [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.0
2023-08-02T20:56:57.949+1000 [DEBUG] using github.com/zclconf/go-cty v1.12.2
2023-08-02T20:56:57.949+1000 [INFO]  Go runtime version: go1.20
2023-08-02T20:56:57.949+1000 [INFO]  CLI args: []string{"C:\\Users\\Adam\\OneDrive\\bin\\terraform.exe", "init"}
2023-08-02T20:56:57.950+1000 [TRACE] Stdout is a terminal of width 169
2023-08-02T20:56:57.950+1000 [TRACE] Stderr is a terminal of width 169
2023-08-02T20:56:57.950+1000 [TRACE] Stdin is a terminal
2023-08-02T20:56:57.951+1000 [DEBUG] Attempting to open CLI config file: C:\Users\Adam\AppData\Roaming\terraform.rc
2023-08-02T20:56:57.951+1000 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2023-08-02T20:56:57.952+1000 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2023-08-02T20:56:57.952+1000 [DEBUG] ignoring non-existing provider search directory C:\Users\Adam\AppData\Roaming\terraform.d\plugins
2023-08-02T20:56:57.952+1000 [DEBUG] ignoring non-existing provider search directory C:\Users\Adam\AppData\Roaming\HashiCorp\Terraform\plugins
2023-08-02T20:56:57.952+1000 [INFO]  CLI command args: []string{"init"}

Initializing the backend...
2023-08-02T20:56:57.953+1000 [TRACE] Meta.Backend: built configuration for "local" backend with hash value 3318668199
2023-08-02T20:56:57.953+1000 [TRACE] Meta.Backend: backend has not previously been initialized in this working directory
2023-08-02T20:56:57.953+1000 [DEBUG] New state was assigned lineage "4de671a5-29e2-0c30-a8fa-11fe76ef5067"
2023-08-02T20:56:57.953+1000 [TRACE] Meta.Backend: moving from default local state only to "local" backend
2023-08-02T20:56:57.953+1000 [DEBUG] checking for provisioner in "."
2023-08-02T20:56:57.954+1000 [DEBUG] checking for provisioner in "C:\\Users\\Adam\\OneDrive\\bin"
2023-08-02T20:56:57.954+1000 [TRACE] backend/local: state manager for workspace "default" will:
 - read initial snapshot from terraform.tfstate
 - write new snapshots to terraform.tfstate
 - create any backup at terraform.tfstate.backup
2023-08-02T20:56:57.954+1000 [TRACE] statemgr.Filesystem: reading initial snapshot from terraform.tfstate
2023-08-02T20:56:57.954+1000 [TRACE] statemgr.Filesystem: snapshot file has nil snapshot, but that's okay
2023-08-02T20:56:57.954+1000 [TRACE] statemgr.Filesystem: read nil snapshot
2023-08-02T20:56:57.954+1000 [TRACE] Meta.Backend: ignoring local "default" workspace because its state is empty

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

Terraform crashed! This is always indicative of a bug within Terraform.
Please report the crash with Terraform[1] so that we can fix this.

When reporting bugs, please include your terraform version, the stack trace
shown below, and any additional information which may help replicate the issue.

[1]: https://github.com/hashicorp/terraform/issues

!!!!!!!!!!!!!!!!!!!!!!!!!!! TERRAFORM CRASH !!!!!!!!!!!!!!!!!!!!!!!!!!!!

value is unknown
goroutine 1 [running]:
runtime/debug.Stack()
        /opt/hostedtoolcache/go/1.20.0/x64/src/runtime/debug/stack.go:24 +0x65
runtime/debug.PrintStack()
        /opt/hostedtoolcache/go/1.20.0/x64/src/runtime/debug/stack.go:16 +0x19
github.com/hashicorp/terraform/internal/logging.PanicHandler()
        /home/runner/work/terraform/terraform/internal/logging/panic.go:58 +0x153
panic({0x224a640, 0x2ce6c30})
        /opt/hostedtoolcache/go/1.20.0/x64/src/runtime/panic.go:890 +0x263
github.com/zclconf/go-cty/cty.Value.AsString({{{0x2d10500?, 0xc00011e96d?}}, {0x22ff700?, 0x41c84a0?}})
        /home/runner/go/pkg/mod/github.com/zclconf/[email protected]/cty/value_ops.go:1265 +0x111
github.com/hashicorp/terraform/internal/backend/local.(*Local).PrepareConfig(0x2d105e0?, {{{0x2d105e0?, 0xc0009a2b80?}}, {0x232cd00?, 0xc0009b8b10?}})
        /home/runner/work/terraform/terraform/internal/backend/local/backend.go:134 +0xde
github.com/hashicorp/terraform/internal/command.(*Meta).backendInitFromConfig(0x0?, 0xc00030d9a0)
        /home/runner/work/terraform/terraform/internal/command/meta_backend.go:1391 +0x527
github.com/hashicorp/terraform/internal/command.(*Meta).backend_C_r_s(0xc0002fdc00, 0xc00030d9a0, 0xc5cedba7, 0xc00016d920, 0xc00082cee8)
        /home/runner/work/terraform/terraform/internal/command/meta_backend.go:984 +0x8d2
github.com/hashicorp/terraform/internal/command.(*Meta).backendFromConfig(0xc0002fdc00, 0xc00082cee8)
        /home/runner/work/terraform/terraform/internal/command/meta_backend.go:644 +0x14b3
github.com/hashicorp/terraform/internal/command.(*Meta).Backend(0xc0002fdc00, 0x0?)
        /home/runner/work/terraform/terraform/internal/command/meta_backend.go:104 +0x73
github.com/hashicorp/terraform/internal/command.(*InitCommand).initBackend(0xc0002fdc00, 0xc0009b60d0, {{0x271eb7d?, 0x273305e?}, 0xc00052bc08?})
        /home/runner/work/terraform/terraform/internal/command/init.go:469 +0x77b
github.com/hashicorp/terraform/internal/command.(*InitCommand).Run(0xc0002fdc00, {0xc0001183b0, 0x0, 0x0})
        /home/runner/work/terraform/terraform/internal/command/init.go:186 +0xf33
github.com/mitchellh/cli.(*CLI).Run(0xc00011bcc0)
        /home/runner/go/pkg/mod/github.com/mitchellh/[email protected]/cli.go:262 +0x5f8
main.realMain()
        /home/runner/work/terraform/terraform/main.go:318 +0x1749
main.main()
        /home/runner/work/terraform/terraform/main.go:61 +0x19

Expected Behavior

Probably an error about variables not being supported in backend.tf, or an error about the variable not being set

Actual Behavior

Crash, as seen above

Steps to Reproduce

  1. create backend.tf with the invalid variable
  2. terraform init
  3. crash

Additional Context

No response

References

No response

@voltagex voltagex added bug new new issue not yet triaged labels Aug 2, 2023
@liamcervante liamcervante added confirmed a Terraform Core team member has reproduced this issue and removed new new issue not yet triaged labels Aug 2, 2023
@liamcervante
Copy link
Member

Hi @voltagex, thanks for filing this!

I can confirm I can replicate this. Interestingly I've found it's not the unset variable that's causing the crash but the presence of the $ character. For example, path = something gets a nice error message while path = $something gets the crash.

Definitely still a bug, and I'll investigate this a bit more to see what sort of fix we can get in place.

@liamcervante liamcervante self-assigned this Aug 2, 2023
@voltagex
Copy link
Author

voltagex commented Aug 2, 2023

I didn't think of modifying the value too much when I got that crash - good catch!

@liamcervante
Copy link
Member

Okay, I've investigated this and discovered the root cause.

During the init operation Terraform deliberately ignores configuration errors early on in favour of validating the required_versions check. Next, Terraform attempts to initialise the backend using the bad config as it can't perform the version check without the backend initialised. The backend then crashes when it comes across the value it does not expect, which is what we are seeing here.

There's two options I can see, the first is to make the backend loading process more careful so that it doesn't assume the configuration is in a valid state. The second would be to make Terraform error out after it finds bad configuration even if it hasn't yet checked the version requirements.

I haven't decided which of the two I like more yet.

Copy link
Contributor

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants