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

Combine Stack and Deploy files and diagnostics #1754

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 0 additions & 85 deletions internal/features/stacks/ast/deploy.go

This file was deleted.

68 changes: 55 additions & 13 deletions internal/features/stacks/ast/stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ import (
globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast"
)

type Filename interface {
String() string
IsJSON() bool
IsIgnored() bool
}

// StackFilename is a custom type for stack configuration files
type StackFilename string
type StackFiles map[StackFilename]*hcl.File
type StackDiags map[StackFilename]hcl.Diagnostics

func (mf StackFilename) String() string {
return string(mf)
Expand All @@ -31,25 +36,62 @@ func IsStackFilename(name string) bool {
strings.HasSuffix(name, ".tfstack.json")
}

func (sf StackFiles) Copy() StackFiles {
m := make(StackFiles, len(sf))
// DeployFilename is a custom type for deployment files
type DeployFilename string

func (df DeployFilename) String() string {
return string(df)
}

func (df DeployFilename) IsJSON() bool {
return strings.HasSuffix(string(df), ".json")
}

func (df DeployFilename) IsIgnored() bool {
return globalAst.IsIgnoredFile(string(df))
}

func IsDeployFilename(name string) bool {
return strings.HasSuffix(name, ".tfdeploy.hcl") ||
strings.HasSuffix(name, ".tfdeploy.json")
}

// FilenameFromName returns either a StackFilename or DeployFilename based
// on the name
func FilenameFromName(name string) Filename {
if IsStackFilename(name) {
return StackFilename(name)
}
if IsDeployFilename(name) {
return DeployFilename(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 (sd StackDiags) Copy() StackDiags {
m := make(StackDiags, len(sd))
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 StackDiags) AutoloadedOnly() StackDiags {
diags := make(StackDiags)
func (sd Diagnostics) AutoloadedOnly() Diagnostics {
diags := make(Diagnostics)
for name, f := range sd {
if !name.IsIgnored() {
diags[name] = f
Expand All @@ -58,25 +100,25 @@ func (sd StackDiags) AutoloadedOnly() StackDiags {
return diags
}

func (sd StackDiags) AsMap() map[string]hcl.Diagnostics {
func (sd Diagnostics) AsMap() map[string]hcl.Diagnostics {
m := make(map[string]hcl.Diagnostics, len(sd))
for name, diags := range sd {
m[string(name)] = diags
m[name.String()] = diags
}
return m
}

func (sd StackDiags) Count() int {
func (sd Diagnostics) Count() int {
count := 0
for _, diags := range sd {
count += len(diags)
}
return count
}

type SourceStackDiags map[globalAst.DiagnosticSource]StackDiags
type SourceDiagnostics map[globalAst.DiagnosticSource]Diagnostics

func (svd SourceStackDiags) Count() int {
func (svd SourceDiagnostics) Count() int {
count := 0
for _, diags := range svd {
count += diags.Count()
Expand Down
28 changes: 22 additions & 6 deletions internal/features/stacks/decoder/path_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"github.com/hashicorp/hcl-lang/lang"
"github.com/hashicorp/hcl-lang/reference"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/terraform-ls/internal/features/stacks/ast"
"github.com/hashicorp/terraform-ls/internal/features/stacks/state"
ilsp "github.com/hashicorp/terraform-ls/internal/lsp"
stackschema "github.com/hashicorp/terraform-schema/schema"
Expand Down Expand Up @@ -45,7 +46,7 @@
func stackPathContext(record *state.StackRecord) (*decoder.PathContext, error) {
// TODO: get Terraform version from record and use that to get the schema
// TODO: this should only work for terraform 1.8 and above
schema, err := stackschema.CoreStackSchemaForVersion(stackschema.LatestAvailableVersion)

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (freebsd, amd64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (openbsd, 386)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (openbsd, amd64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (freebsd, 386)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (windows, 386)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (linux, amd64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (solaris, amd64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (freebsd, arm)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (windows, arm64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (linux, 386)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (windows, amd64)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (linux, arm)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: stackschema.CoreStackSchemaForVersion

Check failure on line 49 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: stackschema.CoreStackSchemaForVersion
if err != nil {
return nil, err
}
Expand All @@ -59,8 +60,10 @@

// TODO: Add reference origins and targets if needed

for name, f := range record.ParsedStackFiles {
pathCtx.Files[name.String()] = f
for name, f := range record.ParsedFiles {
if _, ok := name.(ast.StackFilename); ok {
pathCtx.Files[name.String()] = f
}
}

return pathCtx, nil
Expand All @@ -69,7 +72,7 @@
func deployPathContext(record *state.StackRecord) (*decoder.PathContext, error) {
// TODO: get Terraform version from record and use that to get the schema
// TODO: this should only work for terraform 1.8 and above
schema, err := stackschema.CoreDeploySchemaForVersion(stackschema.LatestAvailableVersion)

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (freebsd, amd64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (darwin, arm64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (openbsd, 386)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (openbsd, amd64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (freebsd, 386)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (windows, 386)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (linux, amd64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (darwin, amd64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (solaris, amd64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (freebsd, arm)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (windows, arm64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (linux, 386)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (windows, amd64)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / build (linux, arm)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

undefined: stackschema.CoreDeploySchemaForVersion

Check failure on line 75 in internal/features/stacks/decoder/path_reader.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

undefined: stackschema.CoreDeploySchemaForVersion
if err != nil {
return nil, err
}
Expand All @@ -83,8 +86,10 @@

// TODO: Add reference origins and targets if needed

for name, f := range record.ParsedDeployFiles {
pathCtx.Files[name.String()] = f
for name, f := range record.ParsedFiles {
if _, ok := name.(ast.DeployFilename); ok {
pathCtx.Files[name.String()] = f
}
}

return pathCtx, nil
Expand All @@ -99,13 +104,24 @@
}

for _, record := range stackRecords {
if len(record.ParsedStackFiles) > 0 {
foundStack := false
foundDeploy := false
for name := range record.ParsedFiles {
if _, ok := name.(ast.StackFilename); ok {
foundStack = true
}
if _, ok := name.(ast.DeployFilename); ok {
foundDeploy = true
}
}

if foundStack {
paths = append(paths, lang.Path{
Path: record.Path(),
LanguageID: ilsp.Stacks.String(),
})
}
if len(record.ParsedDeployFiles) > 0 {
if foundDeploy {
paths = append(paths, lang.Path{
Path: record.Path(),
LanguageID: ilsp.Deploy.String(),
Expand Down
82 changes: 5 additions & 77 deletions internal/features/stacks/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"path/filepath"

lsctx "github.com/hashicorp/terraform-ls/internal/context"
"github.com/hashicorp/terraform-ls/internal/document"
"github.com/hashicorp/terraform-ls/internal/features/stacks/ast"
"github.com/hashicorp/terraform-ls/internal/features/stacks/jobs"
Expand Down Expand Up @@ -56,19 +55,7 @@ func (f *StacksFeature) didOpen(ctx context.Context, dir document.DirHandle, lan
return ids, err
}

sIds, err := f.decodeStacks(ctx, dir, false, true)
if err != nil {
return ids, err
}
ids = append(ids, sIds...)

dIds, err := f.decodeDeploy(ctx, dir, false, true)
if err != nil {
return ids, err
}
ids = append(ids, dIds...)

return ids, nil
return f.decodeStack(ctx, dir, false, true)
}

func (f *StacksFeature) didChange(ctx context.Context, dir document.DirHandle) (job.IDs, error) {
Expand All @@ -77,15 +64,7 @@ func (f *StacksFeature) didChange(ctx context.Context, dir document.DirHandle) (
return job.IDs{}, nil
}

rpcContext := lsctx.DocumentContext(ctx)
switch rpcContext.LanguageID {
case lsp.Stacks.String():
return f.decodeStacks(ctx, dir, true, true)
case lsp.Deploy.String():
return f.decodeDeploy(ctx, dir, true, true)
}

return nil, nil
return f.decodeStack(ctx, dir, true, true)
}

func (f *StacksFeature) didChangeWatched(ctx context.Context, rawPath string, changeType protocol.FileChangeType, isDir bool) (job.IDs, error) {
Expand Down Expand Up @@ -138,19 +117,7 @@ func (f *StacksFeature) didChangeWatched(ctx context.Context, rawPath string, ch
return ids, nil
}

sIds, err := f.decodeStacks(ctx, dir, false, true)
if err != nil {
return ids, err
}
ids = append(ids, sIds...)

dIds, err := f.decodeDeploy(ctx, dir, false, true)
if err != nil {
return ids, err
}
ids = append(ids, dIds...)

return ids, nil
return f.decodeStack(ctx, dir, true, true)

case protocol.Changed:
fallthrough
Expand Down Expand Up @@ -178,25 +145,13 @@ func (f *StacksFeature) didChangeWatched(ctx context.Context, rawPath string, ch
return ids, nil
}

sIds, err := f.decodeStacks(ctx, dir, false, true)
if err != nil {
return ids, err
}
ids = append(ids, sIds...)

dIds, err := f.decodeDeploy(ctx, dir, false, true)
if err != nil {
return ids, err
}
ids = append(ids, dIds...)

return ids, nil
return f.decodeStack(ctx, dir, true, true)
}

return nil, nil
}

func (f *StacksFeature) decodeStacks(ctx context.Context, dir document.DirHandle, ignoreState bool, isFirstLevel bool) (job.IDs, error) {
func (f *StacksFeature) decodeStack(ctx context.Context, dir document.DirHandle, ignoreState bool, isFirstLevel bool) (job.IDs, error) {
ids := make(job.IDs, 0)
path := dir.Path()

Expand All @@ -223,33 +178,6 @@ func (f *StacksFeature) decodeStacks(ctx context.Context, dir document.DirHandle
return ids, nil
}

func (f *StacksFeature) decodeDeploy(ctx context.Context, dir document.DirHandle, ignoreState bool, isFirstLevel bool) (job.IDs, error) {
ids := make(job.IDs, 0)
path := dir.Path()

parseId, err := f.stateStore.JobStore.EnqueueJob(ctx, job.Job{
Dir: dir,
Func: func(ctx context.Context) error {
return jobs.ParseDeployConfiguration(ctx, f.fs, f.store, path)
},
Type: operation.OpTypeParseDeployConfiguration.String(),
IgnoreState: ignoreState,
})
if err != nil {
return ids, err
}
ids = append(ids, parseId)

// TODO: Implement the following functions where appropriate to stacks
// Future: LoadModuleMetadata(ctx, f.Store, path)
// Future: decodeDeclaredModuleCalls(ctx, dir, ignoreState)
// TODO: PreloadEmbeddedSchema(ctx, f.logger, schemas.FS,
// Future: DecodeReferenceTargets(ctx, f.Store, f.rootFeature, path)
// Future: DecodeReferenceOrigins(ctx, f.Store, f.rootFeature, path)

return ids, nil
}

func (f *StacksFeature) removeIndexedStack(rawPath string) {
stackHandle := document.DirHandleFromPath(rawPath)

Expand Down
Loading
Loading