Skip to content

Commit e4e739c

Browse files
authored
(TFECO-7637) Raise HCL diagnostics (#1850)
* Raise HCL diagnostics This change updates the stack metadata loading to merge the new diagnostics with the existing ones. This is necessary to ensure that we don't lose any diagnostics that were previously reported.
1 parent 6510bc9 commit e4e739c

File tree

5 files changed

+46
-9
lines changed

5 files changed

+46
-9
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: ENHANCEMENTS
2+
body: Raise HCL Diagnostics during early validation
3+
time: 2024-11-07T16:47:03.069677-05:00
4+
custom:
5+
Issue: "1500"
6+
Repository: terraform-ls

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/hashicorp/terraform-exec v0.21.0
1919
github.com/hashicorp/terraform-json v0.23.0
2020
github.com/hashicorp/terraform-registry-address v0.2.3
21-
github.com/hashicorp/terraform-schema v0.0.0-20241030161413-0438899ff948
21+
github.com/hashicorp/terraform-schema v0.0.0-20241113181710-ea3872fef6cf
2222
github.com/mcuadros/go-defaults v1.2.0
2323
github.com/mh-cbon/go-fmt-fail v0.0.0-20160815164508-67765b3fbcb5
2424
github.com/mitchellh/cli v1.1.5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ github.com/hashicorp/terraform-json v0.23.0 h1:sniCkExU4iKtTADReHzACkk8fnpQXrdD2
233233
github.com/hashicorp/terraform-json v0.23.0/go.mod h1:MHdXbBAbSg0GvzuWazEGKAn/cyNfIB7mN6y7KJN6y2c=
234234
github.com/hashicorp/terraform-registry-address v0.2.3 h1:2TAiKJ1A3MAkZlH1YI/aTVcLZRu7JseiXNRHbOAyoTI=
235235
github.com/hashicorp/terraform-registry-address v0.2.3/go.mod h1:lFHA76T8jfQteVfT7caREqguFrW3c4MFSPhZB7HHgUM=
236-
github.com/hashicorp/terraform-schema v0.0.0-20241030161413-0438899ff948 h1:2yw+7HPYOAEFafRpcw7BOtX9svWmXdc8sK5sSLZIjiI=
237-
github.com/hashicorp/terraform-schema v0.0.0-20241030161413-0438899ff948/go.mod h1:cA6LcD9EWWwgG3ZsZx+Fvyvgil6LYxTl3bknC8LF0B8=
236+
github.com/hashicorp/terraform-schema v0.0.0-20241113181710-ea3872fef6cf h1:rUFHjz0LWE7jM98hz2p5MDGtHq9oA8ZbDCY28VjcZtg=
237+
github.com/hashicorp/terraform-schema v0.0.0-20241113181710-ea3872fef6cf/go.mod h1:hwYMiQp/tVcJtYfbNSxEEK+ilauXwwtZgpLXmeUBVGg=
238238
github.com/hashicorp/terraform-svchost v0.1.1 h1:EZZimZ1GxdqFRinZ1tpJwVxxt49xc/S52uzrw4x0jKQ=
239239
github.com/hashicorp/terraform-svchost v0.1.1/go.mod h1:mNsjQfZyf/Jhz35v6/0LWcv26+X7JPS+buii2c9/ctc=
240240
github.com/hexops/autogold v1.3.1 h1:YgxF9OHWbEIUjhDbpnLhgVsjUDsiHDTyDfy2lrfdlzo=

internal/features/stacks/jobs/metadata.go

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
1921
func 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
}

internal/langserver/handlers/initialize_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,12 @@ func TestInitialize_differentWorkspaceLayouts(t *testing.T) {
567567
t.Fatal(err)
568568
}
569569
if len(allModules) != len(tc.expectedModules) {
570+
for _, mods := range tc.expectedModules {
571+
t.Logf("expected module: %s", mods)
572+
}
573+
for _, mods := range allModules {
574+
t.Logf("got module: %s", mods.Path())
575+
}
570576
t.Fatalf("expected %d modules, got %d", len(tc.expectedModules), len(allModules))
571577
}
572578
for _, path := range tc.expectedModules {

0 commit comments

Comments
 (0)