-
Notifications
You must be signed in to change notification settings - Fork 140
Support for Terraform Search (.tfquery.hcl) files #2007
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
6a8306a
feat(tfsearch): TF-26847 TF-27074: Added: init search feature
anubhav-goel cfb1a68
feat(tfsearch): TF-26847 TF-27074: Modified: renamed file name
anubhav-goel 5aaf299
feat(tfsearch): TF-26847 TF-27288: Added: dynamic schema for provider…
anubhav-goel b23cc59
Update metadata to update provider references correctly.
sunnyhashi 4b97681
feat(tfsearch): TF-26847: Modified: terraform-json version to pseudo…
anubhav-goel 892a892
feat(tfsearch): TF-26847: Modified: terraform-schema version to pseu…
anubhav-goel e87b916
feat(tfsearch): TF-26847: Added: changelog
anubhav-goel 7a5f481
feat(tfsearch): TF-26847: Modified: terrform-schema version
anubhav-goel 6091883
feat(tfsearch): TF-26847: Modified: terrform-schema version
anubhav-goel 2ed7e57
feat(tfsearch): TF-26847: Modified: added terraform version in meta
anubhav-goel 25fb048
feat(tfsearch): TF-26847: Modified: removed unused code
anubhav-goel 631b6f8
feat(tfsearch): TF-26847: Modified: added test cases
anubhav-goel dbbe7ad
feat(tfsearch): TF-26847: Refactored: removed unused code
anubhav-goel 2f2fc3b
feature/tfsearch-TF-26847: fix: Remove Deploy checks from search config
sunnyhashi d55cea6
feat(tfsearch): TF-26847 TF-27270: Modified: docs md files
anubhav-goel faa79d4
feat(tfsearch): TF-26847: Bumped: terraform-json
anubhav-goel c1652c2
feat(tfsearch): TF-26847: Bumped: terraform-schema
anubhav-goel 48088c2
Merge branch 'main' into feature/tfsearch-TF-26847
anubhav-goel b46ceb2
feat(tfsearch): TF-26847: Fixed: typo in tfstack file regex
anubhav-goel d486d69
feat(tfsearch): TF-26847: Added: ReferenceValidation
anubhav-goel 0ce0840
feat(tfsearch): TF-26847: Added: MissingRequiredAttribute
anubhav-goel a229751
feat(tfsearch): TF-26847: Refactored: renamed function name
anubhav-goel 0c3ec81
feat(tfsearch): TF-26847: Bumped: terraform-schema version
anubhav-goel 2fb0ba9
feat(tfsearch): TF-26847: Fixed: test case
anubhav-goel ff0d2c1
feat(tfsearch): TF-26847: Modified: removed corerequirements and prov…
anubhav-goel 2fc1a77
feat(tfsearch): TF-26847: Bumped: terraform-schema
anubhav-goel 2af80b4
feat(tfsearch): TF-26847: Bumped: terraform-schema
anubhav-goel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: ENHANCEMENTS | ||
body: Add support for Terraform Search files. This provides block and attribute completion, hover, and diagnostics along with syntax validation for Terraform Search files. | ||
time: 2025-08-06T10:44:56.893693+05:30 | ||
custom: | ||
Issue: "2007" | ||
Repository: terraform-ls |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package ast | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/hashicorp/hcl/v2" | ||
globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast" | ||
) | ||
|
||
type Filename interface { | ||
String() string | ||
IsJSON() bool | ||
IsIgnored() bool | ||
} | ||
|
||
// SearchFilename is a custom type for search configuration files | ||
type SearchFilename string | ||
|
||
func (mf SearchFilename) String() string { | ||
return string(mf) | ||
} | ||
|
||
func (mf SearchFilename) IsJSON() bool { | ||
return strings.HasSuffix(string(mf), ".json") | ||
} | ||
|
||
func (mf SearchFilename) IsIgnored() bool { | ||
return globalAst.IsIgnoredFile(string(mf)) | ||
} | ||
|
||
func IsSearchFilename(name string) bool { | ||
return strings.HasSuffix(name, ".tfquery.hcl") || | ||
strings.HasSuffix(name, ".tfquery.json") | ||
} | ||
|
||
// FilenameFromName returns a valid SearchFilename | ||
func FilenameFromName(name string) Filename { | ||
if IsSearchFilename(name) { | ||
return SearchFilename(name) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type Files map[Filename]*hcl.File | ||
|
||
func (sf Files) Copy() Files { | ||
m := make(Files, len(sf)) | ||
for name, file := range sf { | ||
m[name] = file | ||
} | ||
return m | ||
} | ||
|
||
func (mf Files) AsMap() map[string]*hcl.File { | ||
m := make(map[string]*hcl.File, len(mf)) | ||
for name, file := range mf { | ||
m[name.String()] = file | ||
} | ||
return m | ||
} | ||
|
||
type Diagnostics map[Filename]hcl.Diagnostics | ||
|
||
func (sd Diagnostics) Copy() Diagnostics { | ||
m := make(Diagnostics, len(sd)) | ||
for name, diags := range sd { | ||
m[name] = diags | ||
} | ||
return m | ||
} | ||
|
||
// AutoloadedOnly returns only diagnostics that are not from ignored files | ||
func (sd Diagnostics) AutoloadedOnly() Diagnostics { | ||
diags := make(Diagnostics) | ||
for name, f := range sd { | ||
if !name.IsIgnored() { | ||
diags[name] = f | ||
} | ||
} | ||
return diags | ||
} | ||
|
||
func (sd Diagnostics) AsMap() map[string]hcl.Diagnostics { | ||
m := make(map[string]hcl.Diagnostics, len(sd)) | ||
for name, diags := range sd { | ||
m[name.String()] = diags | ||
} | ||
return m | ||
} | ||
|
||
func (sd Diagnostics) Count() int { | ||
count := 0 | ||
for _, diags := range sd { | ||
count += len(diags) | ||
} | ||
return count | ||
} | ||
|
||
func DiagnosticsFromMap(m map[string]hcl.Diagnostics) Diagnostics { | ||
mf := make(Diagnostics, len(m)) | ||
for name, file := range m { | ||
mf[FilenameFromName(name)] = file | ||
} | ||
return mf | ||
} | ||
|
||
type SourceDiagnostics map[globalAst.DiagnosticSource]Diagnostics | ||
|
||
func (svd SourceDiagnostics) Count() int { | ||
count := 0 | ||
for _, diags := range svd { | ||
count += diags.Count() | ||
} | ||
return count | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package decoder | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/go-version" | ||
"github.com/hashicorp/hcl-lang/decoder" | ||
"github.com/hashicorp/hcl-lang/lang" | ||
"github.com/hashicorp/hcl-lang/reference" | ||
"github.com/hashicorp/hcl-lang/schema" | ||
"github.com/hashicorp/hcl/v2" | ||
"github.com/hashicorp/terraform-ls/internal/features/search/ast" | ||
"github.com/hashicorp/terraform-ls/internal/features/search/state" | ||
ilsp "github.com/hashicorp/terraform-ls/internal/lsp" | ||
tfaddr "github.com/hashicorp/terraform-registry-address" | ||
tfmod "github.com/hashicorp/terraform-schema/module" | ||
tfschema "github.com/hashicorp/terraform-schema/schema" | ||
searchSchema "github.com/hashicorp/terraform-schema/schema/search" | ||
tfsearch "github.com/hashicorp/terraform-schema/search" | ||
) | ||
|
||
type PathReader struct { | ||
StateReader StateReader | ||
ModuleReader ModuleReader | ||
RootReader RootReader | ||
} | ||
|
||
var _ decoder.PathReader = &PathReader{} | ||
|
||
type CombinedReader struct { | ||
ModuleReader | ||
StateReader | ||
RootReader | ||
} | ||
|
||
type StateReader interface { | ||
List() ([]*state.SearchRecord, error) | ||
SearchRecordByPath(modPath string) (*state.SearchRecord, error) | ||
ProviderSchema(modPath string, addr tfaddr.Provider, vc version.Constraints) (*tfschema.ProviderSchema, error) | ||
} | ||
|
||
type ModuleReader interface { | ||
// LocalModuleMeta returns the module meta data for a local module. This is the result | ||
// of the [earlydecoder] when processing module files | ||
LocalModuleMeta(modPath string) (*tfmod.Meta, error) | ||
} | ||
|
||
type RootReader interface { | ||
InstalledModulePath(rootPath string, normalizedSource string) (string, bool) | ||
|
||
TerraformVersion(modPath string) *version.Version | ||
} | ||
|
||
// PathContext returns a PathContext for the given path based on the language ID | ||
func (pr *PathReader) PathContext(path lang.Path) (*decoder.PathContext, error) { | ||
record, err := pr.StateReader.SearchRecordByPath(path.Path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
switch path.LanguageID { | ||
case ilsp.Search.String(): | ||
return searchPathContext(record, CombinedReader{ | ||
StateReader: pr.StateReader, | ||
ModuleReader: pr.ModuleReader, | ||
RootReader: pr.RootReader, | ||
}) | ||
} | ||
|
||
return nil, fmt.Errorf("unknown language ID: %q", path.LanguageID) | ||
} | ||
|
||
func searchPathContext(record *state.SearchRecord, stateReader CombinedReader) (*decoder.PathContext, error) { | ||
resolvedVersion := tfschema.ResolveVersion(stateReader.TerraformVersion(record.Path()), record.Meta.CoreRequirements) | ||
anubhav-goel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
sm := searchSchema.NewSearchSchemaMerger(mustCoreSchemaForVersion(resolvedVersion)) | ||
sm.SetStateReader(stateReader) | ||
|
||
meta := &tfsearch.Meta{ | ||
Path: record.Path(), | ||
CoreRequirements: record.Meta.CoreRequirements, | ||
Lists: record.Meta.Lists, | ||
Variables: record.Meta.Variables, | ||
Filenames: record.Meta.Filenames, | ||
ProviderReferences: record.Meta.ProviderReferences, | ||
ProviderRequirements: record.Meta.ProviderRequirements, | ||
} | ||
|
||
mergedSchema, err := sm.SchemaForSearch(meta) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
pathCtx := &decoder.PathContext{ | ||
Schema: mergedSchema, | ||
ReferenceOrigins: make(reference.Origins, 0), | ||
ReferenceTargets: make(reference.Targets, 0), | ||
Files: make(map[string]*hcl.File, 0), | ||
Validators: searchValidators, | ||
} | ||
|
||
// TODO: Add reference origins and targets if needed | ||
for _, origin := range record.RefOrigins { | ||
if ast.IsSearchFilename(origin.OriginRange().Filename) { | ||
pathCtx.ReferenceOrigins = append(pathCtx.ReferenceOrigins, origin) | ||
} | ||
} | ||
|
||
for _, target := range record.RefTargets { | ||
if target.RangePtr != nil && ast.IsSearchFilename(target.RangePtr.Filename) { | ||
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target) | ||
} else if target.RangePtr == nil { | ||
pathCtx.ReferenceTargets = append(pathCtx.ReferenceTargets, target) | ||
} | ||
} | ||
|
||
for name, f := range record.ParsedFiles { | ||
if _, ok := name.(ast.SearchFilename); ok { | ||
pathCtx.Files[name.String()] = f | ||
} | ||
} | ||
|
||
return pathCtx, nil | ||
} | ||
|
||
func (pr *PathReader) Paths(ctx context.Context) []lang.Path { | ||
paths := make([]lang.Path, 0) | ||
|
||
searchRecords, err := pr.StateReader.List() | ||
if err != nil { | ||
return paths | ||
} | ||
|
||
for _, record := range searchRecords { | ||
foundSearch := false | ||
for name := range record.ParsedFiles { | ||
if _, ok := name.(ast.SearchFilename); ok { | ||
foundSearch = true | ||
} | ||
|
||
} | ||
|
||
if foundSearch { | ||
paths = append(paths, lang.Path{ | ||
Path: record.Path(), | ||
LanguageID: ilsp.Search.String(), | ||
}) | ||
} | ||
|
||
} | ||
|
||
return paths | ||
} | ||
|
||
func mustCoreSchemaForVersion(v *version.Version) *schema.BodySchema { | ||
s, err := searchSchema.CoreSearchSchemaForVersion(v) | ||
if err != nil { | ||
// this should never happen | ||
panic(err) | ||
} | ||
return s | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package decoder | ||
|
||
import ( | ||
"github.com/hashicorp/hcl-lang/validator" | ||
) | ||
|
||
var searchValidators = []validator.Validator{ | ||
validator.BlockLabelsLength{}, | ||
validator.DeprecatedAttribute{}, | ||
validator.DeprecatedBlock{}, | ||
validator.MaxBlocks{}, | ||
validator.MinBlocks{}, | ||
validator.MissingRequiredAttribute{}, | ||
validator.UnexpectedAttribute{}, | ||
validator.UnexpectedBlock{}, | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.