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

Don't emit missing var errors for const vars #11426

Merged
merged 1 commit into from Nov 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions pkg/codegen/hcl2/model/type_const.go
Expand Up @@ -98,3 +98,20 @@ func (t *ConstType) unify(other Type) (Type, ConversionKind) {
}

func (*ConstType) isType() {}

func IsConstType(t Type) bool {
switch t := t.(type) {
case *ConstType:
return true
case *UnionType:
for _, t := range t.ElementTypes {
_, ok := t.(*ConstType)
if !ok {
return false
}
}
return true
default:
return false
}
}
11 changes: 9 additions & 2 deletions pkg/codegen/pcl/binder_resource.go
Expand Up @@ -382,9 +382,16 @@ func (b *binder) bindResourceBody(node *Resource) hcl.Diagnostics {
}

for _, k := range codegen.SortedKeys(objectType.Properties) {
if !model.IsOptionalType(objectType.Properties[k]) && !attrNames.Has(k) {
diag(missingRequiredAttribute(k, block.Body.Syntax.MissingItemRange()))
typ := objectType.Properties[k]
if model.IsOptionalType(typ) || attrNames.Has(k) {
// The type is present or optional. No error.
continue
}
if model.IsConstType(objectType.Properties[k]) {
// The type is const, so the value is implied. No error.
continue
}
diag(missingRequiredAttribute(k, block.Body.Syntax.MissingItemRange()))
}
}

Expand Down
3 changes: 0 additions & 3 deletions pkg/codegen/testing/test/program_driver.go
Expand Up @@ -292,9 +292,6 @@ var PulumiPulumiYAMLProgramTests = []ProgramTest{
Directory: transpiled("kubernetes"),
Description: "Kubernetes",
Skip: codegen.NewStringSet("go"),
// PCL resource attribute type checking doesn't handle missing `const` attributes.
//
BindOptions: []pcl.BindOption{pcl.SkipResourceTypechecking},
},
{
Directory: transpiled("pulumi-variable"),
Expand Down
Expand Up @@ -7,7 +7,6 @@
var bar = new Kubernetes.Core.V1.Pod("bar", new()
{
ApiVersion = "v1",
Kind = "Pod",
Metadata = new Kubernetes.Types.Inputs.Meta.V1.ObjectMetaArgs
{
Namespace = "foo",
Expand Down Expand Up @@ -41,5 +40,7 @@
},
});

var kind = bar.Kind;

});

Expand Up @@ -8,9 +8,8 @@ import (

func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
_, err := corev1.NewPod(ctx, "bar", &corev1.PodArgs{
bar, err := corev1.NewPod(ctx, "bar", &corev1.PodArgs{
ApiVersion: pulumi.String("v1"),
Kind: pulumi.String("Pod"),
Metadata: &metav1.ObjectMetaArgs{
Namespace: pulumi.String("foo"),
Name: pulumi.String("bar"),
Expand Down Expand Up @@ -38,6 +37,7 @@ func main() {
if err != nil {
return err
}
_ := bar.Kind
return nil
})
}
@@ -1,6 +1,5 @@
resource bar "kubernetes:core/v1:Pod" {
apiVersion = "v1"
kind = "Pod"
metadata = {
namespace = "foo"
name = "bar"
Expand All @@ -21,3 +20,6 @@
]
}
}

// Test that we can assign from a constant without type errors
kind = bar.kind
Expand Up @@ -3,7 +3,6 @@ import * as kubernetes from "@pulumi/kubernetes";

const bar = new kubernetes.core.v1.Pod("bar", {
apiVersion: "v1",
kind: "Pod",
metadata: {
namespace: "foo",
name: "bar",
Expand All @@ -24,3 +23,4 @@ const bar = new kubernetes.core.v1.Pod("bar", {
}],
},
});
const kind = bar.kind;
Expand Up @@ -3,7 +3,6 @@

bar = kubernetes.core.v1.Pod("bar",
api_version="v1",
kind="Pod",
metadata=kubernetes.meta.v1.ObjectMetaArgs(
namespace="foo",
name="bar",
Expand All @@ -23,3 +22,4 @@
),
)],
))
kind = bar.kind