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

Terraform: / in string within interpolated function are not interpreted properly #2087

Open
Sayrus opened this issue Nov 12, 2024 · 2 comments
Labels
bugfix-request A request for a bugfix to be developed.

Comments

@Sayrus
Copy link

Sayrus commented Nov 12, 2024

Name of the lexer
Terraform

Code sample

locals {
        example   = "${function_call("", "", "/")}"
        project_name = project_name
}

image

https://rouge.jneen.net/v4.5.1/terraform/bG9jYWxzIHsKICAgICAgICBleGFtcGxlICAgPSAiJHtmdW5jdGlvbl9jYWxsKCIiLCAiIiwgIi8iKX0iCiAgICAgICAgcHJvamVjdF9uYW1lID0gcHJvamVjdF9uYW1lCn0

Additional context
The function call is has a correct syntax function_call(string, string, string) enclosed with the interpolation sequence${...}. (Code won't pass a linter, you can use example = "${function_call("", "", "/")}/interpolated_string" instead)

Expected behavior:
image

@Sayrus Sayrus added the bugfix-request A request for a bugfix to be developed. label Nov 12, 2024
@Sayrus
Copy link
Author

Sayrus commented Nov 28, 2024

After looking a bit more into it. The root cause is that "/ (%r/"\//) is considered a start for :regexps introduced in #1490. However, as far as I can tell, Regex string are not a special object in Terraform.

The visual test uses the following example (Note that GitHub highlighting considers this a string):

## Object with regular expression
resource "aws_cloudfront_distribution" "s3_distribution" {
  aliases = ["www.${replace(var.domain_name, "/\\.$/", "")}"]
}

The documentation for replace says the following:

If substring is wrapped in forward slashes, it is treated as a regular expression, using the same pattern syntax as [regex](https://developer.hashicorp.com/terraform/language/functions/regex).

The issue from #1304 was that $ was highlighted as an error when the entire thing should have been a string literal. I tried looking for a definition of what could be a Regexp in HCL (https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md) but I couldn't find much.

I see several things that can be done:

  • Remove regexps and ensure strings do not pop for $ but that may break some users use-cases.
  • Ensure a single or double quote can get us out of the expression and backtrack or ensure there is an end to the regex with a lookahead.
  • Find a way to tokenize a Regex first, then dig into it.

Unfortunately, I am not familiar enough with the project to know if the last two are actually implementable or if the lexer only goes forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugfix-request A request for a bugfix to be developed.
Projects
None yet
Development

No branches or pull requests

1 participant