Skip to content

Commit 9a422a5

Browse files
committed
Don't send back empty AttributePaths.
When returning Diagnostics, only populate the AttributePath if there are actually steps. Terraform core has a bug (see hashicorp/terraform#27710) that will result in any Diagnostic that has an AttributePath with no steps not being associated with a specific part of the config. Fixes #561.
1 parent 5b5e31e commit 9a422a5

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

helper/schema/grpc_provider.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strconv"
99
"sync"
1010

11-
"github.com/davecgh/go-spew/spew"
1211
"github.com/hashicorp/go-cty/cty"
1312
ctyconvert "github.com/hashicorp/go-cty/cty/convert"
1413
"github.com/hashicorp/go-cty/cty/msgpack"
@@ -1106,7 +1105,6 @@ func (s *GRPCProviderServer) ReadDataSource(ctx context.Context, req *tfprotov5.
11061105
newInstanceState, diags := res.ReadDataApply(ctx, diff, s.provider.Meta())
11071106
resp.Diagnostics = convert.AppendProtoDiag(resp.Diagnostics, diags)
11081107
if diags.HasError() {
1109-
log.Printf("[DEBUG] paddy561 got response: %s", spew.Sdump(resp))
11101108
return resp, nil
11111109
}
11121110

@@ -1154,6 +1152,9 @@ func pathToAttributePath(path cty.Path) *tftypes.AttributePath {
11541152
}
11551153
}
11561154

1155+
if len(steps) < 1 {
1156+
return nil
1157+
}
11571158
return &tftypes.AttributePath{Steps: steps}
11581159
}
11591160

helper/schema/grpc_provider_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,3 +1753,10 @@ func TestStopContext_stopReset(t *testing.T) {
17531753
t.Fatal("Stop message did not cancel request context")
17541754
}
17551755
}
1756+
1757+
func Test_pathToAttributePath_noSteps(t *testing.T) {
1758+
res := pathToAttributePath(cty.Path{})
1759+
if res != nil {
1760+
t.Errorf("Expected nil attribute path, got %+v", res)
1761+
}
1762+
}

internal/plugin/convert/diagnostics.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ func AttributePathToPath(ap *tftypes.AttributePath) cty.Path {
107107

108108
// PathToAttributePath takes a cty.Path and converts it to a proto-encoded path.
109109
func PathToAttributePath(p cty.Path) *tftypes.AttributePath {
110+
if p == nil || len(p) < 1 {
111+
return nil
112+
}
110113
ap := &tftypes.AttributePath{}
111114
for _, step := range p {
112115
switch selector := step.(type) {

internal/plugin/convert/diagnostics_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,3 +281,23 @@ func TestDiagnostics(t *testing.T) {
281281
})
282282
}
283283
}
284+
285+
func TestPathToAttributePath(t *testing.T) {
286+
tests := map[string]struct {
287+
path cty.Path
288+
want *tftypes.AttributePath
289+
}{
290+
"no steps": {
291+
path: cty.Path{},
292+
want: nil,
293+
},
294+
}
295+
for name, tc := range tests {
296+
t.Run(name, func(t *testing.T) {
297+
got := PathToAttributePath(tc.path)
298+
if diff := cmp.Diff(got, tc.want); diff != "" {
299+
t.Errorf("Unexpected diff: %s", diff)
300+
}
301+
})
302+
}
303+
}

0 commit comments

Comments
 (0)