-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
Converting cty.NilVal to cty.String panics #43
Comments
Hi @azr, It's intentional that I think the bug here is in the HCL layer instead, but I'm not entirely sure where. The The contract for Hopefully the above is a useful starting point! I'm not sure where exactly the issue is but I'm happy to give some more pointers if needed. Might be worth opening in issue for this over in the HCL repository so we can discuss it more over there. |
Hey Martin ! Thanks for the explanation. You are right, this variable code that will This was handy on the unit testing side because it would be silent when a variable was unused and using the variable would error: But a Packer run, tries to get as far as possible, which caused this panic. I changed it to
I thought this was really missleading for users, so I tried something else I added a piece of code that checks that all variables are set, but then this is also not the best because a user can define an unused variable but must set it. Trying to look for a better option. For now my thinking is that it would be super nice if the hcl.EvalContext allowed to be more dynamic, for example:
But this is more of an hcl topic :D |
Hi @azr, If you want to represent the absence of a value in "userspace" (that is, representing something absent in the space of the configuration language rather than in the space of the Go program implementing it) then you could use If you do that, then the The main thing here is that an As some additional context, here's how Terraform deals with this problem: By default, a Terraform variable is required and so Terraform would emit an error if it isn't set. An author can make a variable optional by providing a default value: variable "example" {
type = string
default = "Hello"
} In some cases there isn't any reasonable default value, and the module simply wants to detect whether or not the value is set. In that case, the author can specify the default as variable "example" {
type = string
default = null
} When preparing the object If the module elsewhere contains an expression like |
Howdy @apparentlymart many thanks for explaining this; I superconfused myself reading the docs over and now reading your comment it's super clear 👍 👍 👍 👍 , I think this can be closed. |
Hello there ! We had this issue in packer: hashicorp/packer#8730
Basically, the default value of a variable is not set but its type is and when a user tries to access that variable without setting it, a panic occurs.
This was actually tested in the Packer codebase and successfully errored with the following error:
But this panics 'in real life' because packer uses
"github.com/zclconf/go-cty/cty/json".Marshal(value, cty.DynamicPseudoType)
I will try to open an PR to fix this, but I might fix something in the wrong place.
Here's a stack trace:
Edit: I tried introducing Marshaling there and there and it did not trigger the panic.
The text was updated successfully, but these errors were encountered: