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

[0.14 backport] Fix empty diags not getting associated with source. #28029

Merged
merged 2 commits into from
Mar 9, 2021
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
2 changes: 1 addition & 1 deletion plugin/convert/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func ProtoToDiagnostics(ds []*proto.Diagnostic) tfdiags.Diagnostics {
var newDiag tfdiags.Diagnostic

// if there's an attribute path, we need to create a AttributeValue diagnostic
if d.Attribute != nil {
if d.Attribute != nil && len(d.Attribute.Steps) > 0 {
path := AttributePathToPath(d.Attribute)
newDiag = tfdiags.AttributeValue(severity, d.Summary, d.Detail, path)
} else {
Expand Down
44 changes: 44 additions & 0 deletions plugin/convert/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
proto "github.com/hashicorp/terraform/internal/tfplugin5"
"github.com/hashicorp/terraform/tfdiags"
"github.com/zclconf/go-cty/cty"
Expand Down Expand Up @@ -357,3 +359,45 @@ func TestDiagnostics(t *testing.T) {
})
}
}

// Test that a diagnostic with a present but empty attribute results in a
// whole body diagnostic. We verify this by inspecting the resulting Subject
// from the diagnostic when considered in the context of a config body.
func TestProtoDiagnostics_emptyAttributePath(t *testing.T) {
protoDiags := []*proto.Diagnostic{
{
Severity: proto.Diagnostic_ERROR,
Summary: "error 1",
Detail: "error 1 detail",
Attribute: &proto.AttributePath{
Steps: []*proto.AttributePath_Step{
// this slice is intentionally left empty
},
},
},
}
tfDiags := ProtoToDiagnostics(protoDiags)

testConfig := `provider "test" {
foo = "bar"
}`
f, parseDiags := hclsyntax.ParseConfig([]byte(testConfig), "test.tf", hcl.Pos{Line: 1, Column: 1})
if parseDiags.HasErrors() {
t.Fatal(parseDiags)
}
diags := tfDiags.InConfigBody(f.Body)

if len(tfDiags) != 1 {
t.Fatalf("expected 1 diag, got %d", len(tfDiags))
}
got := diags[0].Source().Subject
want := &tfdiags.SourceRange{
Filename: "test.tf",
Start: tfdiags.SourcePos{Line: 1, Column: 1},
End: tfdiags.SourcePos{Line: 1, Column: 1},
}

if !cmp.Equal(got, want, typeComparer, valueComparer) {
t.Fatal(cmp.Diff(got, want, typeComparer, valueComparer))
}
}