hashicorp / terraform-config-inspect

A helper library for shallow inspection of Terraform configurations
Mozilla Public License 2.0
376 stars 76 forks source link

Expose lower-level LoadModuleFromFile function #53

Closed radeksimko closed 3 years ago

radeksimko commented 3 years ago

The diff may be confusing, but there is just a few real changes being introduced:

https://github.com/hashicorp/terraform-config-inspect/blob/c481b8bfa41ea9dca417c2a8a98fd21bd0399e14/tfconfig/load_hcl.go#L125-L139


The use case I have in mind for this is the Terraform language server, where we would benefit from not having to go through the tokenization and parsing twice. I assume that other callers like Terraform core might benefit too though as this library is used there as an "early config parser/decoder" and the same config would then be parsed again by a more detailed/precise decoder with more detailed schema AFAIK?

I originally wanted to propose something like

LoadModuleFromBody(mergedBodies hcl.Body) (*Module, hcl.Diagnostics)

which would probably be cleaner, but the piece of code related to the error handling which is looking up more context in byte slice sadly makes that much more difficult and I didn't feel like it's a good idea to just remove it.

Another alternative interface is also this

LoadModuleFromFiles(dir string, files []*hcl.File) (*Module, hcl.Diagnostics)

but I'm unsure whether that's significantly better 🤔

radeksimko commented 3 years ago

Added comments, thanks for the review.