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

chore: make the patch to objchange.ProposedNew explicit #2246

Merged
merged 1 commit into from
Jul 25, 2024
Merged
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
28 changes: 27 additions & 1 deletion pkg/tfshim/sdk-v2/internal/tf/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"os/exec"
"path/filepath"
"strings"

"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
)

//go:generate go run generate.go
Expand Down Expand Up @@ -97,7 +99,7 @@ func files() []file {
{
src: "internal/plans/objchange/objchange.go",
dest: "plans/objchange/objchange.go",
transforms: transforms,
transforms: append(transforms, patchProposedNewForUnknownBlocks),
},
{
src: "internal/plans/objchange/plan_valid.go",
Expand Down Expand Up @@ -178,3 +180,27 @@ func fixupCodeTypeError(code string) string {
after := `panic(fmt.Sprintf("unsupported block nesting mode %v"`
return strings.ReplaceAll(code, before, after)
}

// This patch introduces a change in behavior for the vendored objchange.ProposedNew algorithm. Before the change,
// planning a block change where config is entirely unknown used to pick the prior state. After the change it picks the
// unknown. This is especially interesting when planning set-nested blocks, as when the algorithm fails to find a
// matching set element in priorState it will send prior=null instead, and proceed to substitute null with an empty
// value matching the block structure. Without the patch, this empty value will be selected over the unknown and
// surfaced to Pulumi users, which is confusing.
//
// See TestUnknowns test suite and the "unknown for set block prop" test case.
//
// TODO[pulumi/pulumi-terraform-bridge#2247] revisit this patch.
func patchProposedNewForUnknownBlocks(goCode string) string {
oldCode := `func proposedNew(schema *configschema.Block, prior, config cty.Value) cty.Value {
if config.IsNull() || !config.IsKnown() {`

newCode := `func proposedNew(schema *configschema.Block, prior, config cty.Value) cty.Value {
if !config.IsKnown() {
return config
}
if config.IsNull() {`
updatedGoCode := strings.Replace(goCode, oldCode, newCode, 1)
contract.Assertf(updatedGoCode != oldCode, "patchProposedNewForUnknownBlocks failed to apply")
return updatedGoCode
}