Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Error when var file includes undeclared variables #2072

Closed
rpdelaney opened this issue Jun 28, 2024 · 7 comments
Closed

Error when var file includes undeclared variables #2072

rpdelaney opened this issue Jun 28, 2024 · 7 comments

Comments

@rpdelaney
Copy link

Introduction

Terraform gives appropriate warnings for values found in tfvars without a corresponding variable in the configuration:

2024-06-28_09-19-19

Proposal

tflint should also raise errors for the same problem.

References

N/A

@bendrucker
Copy link
Member

bendrucker commented Jun 28, 2024

Duplicate of #1877
Resolved #1941

tflint --var 'foo=bar'
Failed to parse variables; <value for var.foo>:1,1-1: Value for undeclared variable; A variable named "foo" was assigned, but the root module does not declare a variable of that name.:

Error: Value for undeclared variable

  on <value for var.foo> line 1:
  (source code not available)

A variable named "foo" was assigned, but the root module does not declare a variable of that name.

@rpdelaney
Copy link
Author

@bendrucker I'm sorry, but I'm not sure I understand how this is a solution for me. As a user I want to use tflint in CI to scan for tfvars settings that do not have a corresponding variable. Adding extra command-line switches does not help because that would require me to do the analysis myself, wouldn't it?

@rpdelaney
Copy link
Author

Reading the tickets you mentioned, they seem to describe a very different issue than the new feature I'm proposing.

@bendrucker
Copy link
Member

Hmm, I looked at --var as a shortcut but it turns out --var-file behaves differently and does not error.

echo 'foo = "bar"' > terraform.tfvars
tflint --var-file terraform.tfvars
# nothing...

#1941 says:

Note that the environment variables TF_VAR_* are not subject to this check. This is the same behavior as Terraform.

Given that the goal is to error in the cases where Terraform prints a warning, this is looking more like a bug/inconsistency.

@bendrucker bendrucker reopened this Jun 28, 2024
@bendrucker bendrucker changed the title Feature proposal: Value in tfvars for undeclared variable Error when var file includes undeclared variables Jun 28, 2024
@bendrucker bendrucker added the bug label Jun 28, 2024
@wata727
Copy link
Member

wata727 commented Jun 29, 2024

See https://github.com/hashicorp/terraform/blob/v1.9.0/internal/backend/backendrun/unparsed_value.go#L58-L61

Reading the above comment, it seems like Terraform intentionally makes this a warning rather than an error. There is room for discussion on whether TFLint should actively raise an error or conservatively keep the same behavior as Terraform.

Personally, I think it might be better to provide it as a rule that can be enabled when needed.

@rpdelaney
Copy link
Author

If tflint returns a message with the same log level for this as e.g. when a local variable is declared but unused, that would feel internally consistent, no?

@wata727
Copy link
Member

wata727 commented Jun 30, 2024

It's consistent, but I'm not sure if it helps. Unlike Terraform warnings, TFLint logs are intended for debugging, so they may not be useful for finding typos or other problems.

In your use case, does this help?

@bendrucker bendrucker removed the bug label Jul 1, 2024
@terraform-linters terraform-linters locked and limited conversation to collaborators Jul 1, 2024
@bendrucker bendrucker converted this issue into discussion #2080 Jul 1, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Development

No branches or pull requests

3 participants