From 9acbaee63292a1a5cc8cbc4aeed2ea41bbad65aa Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Tue, 26 Mar 2024 17:06:46 +0100 Subject: [PATCH] feat: support provider defined functions (#1636) * feat: initial provider defined functions support work * chore: update hashicorp/hcl, hcl-lang, and terraform-schema to unreleased versions * fix: remove obsolete method call * Bump hcl to 2.20.1 * Bump hcl-lang to `49d7378` * Bump terraform-schema to `560df6b` --------- Co-authored-by: Daniel Banck --- go.mod | 7 +++---- go.sum | 14 ++++++-------- internal/decoder/decoder.go | 6 +++++- internal/decoder/functions.go | 29 ++++++++++------------------- 4 files changed, 24 insertions(+), 32 deletions(-) diff --git a/go.mod b/go.mod index 37bfba3aa..642164f91 100644 --- a/go.mod +++ b/go.mod @@ -13,12 +13,12 @@ require ( github.com/hashicorp/go-uuid v1.0.3 github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/hc-install v0.6.3 - github.com/hashicorp/hcl-lang v0.0.0-20240307084507-b482690b023d - github.com/hashicorp/hcl/v2 v2.19.1 + github.com/hashicorp/hcl-lang v0.0.0-20240326153306-49d737897778 + github.com/hashicorp/hcl/v2 v2.20.1 github.com/hashicorp/terraform-exec v0.20.0 github.com/hashicorp/terraform-json v0.21.0 github.com/hashicorp/terraform-registry-address v0.2.3 - github.com/hashicorp/terraform-schema v0.0.0-20240212083923-1ed5c2075222 + github.com/hashicorp/terraform-schema v0.0.0-20240326154150-560df6bbc4dc github.com/mcuadros/go-defaults v1.2.0 github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5 github.com/mitchellh/cli v1.1.5 @@ -72,7 +72,6 @@ require ( github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jinzhu/copier v0.3.5 // indirect github.com/kr/pretty v0.3.1 // indirect - github.com/kylelemons/godebug v1.1.0 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 0fb68355f..8942069c7 100644 --- a/go.sum +++ b/go.sum @@ -220,18 +220,18 @@ github.com/hashicorp/hc-install v0.6.3 h1:yE/r1yJvWbtrJ0STwScgEnCanb0U9v7zp0Gbkm github.com/hashicorp/hc-install v0.6.3/go.mod h1:KamGdbodYzlufbWh4r9NRo8y6GLHWZP2GBtdnms1Ln0= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= -github.com/hashicorp/hcl-lang v0.0.0-20240307084507-b482690b023d h1:Q7JcyMeEWWdUCM48ZwVE6pfnxUcrm7azB9a+Z7aIEbs= -github.com/hashicorp/hcl-lang v0.0.0-20240307084507-b482690b023d/go.mod h1:uCj8V85KJStf2MkhcA6WR1TIjDHwtswOwDCVsXPhPNI= -github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI= -github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE= +github.com/hashicorp/hcl-lang v0.0.0-20240326153306-49d737897778 h1:Yw/5Lno+YiOAEQQ6krRpiJ4lCv+x0k7Q6/SxH5AIsus= +github.com/hashicorp/hcl-lang v0.0.0-20240326153306-49d737897778/go.mod h1:2Vwxuf2nQi/095HYVqo0kWhlBLKhwEi/jTf5TxB61MI= +github.com/hashicorp/hcl/v2 v2.20.1 h1:M6hgdyz7HYt1UN9e61j+qKJBqR3orTWbI1HKBJEdxtc= +github.com/hashicorp/hcl/v2 v2.20.1/go.mod h1:TZDqQ4kNKCbh1iJp99FdPiUaVDDUPivbqxZulxDYqL4= github.com/hashicorp/terraform-exec v0.20.0 h1:DIZnPsqzPGuUnq6cH8jWcPunBfY+C+M8JyYF3vpnuEo= github.com/hashicorp/terraform-exec v0.20.0/go.mod h1:ckKGkJWbsNqFKV1itgMnE0hY9IYf1HoiekpuN0eWoDw= github.com/hashicorp/terraform-json v0.21.0 h1:9NQxbLNqPbEMze+S6+YluEdXgJmhQykRyRNd+zTI05U= github.com/hashicorp/terraform-json v0.21.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk= github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI= github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM= -github.com/hashicorp/terraform-schema v0.0.0-20240212083923-1ed5c2075222 h1:BE095S2PxCR65CB7XrfRBuc4uU1Lc6hW7Ht8qtf9KIg= -github.com/hashicorp/terraform-schema v0.0.0-20240212083923-1ed5c2075222/go.mod h1:ljuNMgZEQlUzS3HSdaVwkanVQDbYiNmA9UnGvQZqbio= +github.com/hashicorp/terraform-schema v0.0.0-20240326154150-560df6bbc4dc h1:d80NP3l4Y3rgtyljXeFUnoExfppUoqy2bk0fV6bBudA= +github.com/hashicorp/terraform-schema v0.0.0-20240326154150-560df6bbc4dc/go.mod h1:NuD9aPdZD2FFmT+56jkwDMAO/lX9qPPK7pkhB+umAaw= github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ= github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc= github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo= @@ -277,8 +277,6 @@ github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kylelemons/godebug v0.0.0-20170820004349-d65d576e9348/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k= -github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= -github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= diff --git a/internal/decoder/decoder.go b/internal/decoder/decoder.go index 36ffe83d0..bda330b2f 100644 --- a/internal/decoder/decoder.go +++ b/internal/decoder/decoder.go @@ -23,13 +23,17 @@ func modulePathContext(mod *state.Module, schemaReader state.SchemaReader, modRe if err != nil { return nil, err } + functions, err := functionsForModule(mod, schemaReader) + if err != nil { + return nil, err + } pathCtx := &decoder.PathContext{ Schema: schema, ReferenceOrigins: make(reference.Origins, 0), ReferenceTargets: make(reference.Targets, 0), Files: make(map[string]*hcl.File, 0), - Functions: coreFunctions(mod), + Functions: functions, Validators: moduleValidators, } diff --git a/internal/decoder/functions.go b/internal/decoder/functions.go index 7ef784a78..8c439ae68 100644 --- a/internal/decoder/functions.go +++ b/internal/decoder/functions.go @@ -7,31 +7,22 @@ import ( "github.com/hashicorp/go-version" "github.com/hashicorp/hcl-lang/schema" "github.com/hashicorp/terraform-ls/internal/state" + tfmodule "github.com/hashicorp/terraform-schema/module" tfschema "github.com/hashicorp/terraform-schema/schema" ) -func coreFunctions(mod *state.Module) map[string]schema.FunctionSignature { - if mod.TerraformVersion != nil { - s, err := tfschema.FunctionsForVersion(mod.TerraformVersion) - if err == nil { - return s - } - if mod.TerraformVersion.LessThan(tfschema.OldestAvailableVersion) { - return mustFunctionsForVersion(tfschema.OldestAvailableVersion) - } +func functionsForModule(mod *state.Module, schemaReader state.SchemaReader) (map[string]schema.FunctionSignature, error) { + resolvedVersion := tfschema.ResolveVersion(mod.TerraformVersion, mod.Meta.CoreRequirements) + sm := tfschema.NewFunctionsMerger(mustFunctionsForVersion(resolvedVersion)) + sm.SetSchemaReader(schemaReader) - return mustFunctionsForVersion(tfschema.LatestAvailableVersion) + meta := &tfmodule.Meta{ + Path: mod.Path, + ProviderRequirements: mod.Meta.ProviderRequirements, + ProviderReferences: mod.Meta.ProviderReferences, } - s, err := tfschema.FunctionsForConstraint(mod.Meta.CoreRequirements) - if err == nil { - return s - } - if mod.Meta.CoreRequirements.Check(tfschema.OldestAvailableVersion) { - return mustFunctionsForVersion(tfschema.OldestAvailableVersion) - } - - return mustFunctionsForVersion(tfschema.LatestAvailableVersion) + return sm.FunctionsForModule(meta) } func mustFunctionsForVersion(v *version.Version) map[string]schema.FunctionSignature {