Skip to content

Commit a46d263

Browse files
authored
internal/fwserver: Prevent PlanResourceChange panic during changed path logging (#797)
Reference: #783 Prior to the logic update: ``` --- FAIL: TestServerPlanResourceChange (0.01s) --- FAIL: TestServerPlanResourceChange/update-set-default-values (0.00s) panic: runtime error: invalid memory address or nil pointer dereference [recovered] panic: runtime error: invalid memory address or nil pointer dereference [signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1043b0484] goroutine 32 [running]: testing.tRunner.func1.2({0x1048d4e60, 0x104c08f90}) /opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1526 +0x1c8 testing.tRunner.func1() /opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1529 +0x384 panic({0x1048d4e60, 0x104c08f90}) /opt/homebrew/Cellar/go/1.20.5/libexec/src/runtime/panic.go:884 +0x204 github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).PlanResourceChange(0x140001f4a00, {0x10494ddb0, 0x14000098008}, 0x140001eeeb0, 0x14000592540) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange.go:219 +0x19e4 github.com/hashicorp/terraform-plugin-framework/internal/fwserver_test.TestServerPlanResourceChange.func37(0x14000231860) /Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:5849 +0x70 testing.tRunner(0x14000231860, 0x140000ccde0) /opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1576 +0x10c created by testing.(*T).Run /opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1629 +0x368 FAIL github.com/hashicorp/terraform-plugin-framework/internal/fwserver 0.457s ``` This change verifies and handles the more critical bug fix of the panic report. The handling of defaults for set nested attributes will require further logic updates, which will be handled in a future change.
1 parent e036d9f commit a46d263

File tree

4 files changed

+2634
-482
lines changed

4 files changed

+2634
-482
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'resource: Prevented panic during planning caused by `SetNestedAttribute` with
3+
nested attribute `Default` and multiple configured elements'
4+
time: 2023-07-11T16:17:47.990142-04:00
5+
custom:
6+
Issue: "783"

internal/fwschemadata/data_get_at_path_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,40 @@ func TestDataGetAtPath(t *testing.T) {
3434
expected any
3535
expectedDiags diag.Diagnostics
3636
}{
37+
"invalid-path": {
38+
data: fwschemadata.Data{
39+
Schema: testschema.Schema{
40+
Attributes: map[string]fwschema.Attribute{
41+
"test": testschema.Attribute{
42+
Optional: true,
43+
Type: types.StringType,
44+
},
45+
},
46+
},
47+
TerraformValue: tftypes.NewValue(
48+
tftypes.Object{
49+
AttributeTypes: map[string]tftypes.Type{
50+
"test": tftypes.String,
51+
},
52+
},
53+
map[string]tftypes.Value{
54+
"test": tftypes.NewValue(tftypes.String, "test-value"),
55+
},
56+
),
57+
},
58+
path: path.Root("not-test"),
59+
target: new(string),
60+
expected: new(string),
61+
expectedDiags: diag.Diagnostics{
62+
diag.NewAttributeErrorDiagnostic(
63+
path.Root("not-test"),
64+
"Data Read Error",
65+
"An unexpected error was encountered trying to retrieve type information at a given path. "+
66+
"This is always an error in the provider. Please report the following to the provider developer:\n\n"+
67+
"Error: AttributeName(\"not-test\") still remains in the path: could not find attribute or block \"not-test\" in schema",
68+
),
69+
},
70+
},
3771
"invalid-target": {
3872
data: fwschemadata.Data{
3973
Schema: testschema.Schema{
@@ -104,6 +138,29 @@ func TestDataGetAtPath(t *testing.T) {
104138
),
105139
},
106140
},
141+
"TerraformValue-null": {
142+
data: fwschemadata.Data{
143+
Schema: testschema.Schema{
144+
Attributes: map[string]fwschema.Attribute{
145+
"test": testschema.Attribute{
146+
Optional: true,
147+
Type: types.StringType,
148+
},
149+
},
150+
},
151+
TerraformValue: tftypes.NewValue(
152+
tftypes.Object{
153+
AttributeTypes: map[string]tftypes.Type{
154+
"test": tftypes.String,
155+
},
156+
},
157+
nil,
158+
),
159+
},
160+
path: path.Root("test"),
161+
target: new(types.String),
162+
expected: pointer(types.StringNull()),
163+
},
107164
"AttrTypeWithValidateError": {
108165
data: fwschemadata.Data{
109166
Schema: testschema.Schema{

internal/fwserver/server_planresourcechange.go

+8
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
207207
_ = resp.PlannedState.GetAttribute(ctx, p, &plannedState)
208208
_ = req.PriorState.GetAttribute(ctx, p, &priorState)
209209

210+
// Due to ignoring diagnostics, the value may not be populated.
211+
// Prevent the panic and show the path as changed.
212+
if plannedState == nil {
213+
changedPaths.Append(p)
214+
215+
continue
216+
}
217+
210218
if plannedState.Equal(priorState) {
211219
continue
212220
}

0 commit comments

Comments
 (0)