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

Solidify handling of optional/default object attributes #917

Open
1 of 13 tasks
radeksimko opened this issue May 9, 2022 · 2 comments
Open
1 of 13 tasks

Solidify handling of optional/default object attributes #917

radeksimko opened this issue May 9, 2022 · 2 comments
Assignees

Comments

@radeksimko
Copy link
Member

radeksimko commented May 9, 2022

Background - TF Core

Support of optional and default object attributes has been developed through various experiments over time. We currently don't reflect any of these in IntelliSense.

Terraform 0.14 (optional types)

hashicorp/terraform#26540

variable "with_optional_attribute" {
  type = object({
    a = string           # a required attribute
    b = optional(string) # an optional attribute
  })
}

Terraform 0.15 (default values)

hashicorp/terraform#26766

output "with_defaults" {
  value = defaults(var.with_optional_attribute, object({
    a = "foo"
    b = "bar"
  }))
}

Terraform 1.3 (optional type with defaults)

hashicorp/terraform#31154

variable "with_optional_attribute" {
  type = object({
    a = string                # a required attribute
    b = optional(string)      # an optional attribute
    c = optional(number, 127) # an optional attribute with a default value
  })
}

The Core team now considers this implementation/syntax to be stable and doesn't have more immediate plans for changing it.

Background - LS

We currently only reflect optional types as present in Terraform v0.14 via the typeexpr package was copied over from TF Core as part of the earlydecoder. This allows us to decode variable types, such that we can then use it in IntelliSense for module inputs.

2022-09-02 11 46 53

That IntelliSense however only enables us to identify object, not the fact that it has an optional attribute - all attributes are treated/reported as required currently.

Implementation Notes

earlydecoder / metadata

It seems impractical to distinguish between the different implementations between TF versions in the context of earlydecoder which is generally designed to run early and handle configuration of any TF version without knowing what that version is. We can take advantage of the final v1.3+ backwards-compatible implementation there as upstreamed into HCL v2.14.0.

IntelliSense

If possible, IntelliSense we provide should reflect the Terraform version in use. In practice this means that:

  • in < v0.14 configs: No optional attributes are recognised in completion, hover, or semantic highlighting
  • in v0.14.x configs: optional(type) with single attribute is recognised in variable type completion, hover and semantic highlighting
  • in v0.15+ configs: function defaults() is recognized for completion, hover and semantic highlighting
  • in v1.3+ configs: optional(type, default_val) with two attributes is recognised in variable type completion, hover and semantic highlighting

Proposal

terraform-schema

hcl-lang / terraform-ls

  • (implied) Recognize individual expressions when nested #496
  • Expose a flag on the decoder.Decoder to enable "legacy" optional types (single-argument optional())
    • terraform-ls: Enable the "legacy" mode on Terraform v0.14+ with relevant experiment enabled only
  • Expose a flag on the decoder.Decoder to enable "modern" optional types (with two-argument optional())
    • terraform-ls: Enable the "modern" mode on Terraform 1.3+ only
  • Provide completion for optional() in variable type
  • Provide hover for optional() in variable type
  • Provide semantic highlighting for optional() in variable type
  • (implied) Support built-in functions #37 & Provide function signature help #857
  • Provide completion for defaults() function in v0.15+
  • Provide hover for defaults() function in v0.15+
  • Provide semantic highlighting for defaults() function in v0.15+
@radeksimko radeksimko changed the title Solidify handling of optional object attributes Solidify handling of optional/default object attributes Sep 2, 2022
@xiehan xiehan added this to the v0.31.0 milestone Oct 27, 2022
@jpogran
Copy link
Contributor

jpogran commented Dec 2, 2022

If possible, IntelliSense we provide should reflect the Terraform version in use

Since there are many methods for having a different Terraform version in any given scenario, how does a user know that the completions they are/are not getting is due to the Terraform version in use?

@radeksimko
Copy link
Member Author

@jpogran That's a great question - I think the best answer would be surfacing the detected Terraform version and constraint somewhere in the status bar?
hashicorp/vscode-terraform#697

I'm open to other ideas though.

You have already prototyped what I think is related in hashicorp/vscode-terraform#1190

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

4 participants