@@ -7,8 +7,10 @@ import (
77 "context"
88
99 "github.com/hashicorp/terraform-ls/internal/document"
10+ "github.com/hashicorp/terraform-ls/internal/features/stacks/ast"
1011 "github.com/hashicorp/terraform-ls/internal/features/stacks/state"
1112 "github.com/hashicorp/terraform-ls/internal/job"
13+ globalAst "github.com/hashicorp/terraform-ls/internal/terraform/ast"
1214 "github.com/hashicorp/terraform-ls/internal/terraform/module/operation"
1315 earlydecoder "github.com/hashicorp/terraform-schema/earlydecoder/stacks"
1416)
@@ -17,15 +19,15 @@ import (
1719// way that enables us to decode the rest of the configuration,
1820// e.g. by knowing provider versions, etc.
1921func LoadStackMetadata (ctx context.Context , stackStore * state.StackStore , stackPath string ) error {
20- stack , err := stackStore .StackRecordByPath (stackPath )
22+ record , err := stackStore .StackRecordByPath (stackPath )
2123 if err != nil {
2224 return err
2325 }
2426
2527 // TODO: Avoid parsing if upstream (parsing) job reported no changes
2628
2729 // Avoid parsing if it is already in progress or already known
28- if stack .MetaState != operation .OpStateUnknown && ! job .IgnoreState (ctx ) {
30+ if record .MetaState != operation .OpStateUnknown && ! job .IgnoreState (ctx ) {
2931 return job.StateNotChangedErr {Dir : document .DirHandleFromPath (stackPath )}
3032 }
3133
@@ -34,13 +36,36 @@ func LoadStackMetadata(ctx context.Context, stackStore *state.StackStore, stackP
3436 return err
3537 }
3638
39+ meta , diags := earlydecoder .LoadStack (record .Path (), record .ParsedFiles .AsMap ())
40+
3741 var mErr error
38- meta , diags := earlydecoder . LoadStack ( stack . Path (), stack . ParsedFiles . AsMap () )
39- if len ( diags ) > 0 {
40- mErr = diags
42+ sErr := stackStore . UpdateMetadata ( stackPath , meta , mErr )
43+ if sErr != nil {
44+ return sErr
4145 }
4246
43- sErr := stackStore .UpdateMetadata (stackPath , meta , mErr )
47+ if len (diags ) <= 0 {
48+ // no new diagnostics, so return early
49+ return mErr
50+ }
51+
52+ // Merge the new diagnostics with the existing ones
53+ existingDiags , ok := record .Diagnostics [globalAst .HCLParsingSource ]
54+ if ! ok {
55+ existingDiags = make (ast.Diagnostics )
56+ } else {
57+ existingDiags = existingDiags .Copy ()
58+ }
59+
60+ for fileName , diagnostic := range diags {
61+ // Convert the filename to an AST filename
62+ fn := ast .FilenameFromName (fileName )
63+
64+ // Append the diagnostic to the existing diagnostics if it exists
65+ existingDiags [fn ] = existingDiags [fn ].Extend (diagnostic )
66+ }
67+
68+ sErr = stackStore .UpdateDiagnostics (stackPath , globalAst .HCLParsingSource , existingDiags )
4469 if sErr != nil {
4570 return sErr
4671 }
0 commit comments