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

Plan unable to execute with Space in key=value --> key = value #30600

Closed
mohitwasnik132 opened this issue Mar 2, 2022 · 7 comments · Fixed by #30985
Closed

Plan unable to execute with Space in key=value --> key = value #30600

mohitwasnik132 opened this issue Mar 2, 2022 · 7 comments · Fixed by #30985
Assignees

Comments

@mohitwasnik132
Copy link

Putting a space between variable name and value causes a error message

F:\terraform-scripts\>terraform plan -var "length = 2"

│ Error: Value for undeclared variable
│
│ A variable named "length " was assigned on the command line, but the root module does not declare a variable of that name. To use this value, add a "variable" block to the configuration.

while

F:\terraform-scripts\>terraform plan -var "length=2" -var "separator=."   

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Executes perfectly.

Terraform Version

Terraform v1.1.6
on windows_amd64
+ provider registry.terraform.io/hashicorp/aws v4.3.0

Terraform Configuration Files

resource "random_pet" "spacey" {
  prefix     = var.prefix[1]
  length     = var.length
  separator = var.separator
}

variable "prefix" {
  type    = list(any)
  default = ["Err", "Bugy", "meh"]
}

variable "length" {
  default = "1"
}

variable "separator" {
  default = "."
}

output "pet" {
  value = random_pet.spacey
}

Expected Behavior

on

terraform plan  -var "variable1name = value1" -var "variable2name = value2"

Please notice the "space" before and after "="
The plan should've executed successfully. Which it does, when spaces are removed (find the attached image and code).

Actual Behavior

The code spews confusing error , the one stated at the start of this post.

This error should be prompting to remove spaces instead of

│ Error: Value for undeclared variable

Steps to Reproduce

Just regular terraform workflow.

terraform plan -var "length = 2" -var "separator = ."
#causes one two errors one for each argument

terraform plan -var "length = 2" -var "separator=."
#causes one error one for length argument

terraform plan -var "length = 2" -var "separator=."
#no space no errors. Executes

It could be any code with spaces around "=" as you assign -var values.

Additional Context

Since variable is declared. Values are ok. It's just a small-innocent mistake(?) at user's part.
I don't know if others have reported this issue, (I could be naive and posting it repeat)
Or if it qualifies as Documentation/Feature request, So here I am.

Seems like it isn't the bug/issue, from the issue#11 as it's closed.
but documents still lack and could potentially lead a beginner astray.
Please Consider updating/mentioning this into the documentation, if this issue can't be helped/resolved.

References

Input Variable Documentation
Variables Documentation
tf-helper issue #11
bug-space-in-var

@mohitwasnik132 mohitwasnik132 added bug new new issue not yet triaged labels Mar 2, 2022
@crw
Copy link
Collaborator

crw commented Mar 3, 2022

Thanks for this issue report. I'm going to float it past the docs team to see what they think.

@mohitwasnik132
Copy link
Author

Thanks for this issue report. I'm going to float it past the docs team to see what they think.

That would be wonderful. Thank you @crw

@kmoe kmoe removed the new new issue not yet triaged label Mar 3, 2022
@kmoe
Copy link
Member

kmoe commented Mar 3, 2022

The error message here is certainly confusing. It may be possible to detect the case in which there are spaces around the "=" and return a better error message. In any case this could do with better documentation. Leaving this open in case we fix the error message.

@laurapacilio laurapacilio self-assigned this Mar 3, 2022
@apparentlymart
Copy link
Member

If we do ultimately elect to change the behavior here to treat the spaces differently, I agree with @kmoe that it would be better to still return an error but to make that error more specific, rather than to silently allow it.

Although silently allowing a space before the equals sign would not really hurt too much, because a space is never valid to appear in a variable name anyway, a space after the equals sign should always be taken literally because it is valid for a string value to start with a space, even though I would agree that it's not typical for a string value to start with a space. Given that, I think it's better to be consistent and treat a space before the equals sign as if it were a part of the variable name, and then treat it as an error because there is no variable of that name (and such a name would be impossible, due to the identifier rules).

@mohitwasnik132
Copy link
Author

mohitwasnik132 commented Mar 5, 2022

@apparentlymart I agree. Since nobody can/ "should" make such variable, document on variable character constraints should mention spaces not allowed.
Even though nobody will assign "(space) " as variable value, or start value like that..
A small note should suffice that these fringe cases may throw error with certain error code. That way beginners shall not go on to the wild goose chase or
some one who thinks since "fmt" arranges code stuff symmetrically , why not pass equally good looking symmetrical, well balanced arguments to commands as well, or
someone might happen to copy from code in which case they will only remove the quotes and leave spaces, leading to error (that is something I do for ease of access with local assignments in txt🤓)
An error code or a tip would be of much help without changing current set rules.

@laurapacilio
Copy link
Contributor

Hi all! I put up a PR to help address this issue in the docs. It feels like a simple "Terraform errors if you add spaces" should suffice, but I'm happy to edit if you think it needs more context or detail. Thank you!

#30969

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

Successfully merging a pull request may close this issue.

5 participants