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

"Invalid character" when evaluating expressions with directives #42

Closed
adriannieto-attechnest opened this issue Oct 24, 2022 · 4 comments · Fixed by #43
Closed

"Invalid character" when evaluating expressions with directives #42

adriannieto-attechnest opened this issue Oct 24, 2022 · 4 comments · Fixed by #43
Labels
bug Something isn't working

Comments

@adriannieto-attechnest
Copy link

adriannieto-attechnest commented Oct 24, 2022

Latest ruleset (0.2.0) is not able to check terraform code, this doesn't not happen with version 0.1.1:

Broken code:

resource "local_file" "inventory" {
    filename = "${var.inventory_dir}/hosts.yaml"
    content = <<HOSTS
all:
  children:
    bastion:
      hosts:
%{ for addr in module.bastion.public_ip[*] ~}
        ${addr}
%{ endfor ~}   
HOSTS
}

Error:

tflint                                                                                                                                                
Failed to check ruleset; Failed to check `terraform_deprecated_index` rule: environment.tf:9,9-10: Invalid character; This character is not used within the language., and 183 other diagnostic(s)

Plugin config:

plugin "terraform" {
  enabled = true
  version = "0.2.0"
  source  = "github.com/terraform-linters/tflint-ruleset-terraform"
  preset  = "recommended"
}

plugin "aws" {
    enabled = true
    deep_check = false
    version = "0.17.1"
    source  = "github.com/terraform-linters/tflint-ruleset-aws"    
}

Versions:

tf version                                                                                                                                       
Terraform v1.3.3
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v4.24.0
+ provider registry.terraform.io/hashicorp/dns v3.2.3
+ provider registry.terraform.io/hashicorp/local v2.2.3
+ provider registry.terraform.io/hashicorp/tls v4.0.1

tflint -v                                                                                                                                               
TFLint version 0.42.0
+ ruleset.terraform (0.2.0)
+ ruleset.aws (0.17.1)
@bendrucker bendrucker changed the title Failed to check ruleset; Failed to check terraform_deprecated_index rule: environment.tf:9,9-10: Invalid character; This character is not used within the language., and 183 other diagnostic(s) terraform_deprecated_index: "This character is not used within the language" Oct 24, 2022
@bendrucker bendrucker changed the title terraform_deprecated_index: "This character is not used within the language" "Invalid character" when evaluating expressions with directives Oct 24, 2022
@bendrucker bendrucker transferred this issue from terraform-linters/tflint-ruleset-terraform Oct 24, 2022
@bendrucker bendrucker added the bug Something isn't working label Oct 24, 2022
@bendrucker
Copy link
Member

bendrucker commented Oct 24, 2022

Moved to tflint as it seems very unlikely this is localized to the terraform ruleset, meaning the root cause is the CLI or plugin SDK. Assuming that's the case, this bug would arise anytime a plugin tries to evaluate an attribute containing a directive in its expression. The terraform ruleset just does a lot of meta-evaluation, i.e. visiting all resource attributes rather than 1 specific one.

@adriannieto-attechnest
Copy link
Author

adriannieto-attechnest commented Oct 24, 2022 via email

@pdecat
Copy link

pdecat commented Oct 24, 2022

Using TFLint version 0.42.1, downgrading the terraform ruleset from 0.2.0-bundled to 0.1.1 does not help for me:

# tflint -v
TFLint version 0.42.1
+ ruleset.terraform (0.2.0-bundled)

# pre-commit run --all-files terraform_tflint
Terraform validate with tflint...........................................Failed
- hook id: terraform_tflint
- exit code: 1

Command 'tflint --init' successfully done:




TFLint in modules/deploy/:
Failed to check ruleset; Failed to check `terraform_deprecated_index` rule: main.tf:52,8-9: Invalid character; This character is not used within the language., and 57 other diagnostic(s)
# cat <<EOF > .tflint.hcl
plugin "terraform" {
    enabled = true
    version = "0.1.1"
    source  = "github.com/terraform-linters/tflint-ruleset-terraform"
}
EOF

# tflint --init
Installing `terraform` plugin...
Installed `terraform` (source: github.com/terraform-linters/tflint-ruleset-terraform, version: 0.1.1)

# tflint --version
TFLint version 0.42.1
+ ruleset.terraform (0.1.1)

# pre-commit run --all-files terraform_tflint
Terraform validate with tflint...........................................Failed
- hook id: terraform_tflint
- exit code: 1

Command 'tflint --init' successfully done:
Plugin `terraform` is already installed



TFLint in modules/deploy/:
Failed to check ruleset; Failed to check `terraform_deprecated_index` rule: main.tf:52,8-9: Invalid character; This character is not used within the language., and 57 other diagnostic(s)

For a complete job log, see https://github.com/terraform-aws-modules/terraform-aws-lambda/actions/runs/3312065827/jobs/5468288824

@bendrucker
Copy link
Member

Hmm, so debugging from the bottom up identified this as the source of the error diagnostics:

tokens, diags := hclsyntax.LexExpression(bytes, filename, variable.SourceRange().Start)

I'm able to reproduce the error in the tests on v0.2.0, but not v0.1.1. Which means that #34 is the root cause, via evaluating more expressions than previous. Presumably Terraform handles these directive expressions as a special case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
3 participants