Skip to content

Commit

Permalink
fix: avoid sending empty diagnostics (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
radeksimko committed Jan 7, 2022
1 parent 1b06ad4 commit 9316e07
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
13 changes: 6 additions & 7 deletions internal/langserver/handlers/hooks_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,24 +96,23 @@ func sendModuleTelemetry(ctx context.Context, store *state.StateStore, telemetry

func updateDiagnostics(ctx context.Context, notifier *diagnostics.Notifier) state.ModuleChangeHook {
return func(oldMod, newMod *state.Module) {
// TODO: check if diagnostics have actually changed
oldDiags, newDiags := 0, 0
if oldMod != nil {
oldDiags = len(oldMod.ModuleDiagnostics) + len(oldMod.VarsDiagnostics)
oldDiags = oldMod.ModuleDiagnostics.Count() + oldMod.VarsDiagnostics.Count()
}
if newMod != nil {
newDiags = len(newMod.ModuleDiagnostics) + len(newMod.VarsDiagnostics)
newDiags = newMod.ModuleDiagnostics.Count() + newMod.VarsDiagnostics.Count()
}

if oldDiags == 0 && newDiags == 0 {
return
}

diags := diagnostics.NewDiagnostics()
diags.EmptyRootDiagnostic()

defer notifier.PublishHCLDiags(ctx, newMod.Path, diags)

if oldDiags == 0 && newDiags == 0 {
return
}

if newMod != nil {
diags.Append("HCL", newMod.ModuleDiagnostics.AsMap())
diags.Append("HCL", newMod.VarsDiagnostics.AutoloadedOnly().AsMap())
Expand Down
8 changes: 8 additions & 0 deletions internal/terraform/ast/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ func (md ModDiags) AsMap() map[string]hcl.Diagnostics {
}
return m
}

func (md ModDiags) Count() int {
count := 0
for _, diags := range md {
count += len(diags)
}
return count
}
8 changes: 8 additions & 0 deletions internal/terraform/ast/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,11 @@ func (vd VarsDiags) AsMap() map[string]hcl.Diagnostics {
}
return m
}

func (vd VarsDiags) Count() int {
count := 0
for _, diags := range vd {
count += len(diags)
}
return count
}

0 comments on commit 9316e07

Please sign in to comment.