hashicorp / vscode-terraform

HashiCorp Terraform VSCode extension
https://marketplace.visualstudio.com/items?itemName=HashiCorp.terraform
Mozilla Public License 2.0
919 stars 179 forks source link

Variable references are not recognised in depends_on #1598

Closed radeksimko closed 10 months ago

radeksimko commented 11 months ago
image

Pretty same problem for me. It does not complain when var.tgw_id is referenced under transit_gateway_id, however it it shows error (No declaration found for "var.tgw_id") when referenced in depends_on clause.

Originally posted by @mario-nc in https://github.com/hashicorp/vscode-terraform/issues/1592#issuecomment-1761591993

radeksimko commented 11 months ago

I was able to reproduce this behaviour and I can confirm it is a bug just because Terraform CLI/Core doesn't raise it - so there's deviation.

The aws_subnet.tgw-subnet is already implied by the aws_subnet.tgw-subnet[*].id on the second line, so that entry in depends_on is not necessary. Similarly, dependency on var.tgw_id is already implied by the third line. Therefore, the whole depends_on attribute is redundant and can be removed.

That said, I have confirmed that there are valid use cases for referencing variables in depends_on although such valid use cases are relatively limited - as is the case of depends_on in general. One potential use case could involve variable validation. Therefore we'll need to relax the validation rules there by updating the schemas.


Side note: Given how often I've seen depends_on being over-used I think there is an opportunity for the extension to detect such duplication between implicit and explicit dependencies and raise these as separate diagnostics, although more advisory than as errors.

github-actions[bot] commented 9 months ago

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.