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

Starting a local variable with digits throws an incorrect error message #34146

Open
strugee opened this issue Oct 27, 2023 · 1 comment
Open

Comments

@strugee
Copy link
Contributor

strugee commented Oct 27, 2023

Terraform Version

% terraform version
Terraform v1.6.2
on linux_amd64

Terraform Configuration Files

locals {
  2023_test = "foobar"
}

Debug Output

(Ignoring the request to use a Gist instead of pasting here because the testcase is so trivial that the debug output isn't actually that long.)

% TF_LOG=trace terraform plan
2023-10-26T20:25:19.051-0500 [INFO]  Terraform version: 1.6.2
2023-10-26T20:25:19.051-0500 [DEBUG] using github.com/hashicorp/go-tfe v1.36.0
2023-10-26T20:25:19.051-0500 [DEBUG] using github.com/hashicorp/hcl/v2 v2.19.1
2023-10-26T20:25:19.051-0500 [DEBUG] using github.com/hashicorp/terraform-svchost v0.1.1
2023-10-26T20:25:19.051-0500 [DEBUG] using github.com/zclconf/go-cty v1.14.1
2023-10-26T20:25:19.051-0500 [INFO]  Go runtime version: go1.21.1
2023-10-26T20:25:19.051-0500 [INFO]  CLI args: []string{"terraform", "plan"}
2023-10-26T20:25:19.051-0500 [TRACE] Stdout is a terminal of width 80
2023-10-26T20:25:19.051-0500 [TRACE] Stderr is a terminal of width 80
2023-10-26T20:25:19.051-0500 [TRACE] Stdin is a terminal
2023-10-26T20:25:19.051-0500 [DEBUG] Attempting to open CLI config file: /var/home/alex/.terraformrc
2023-10-26T20:25:19.051-0500 [DEBUG] File doesn't exist, but doesn't need to. Ignoring.
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory terraform.d/plugins
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory /var/home/alex/.terraform.d/plugins
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory /var/home/alex/.local/share/terraform/plugins
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory /var/home/alex/.local/share/flatpak/exports/share/terraform/plugins
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory /var/lib/flatpak/exports/share/terraform/plugins
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory /usr/local/share/terraform/plugins
2023-10-26T20:25:19.051-0500 [DEBUG] ignoring non-existing provider search directory /usr/share/terraform/plugins
2023-10-26T20:25:19.052-0500 [INFO]  CLI command args: []string{"plan"}
╷
│ Error: Argument or block definition required
│ 
│   on test.tf line 2, in locals:
│    2:   2023_test = "foobar"
│ 
│ An argument or block definition is required here.
╵

Expected Behavior

I should have seen something similar to this error:

╷
│ Error: Invalid resource name
│ 
│   on dns.tf line 209, in resource "aws_route53_record" "2023-ephemeral-delegation-a":
│  209: resource "aws_route53_record" "2023-ephemeral-delegation-a" {
│ 
│ A name must start with a letter or underscore and may contain only letters, digits, underscores, and dashes.
╵

Actual Behavior

I saw an error about an argument or block definition being required, even though AFAICT that was provided.

Steps to Reproduce

  1. Create test.tf with the repro file contents
  2. terraform plan

Additional Context

No response

References

No response

@strugee strugee added bug new new issue not yet triaged labels Oct 27, 2023
@apparentlymart
Copy link
Contributor

Hi @strugee,

In this situation Terraform is encountering a number and noticing that a number cannot possibly be the start of "an argument or block definition". Unfortunately with syntax errors Terraform often has to do the best it can with partial information, because before there's even a complete parse tree any assumptions Terraform is making about context are only guesses.

With that said, given that a number is never valid in this particular context I imagine it would be possible to add a special case to the parser for when it encounters a number here. I don't think it would be appropriate to use an error exactly like the other one you shared about resource names -- Terraform is far more certain that the string there is intended to be a resource name, because main parsing was already complete -- but we could perhaps compromise by adding an extra sentence to the detail part of the error message:

╷
│ Error: Argument or block definition required
│ 
│   on test.tf line 2, in locals:
│    2:   2023_test = "foobar"
│ 
│ An argument or block definition is required here. Argument names and
│ block types cannot begin with a digit.
╵

Thanks for reporting this! The code that's handling this actually lives upstream in the HCL repository, so I'm going to label this as "upstream" but I think we should still use this issue to represent the problem since it's most likely to be the Terraform team working on this change.

@apparentlymart apparentlymart added enhancement upstream config and removed bug new new issue not yet triaged labels Oct 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants