From 8e8ee581c66c83d36792c4c9680daa17fa3155aa Mon Sep 17 00:00:00 2001 From: Ansgar Mertens Date: Thu, 7 Nov 2024 14:45:51 +0100 Subject: [PATCH] (TFECO-7942) builtin refs from tf schema (#1854) * source version specific built-in references from terraform-schema * add changie entry --- .../ENHANCEMENTS-20241029-161707.yaml | 6 ++ .../modules/decoder/module_context.go | 3 + .../features/modules/decoder/references.go | 17 ++++++ .../modules/jobs/builtin_references.go | 57 ------------------- internal/features/modules/jobs/references.go | 2 - 5 files changed, 26 insertions(+), 59 deletions(-) create mode 100644 .changes/unreleased/ENHANCEMENTS-20241029-161707.yaml create mode 100644 internal/features/modules/decoder/references.go delete mode 100644 internal/features/modules/jobs/builtin_references.go diff --git a/.changes/unreleased/ENHANCEMENTS-20241029-161707.yaml b/.changes/unreleased/ENHANCEMENTS-20241029-161707.yaml new file mode 100644 index 000000000..edd63ace7 --- /dev/null +++ b/.changes/unreleased/ENHANCEMENTS-20241029-161707.yaml @@ -0,0 +1,6 @@ +kind: ENHANCEMENTS +body: Support terraform.applying built-in reference starting at TF 1.10 +time: 2024-10-29T16:17:07.267245+01:00 +custom: + Issue: "1854" + Repository: terraform-ls diff --git a/internal/features/modules/decoder/module_context.go b/internal/features/modules/decoder/module_context.go index eab9f1db1..07d6effb3 100644 --- a/internal/features/modules/decoder/module_context.go +++ b/internal/features/modules/decoder/module_context.go @@ -43,6 +43,9 @@ func modulePathContext(mod *state.ModuleRecord, stateReader CombinedReader) (*de } } + // append Terraform version specific path targets that are available in all modules (builtin references) + pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, referencesForModule(mod, stateReader)...) + for name, f := range mod.ParsedModuleFiles { pathCtx.Files[name.String()] = f } diff --git a/internal/features/modules/decoder/references.go b/internal/features/modules/decoder/references.go new file mode 100644 index 000000000..5dcb2eece --- /dev/null +++ b/internal/features/modules/decoder/references.go @@ -0,0 +1,17 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package decoder + +import ( + "github.com/hashicorp/hcl-lang/reference" + "github.com/hashicorp/terraform-ls/internal/features/modules/state" + tfschema "github.com/hashicorp/terraform-schema/schema" +) + +func referencesForModule(mod *state.ModuleRecord, stateReader CombinedReader) reference.Targets { + modPath := mod.Path() + resolvedVersion := tfschema.ResolveVersion(stateReader.TerraformVersion(modPath), mod.Meta.CoreRequirements) + + return tfschema.BuiltinReferencesForVersion(resolvedVersion, modPath) +} diff --git a/internal/features/modules/jobs/builtin_references.go b/internal/features/modules/jobs/builtin_references.go deleted file mode 100644 index 32b71f080..000000000 --- a/internal/features/modules/jobs/builtin_references.go +++ /dev/null @@ -1,57 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package jobs - -import ( - "github.com/hashicorp/hcl-lang/lang" - "github.com/hashicorp/hcl-lang/reference" - "github.com/zclconf/go-cty/cty" -) - -var builtinScopeId = lang.ScopeId("builtin") - -func builtinReferences(modPath string) reference.Targets { - return reference.Targets{ - { - Addr: lang.Address{ - lang.RootStep{Name: "path"}, - lang.AttrStep{Name: "module"}, - }, - ScopeId: builtinScopeId, - Type: cty.String, - Description: lang.Markdown("The filesystem path of the module where the expression is placed\n\n" + - modPath), - }, - { - Addr: lang.Address{ - lang.RootStep{Name: "path"}, - lang.AttrStep{Name: "root"}, - }, - ScopeId: builtinScopeId, - Type: cty.String, - Description: lang.Markdown("The filesystem path of the root module of the configuration"), - }, - { - Addr: lang.Address{ - lang.RootStep{Name: "path"}, - lang.AttrStep{Name: "cwd"}, - }, - ScopeId: builtinScopeId, - Type: cty.String, - Description: lang.Markdown("The filesystem path of the current working directory.\n\n" + - "In normal use of Terraform this is the same as `path.root`, " + - "but some advanced uses of Terraform run it from a directory " + - "other than the root module directory, causing these paths to be different."), - }, - { - Addr: lang.Address{ - lang.RootStep{Name: "terraform"}, - lang.AttrStep{Name: "workspace"}, - }, - ScopeId: builtinScopeId, - Type: cty.String, - Description: lang.Markdown("The name of the currently selected workspace"), - }, - } -} diff --git a/internal/features/modules/jobs/references.go b/internal/features/modules/jobs/references.go index 41a7a4560..94c7c9477 100644 --- a/internal/features/modules/jobs/references.go +++ b/internal/features/modules/jobs/references.go @@ -58,8 +58,6 @@ func DecodeReferenceTargets(ctx context.Context, modStore *state.ModuleStore, ro } targets, rErr := pd.CollectReferenceTargets() - targets = append(targets, builtinReferences(modPath)...) - sErr := modStore.UpdateReferenceTargets(modPath, targets, rErr) if sErr != nil { return sErr