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

replace function cannot do double backslashes #8709

Closed
llarsson opened this issue Sep 7, 2016 · 5 comments
Closed

replace function cannot do double backslashes #8709

llarsson opened this issue Sep 7, 2016 · 5 comments
Assignees
Milestone

Comments

@llarsson
Copy link

llarsson commented Sep 7, 2016

I need to use the replace function to replace dots in a domain name with two backslashes. Sounds simple, right? Well, it is not. Terraform's replace function does not work entirely as advertised. See the following example:

variable "sub_domain" { default = "needs.double.backslashes.com"}

output "with_spaces_works" {
  value = "${replace(var.sub_domain, ".", "\\ \\.")}"
}

output "buggy" {
  value = "${replace(var.sub_domain, ".", "\\\\.")}"
}

output "doubling_does_not_work" {
  value = "${replace(var.sub_domain, ".", "\\\\\\\\.")}"
}

I would expect to have the value of "buggy" be needs\\.double\\.backslashes\\.com, but it is not. It is in fact needs\.double\.backslashes\.com.

The entire output from the example above (save it to a file, run terraform apply on it) is:

Outputs:

buggy = needs\.double\.backslashes\.com
doubling_does_not_work = needs\.double\.backslashes\.com
with_spaces_works = needs\ \.double\ \.backslashes\ \.com

Please fix this. :)

(My particular use case is that I need to feed a string like this into a JSON file and then into a regex parser, so I need to escape the periods twice.)

This is using Terraform 0.7.3.

@llarsson
Copy link
Author

llarsson commented Sep 7, 2016

output "buggy_but_with_letters" {
  value = "${replace(var.sub_domain, ".", "AA.")}"
}

Shows that had backslash handling been more correct, it would not have been an issue.

Also, removing the period as in the following example is actually apparently a syntax error, which makes me believe that backslash handling is rather broken:

output "spilling" {
  value = "${replace(var.sub_domain, ".", "\\\\")}"
}

@mitchellh
Copy link
Contributor

I'll look into this, feels like an HCL bug. Thanks for the easy repros. I haven't tried them yet but they're comprehensive here and give me faith.

@mitchellh mitchellh self-assigned this Sep 9, 2016
@mitchellh
Copy link
Contributor

This is fixed and explained here: hashicorp/hcl#150

Unfortunately this has a chance to break working TF configs so we're going to have to hold this until TF 0.8. :( I'm adding it to that milestone so we know. I'm also going to hijack the top of your issue to make a note to my future self.

@mitchellh mitchellh added this to the Terraform 0.8 milestone Sep 10, 2016
mitchellh added a commit that referenced this issue Nov 9, 2016
mitchellh added a commit that referenced this issue Nov 9, 2016
@mitchellh
Copy link
Contributor

We got the fix in, will be part of 0.8 beta 1 this week.

@ghost
Copy link

ghost commented Apr 20, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 20, 2020
This issue was closed.
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

2 participants