Visitar URL original
[programgen] Guard references to conditionally-created resources (Pyt… · pulumi/pulumi@e2cca27 · GitHub
Skip to content

Commit e2cca27

Browse files
authored
[programgen] Guard references to conditionally-created resources (Python & NodeJS) (#23634)
## Description Stacked on #23632 (the `l3-range-ref` test split). A resource declared with a boolean `range` option is created zero or one times, so its program variable is optional. Generated programs that dereferenced such a resource were not well-typed: - **Python** emitted `bool_resource.name` on a `Target | None` value → mypy `union-attr` error. - **NodeJS** emitted `boolResource?.name`, widening the result to `Output<string> | undefined`, which is not assignable to a required `Input<string>`. This detects the dereference in a shared PCL helper, `pcl.IsOptionalResource`, and emits language-appropriate code: - **Python** (`pkg/codegen/pcl/rewrite_optional_resource.go` + `pkg/codegen/python`): wraps the access — and any apply over it — as `expr if root is not None else None`, run *after* applies are rewritten so the guard surrounds the apply rather than being hoisted inside it. Equality against `null` is now rendered with `is`/`is not`, which is idiomatic and lets the type checker narrow. - **NodeJS** (`pkg/codegen/nodejs`): emits a non-null assertion (`boolResource!.name`) at the optional-resource root, yielding `Output<string>`. Genuinely-optional schema properties keep optional chaining. The two languages need different shapes because their generated inputs differ: Python resource inputs are `Optional`-typed (so the `None` fallback is assignable), whereas NodeJS inputs are required (so only a non-null assertion type-checks). `l3-range-bool-ref` now type-checks under mypy and pyright and compiles under tsc, and is removed from the Python and NodeJS conformance expected-failure lists. Go has unrelated range program-generation bugs and is left failing. `l3-range-map-ref` (a separate collection-shape issue) is out of scope. The `is`/`is not` rendering also updates a few existing program goldens (`l2-resource-optional`, `l2-primitive-ref-optional`, `l1-config-types-object`) from `== None` to `is None`. ## Checklist - [x] Conformance: `l3-range-bool-ref` passes for Python (Default/TOML/Classes) and NodeJS (TSC/TSNode/Bun) - [x] Affected program goldens regenerated and pass - [x] `pkg/codegen` program-gen golden batches unaffected - [x] Changelog entry added
1 parent 109f27f commit e2cca27

60 files changed

Lines changed: 535 additions & 84 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
component: programgen
2+
kind: fix
3+
body: Guard references to conditionally-created (boolean `range`) resources in generated Python and NodeJS programs so they type-check
4+
time: 2026-06-18T00:00:00.000000+00:00
5+
custom:
6+
PR: "23634"

‎pkg/codegen/nodejs/gen_program_expressions.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,14 @@ func (g *generator) genRelativeTraversal(w io.Writer, traversal hcl.Traversal, p
930930
}
931931

932932
var indexPrefix string
933-
if model.IsOptionalType(model.GetTraversableType(parts[i])) {
933+
if pcl.IsOptionalResource(parts[i]) {
934+
// A conditionally-created (boolean `range`) resource is `undefined`
935+
// when the condition is false. Dereferencing its property to feed a
936+
// required input asserts the resource exists, so emit a non-null
937+
// assertion rather than optional chaining (which would widen the value
938+
// to `| undefined` and fail to type-check against the required input).
939+
g.Fgen(w, "!")
940+
} else if model.IsOptionalType(model.GetTraversableType(parts[i])) {
934941
g.Fgen(w, "?")
935942
// `expr?[expr]` is not valid typescript, since it looks like a ternary
936943
// operator.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
// Copyright 2026, Pulumi Corporation.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package pcl
16+
17+
import (
18+
"github.com/hashicorp/hcl/v2"
19+
"github.com/hashicorp/hcl/v2/hclsyntax"
20+
"github.com/zclconf/go-cty/cty"
21+
22+
"github.com/pulumi/pulumi/pkg/v3/codegen/hcl2/model"
23+
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
24+
)
25+
26+
// IsOptionalResource reports whether the given traversable is a
27+
// conditionally-created resource — a resource, read-resource, or component whose
28+
// variable type is optional because it ranges over a boolean. Such a resource is
29+
// `null`/`undefined` when the condition is false, so dereferencing one of its
30+
// properties is only well-defined when the resource exists.
31+
func IsOptionalResource(t model.Traversable) bool {
32+
switch t.(type) {
33+
case *Resource, *ReadResource, *Component:
34+
return model.IsOptionalType(model.GetTraversableType(t))
35+
default:
36+
return false
37+
}
38+
}
39+
40+
// optionalResourceRoot reports whether expr is a property access (a traversal of
41+
// at least one attribute) rooted at a conditionally-created resource. It returns
42+
// a reference to the bare resource root so callers can guard the access against
43+
// `null`.
44+
func optionalResourceRoot(expr model.Expression) (*model.ScopeTraversalExpression, bool) {
45+
traversal, ok := expr.(*model.ScopeTraversalExpression)
46+
if !ok || len(traversal.Traversal) < 2 || len(traversal.Parts) == 0 {
47+
return nil, false
48+
}
49+
if !IsOptionalResource(traversal.Parts[0]) {
50+
return nil, false
51+
}
52+
rootRef := &model.ScopeTraversalExpression{
53+
RootName: traversal.RootName,
54+
Traversal: traversal.Traversal[:1],
55+
Parts: traversal.Parts[:1],
56+
}
57+
mustTypecheck(rootRef)
58+
return rootRef, true
59+
}
60+
61+
// mustTypecheck typechecks an expression built during rewriting, where the
62+
// operands are well-formed by construction, and asserts that it does so cleanly.
63+
func mustTypecheck(expr model.Expression) {
64+
diags := expr.Typecheck(false)
65+
contract.Assertf(!diags.HasErrors(), "constructing optional-resource guard: %v", diags)
66+
}
67+
68+
// RewriteOptionalResourceGuards guards dereferences of conditionally-created
69+
// (boolean `range`) resources so the generated code is well-defined when the
70+
// resource was not created. Any expression that reads a property off such a
71+
// resource is wrapped as `root != null ? expr : null`, short-circuiting the
72+
// access — and any apply over it — to `null` when the resource is absent.
73+
//
74+
// The guard is built from the resource references themselves rather than the
75+
// original range condition so it narrows the optional type for type checkers.
76+
// It must run before applies are rewritten so the apply is generated inside the
77+
// guarded branch.
78+
func RewriteOptionalResourceGuards(expr model.Expression) model.Expression {
79+
var roots []*model.ScopeTraversalExpression
80+
seen := map[string]bool{}
81+
collect := func(e model.Expression) (model.Expression, hcl.Diagnostics) {
82+
if rootRef, ok := optionalResourceRoot(e); ok && !seen[rootRef.RootName] {
83+
seen[rootRef.RootName] = true
84+
roots = append(roots, rootRef)
85+
}
86+
return e, nil
87+
}
88+
_, _ = model.VisitExpression(expr, model.IdentityVisitor, collect)
89+
if len(roots) == 0 {
90+
return expr
91+
}
92+
93+
condition := model.Expression(notNull(roots[0]))
94+
for _, root := range roots[1:] {
95+
and := &model.BinaryOpExpression{
96+
LeftOperand: condition,
97+
Operation: hclsyntax.OpLogicalAnd,
98+
RightOperand: notNull(root),
99+
}
100+
mustTypecheck(and)
101+
condition = and
102+
}
103+
104+
guarded := &model.ConditionalExpression{
105+
Condition: condition,
106+
TrueResult: expr,
107+
FalseResult: nullLiteral(),
108+
}
109+
mustTypecheck(guarded)
110+
return guarded
111+
}
112+
113+
// notNull builds a `ref != null` comparison.
114+
func notNull(ref model.Expression) *model.BinaryOpExpression {
115+
op := &model.BinaryOpExpression{
116+
LeftOperand: ref,
117+
Operation: hclsyntax.OpNotEqual,
118+
RightOperand: nullLiteral(),
119+
}
120+
mustTypecheck(op)
121+
return op
122+
}
123+
124+
func nullLiteral() model.Expression {
125+
lit := &model.LiteralValueExpression{Value: cty.NullVal(cty.DynamicPseudoType)}
126+
mustTypecheck(lit)
127+
return lit
128+
}

‎pkg/codegen/python/gen_program_expressions.go‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ func (g *generator) lowerExpression(expr model.Expression, typ model.Type) (mode
4747
expr, diags := pcl.RewriteAppliesWithSkipToJSON(expr, nameInfo(0), false, skipToJSONWhenRewritingApplies)
4848
expr, lowerProxyDiags := g.lowerProxyApplies(expr)
4949
expr, convertDiags := pcl.RewriteConversions(expr, typ)
50+
// Guard dereferences of conditionally-created resources after applies and
51+
// conversions are rewritten, so the guard wraps the (converted) apply rather
52+
// than being hoisted inside it, and the `null` fallback is left untouched.
53+
expr = pcl.RewriteOptionalResourceGuards(expr)
5054
expr, quotes, quoteDiags := g.rewriteQuotes(expr)
5155

5256
diags = diags.Extend(lowerProxyDiags)
@@ -108,6 +112,23 @@ func (g *generator) GenAnonymousFunctionExpression(w io.Writer, expr *model.Anon
108112
g.Fgenf(w, ": %.v", expr.Body)
109113
}
110114

115+
// isNoneLiteral reports whether expr is the `null`/`None` literal, so equality
116+
// comparisons against it can be rendered with Python's identity operators
117+
// (`is`/`is not`), which read idiomatically and let type checkers narrow.
118+
func isNoneLiteral(expr model.Expression) bool {
119+
switch expr := expr.(type) {
120+
case *model.LiteralValueExpression:
121+
return expr.Value.IsNull()
122+
case *model.ScopeTraversalExpression:
123+
if len(expr.Parts) == 1 {
124+
if c, ok := expr.Parts[0].(*model.Constant); ok {
125+
return c.ConstantValue.IsNull()
126+
}
127+
}
128+
}
129+
return false
130+
}
131+
111132
func (g *generator) GenBinaryOpExpression(w io.Writer, expr *model.BinaryOpExpression) {
112133
var opstr string
113134
precedence := g.GetPrecedence(expr)
@@ -118,6 +139,9 @@ func (g *generator) GenBinaryOpExpression(w io.Writer, expr *model.BinaryOpExpre
118139
opstr = "/"
119140
case hclsyntax.OpEqual:
120141
opstr = "=="
142+
if isNoneLiteral(expr.RightOperand) || isNoneLiteral(expr.LeftOperand) {
143+
opstr = "is"
144+
}
121145
case hclsyntax.OpGreaterThan:
122146
opstr = ">"
123147
case hclsyntax.OpGreaterThanOrEqual:
@@ -136,6 +160,9 @@ func (g *generator) GenBinaryOpExpression(w io.Writer, expr *model.BinaryOpExpre
136160
opstr = "*"
137161
case hclsyntax.OpNotEqual:
138162
opstr = "!="
163+
if isNoneLiteral(expr.RightOperand) || isNoneLiteral(expr.LeftOperand) {
164+
opstr = "is not"
165+
}
139166
case hclsyntax.OpSubtract:
140167
opstr = "-"
141168
default:

‎sdk/nodejs/cmd/pulumi-language-nodejs/language_test.go‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ var expectedFailures = map[string]string{
101101
"l1-expand-final": "Node.js program generation does not support `...` argument expansion",
102102
"l3-deferred-outputs": "Cannot find name '_arg0_'.",
103103
"l3-range-map-ref": "Property 'k1' does not exist on type 'Target[]'",
104-
"l3-range-bool-ref": "Type 'Output<string> | undefined' is not assignable to type 'Input<string>'",
105104
"l3-component-primitive-conversions": "primitive conversions accepted by PCL bind, but not lowered correctly by SDK generators", //nolint:lll
106105
"l2-resource-schema-secret": "does not preserve schema-secret unknown outputs",
107106
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/bin/
2+
/node_modules/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name: l3-range-bool-ref
2+
runtime: bun
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as pulumi from "@pulumi/pulumi";
2+
import * as nestedobject from "@pulumi/nestedobject";
3+
4+
const config = new pulumi.Config();
5+
const createBool = config.requireBoolean("createBool");
6+
let boolResource: nestedobject.Target | undefined;
7+
if (createBool) {
8+
boolResource = new nestedobject.Target("boolResource", {name: "bool-resource"});
9+
}
10+
const boolTarget = new nestedobject.Target("boolTarget", {name: boolResource!.name.apply(name => `${name}+`)});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "l3-range-bool-ref",
3+
"main": "index.ts",
4+
"type": "module",
5+
"devDependencies": {
6+
"@types/bun": "latest"
7+
},
8+
"peerDependencies": {
9+
"typescript": "^5"
10+
},
11+
"dependencies": {
12+
"@pulumi/pulumi": "ROOT/artifacts/pulumi-pulumi-CORE.VERSION.tgz",
13+
"@pulumi/nestedobject": "ROOT/artifacts/pulumi-nestedobject-1.42.0.tgz"
14+
}
15+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"compilerOptions": {
3+
// Environment setup & latest features
4+
"lib": ["ESNext"],
5+
"target": "ESNext",
6+
"module": "Preserve",
7+
"moduleDetection": "force",
8+
"jsx": "react-jsx",
9+
"allowJs": true,
10+
// Bundler mode
11+
"moduleResolution": "bundler",
12+
"allowImportingTsExtensions": true,
13+
"verbatimModuleSyntax": true,
14+
"noEmit": true,
15+
// Best practices
16+
"strict": true,
17+
"skipLibCheck": true,
18+
"noFallthroughCasesInSwitch": true,
19+
"noUncheckedIndexedAccess": true,
20+
"noImplicitOverride": true,
21+
// Some stricter flags (disabled by default)
22+
"noUnusedLocals": false,
23+
"noUnusedParameters": false,
24+
"noPropertyAccessFromIndexSignature": false
25+
},
26+
"files": [
27+
"index.ts",
28+
]
29+
}

0 commit comments

Comments
 (0)