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

Recognize expressions fully in JSON configs #675

Closed
radeksimko opened this issue Oct 12, 2021 · 2 comments
Closed

Recognize expressions fully in JSON configs #675

radeksimko opened this issue Oct 12, 2021 · 2 comments
Labels
enhancement New feature or request

Comments

@radeksimko
Copy link
Member

radeksimko commented Oct 12, 2021

Use-cases

Validity of References

When collecting reference addresses (traversals) throughout a module, we also collect "constraints" based on where these references appear. This allows us to tell the difference between a scope-only and a type-aware reference, i.e. depends_on = [aws_instance.example] and output "example" { value = aws_instance.example }.

This allows us to make accurate decisions about what is or isn't a valid reference in any particular context.

For example this means we can tell that this is not a valid reference:

variable "example" {
  type = list(string)
}

resource "aws_vpc" "example" {
  cidr_block = var.example
}

but this is

variable "example" {
  type = list(string)
}

resource "aws_vpc" "example" {
  cidr_block = one(var.example)
}

In JSON however expressions are not "walkable" in the same way as plain HCL expressions (hcl.Expression)
https://github.com/hashicorp/hcl/blob/e84201c45df4fce4e9dfaba9e8aaa8730d24dd25/json/structure.go#L23-L27

We therefore cannot tell where exactly does a reference appear in the expression (e.g. whether it's wrapped in a function call or any other expression). Therefore it is impossible to accurately evaluate valid references.

Addressable attributes of complex types

In both HCL and JSON configs there can be situations where values nested within complex expression types (such as object, list, set, or map) are addressable.

Fox example in HCL:

obj_attribute = {
  inner_attr = "foobar"
}

Here the foobar string is addressable as obj_attribute.inner_attr. It is possible to assign an address to that inner attribute because in HCL we can walk through the hclsyntax.Expression and detect *hclsyntax.TupleConsExpr. In JSON all expression types are unexported, so this is not possible.

So we cannot assign address to the same value in a comparable JSON config as shown below.

{
  "obj_attribute": {
    "inner_attr": "foobar"
  }
}

The example illustrates just object, but exact same problem applies also to all other complex types.

Attempted Solutions

We collect references in JSON without any constraints, which can overestimate the number of references to a particular block or attribute.

We also don't collect references to any data within attributes of complex types in JSON configs.

Proposal

TODO

@radeksimko radeksimko added enhancement New feature or request upstream/hcl labels Oct 12, 2021
@radeksimko radeksimko changed the title Recognize constraints of JSON references Recognize references accurately in JSON configs Oct 12, 2021
@radeksimko radeksimko changed the title Recognize references accurately in JSON configs Recognize expressions fully in JSON configs Oct 13, 2021
@radeksimko
Copy link
Member Author

We now collect targets and origins in JSON, as a result of various recent PRs (#1237, hashicorp/hcl-lang#232, hashicorp/hcl-lang#203, hashicorp/hcl-lang#199, hashicorp/hcl-lang#186, hashicorp/hcl-lang#185, hashicorp/hcl-lang#184) which are all part of the latest stable release v0.31.0.

We are also tracking a follow-up issue which concerns origin constraints in hashicorp/hcl-lang#269

Example UX Before

Screenshot 2023-04-25 at 16 34 02

Screenshot 2023-04-25 at 16 35 13

Example UX After

Screenshot 2023-04-25 at 16 34 32

Screenshot 2023-04-25 at 16 34 47

@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 May 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant