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

tostring does not behave as documented #30899

Closed
cromega opened this issue Apr 20, 2022 · 3 comments
Closed

tostring does not behave as documented #30899

cromega opened this issue Apr 20, 2022 · 3 comments

Comments

@cromega
Copy link

cromega commented Apr 20, 2022

The documentation says that:

Only the primitive types (string, number, and bool) and null can be converted to string. All other values will produce an error.

However, quite clearly, tostring does not perform a type conversion for null values:

> tostring(null)
null

Terraform Version

Terraform v1.1.8
on linux_amd64

Expected Behavior

> tostring(null)
""
@cromega cromega added bug new new issue not yet triaged labels Apr 20, 2022
@jbardin
Copy link
Member

jbardin commented Apr 20, 2022

Hi @cromega,

Thanks for filing the issue. This is being taken care of in #30879. In most cases the tostring conversion of null doesn't matter, since the dynamically typed null will be converted as needed later on, but it can throw off the expected type inference in some cases.

The expected result however is not an empty string, which is distinct from a null value, a properly typed null should be returned here.

Thanks

@jbardin jbardin added config and removed new new issue not yet triaged labels Apr 20, 2022
@apparentlymart
Copy link
Member

Hi @cromega! Thanks for reporting this.

As @jbardin noted, we already by coincidence had #30879 open after we learned about this incorrect behavior from another issue opened recently. That is now merged and will be included in the forthcoming v1.2.0 release.

@jbardin also mentioned that your "Expected Behavior" doesn't match the intended and documented design of functions like tostring. Once you're using a version of Terraform which includes this fix, you'll see your terraform console experiment behave this way instead:

> tostring(null)
tostring(null)

This particular example seems a bit silly because tostring(null) really is the most succinct way to write down "a null value of type string" in the Terraform language, and so Terraform ends up just echoing back what you wrote. But this is now consistent with how terraform console would previously have treated null values of a known type in other situations, such as when inferring an element type for converting a tuple containing strings and untyped nulls:

> tolist(["hello", null])
tolist([
  "hello",
  tostring(null),
])

In the above example, Terraform saw that the element types of ["hello", null] were string and unknown type respectively, and so inferred that the intended type is list(string). In order to produce a value of that type, Terraform converted the unknown-typed null into a string-typed null, represented as tostring(null) in the terraform console result notation.


If you want to write an expression which takes a possibly-null string and returns a definitely-not-null string with "" as the replacement for null then my most ideal suggestion would've been the coalesce function, but that won't work for this particular situation because it inherited a backward-compatibility exception from Terraform v0.11 where it already treats "" as the same as null and so an empty string in particular isn't a valid fallback:

> coalesce(null, "not empty")
"not empty"

> coalesce(null, "")
╷
│ Error: Error in function call
│ 
│   on <console-input> line 1:
│   (source code not available)
│ 
│ Call to function "coalesce" failed: no non-null, non-empty-string arguments.
╵

Because of that, I think the most straightforward way to write such an expression would be using the conditional operator:

var.example != null ? var.example : ""

Since #30879 has now fixed the bug (albeit not exactly in the way you expected), I'm going to close this issue. Thanks again for reporting it!

@github-actions
Copy link

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 May 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants