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

Fix panic when resource shape is invalid #677

Merged
merged 1 commit into from
Nov 16, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Bug Fixes-677.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
component: runtime
kind: Bug Fixes
body: Fix panic when resource shape is invalid
time: 2024-11-15T00:51:03.499599+01:00
custom:
PR: "677"
4 changes: 4 additions & 0 deletions pkg/pulumiyaml/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func LoadDir(directory string) (*ast.TemplateDecl, syntax.Diagnostics, error) {
return nil, diags, err
}

if diags.HasErrors() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we still do SeachPackageDecls so we can add that to the diagnostics as well if it errors but then check if template is nil just before assigning Packages to it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean if the template has errors while parsing, we would early exit like most Go functions, unless it is a pattern to combine errors from functions downstream. I don't have a strong opinion about this, could fix it either way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh fine, easy to change if a compelling reason comes up later

return nil, diags, diags
}

packages, err := packages.SearchPackageDecls(directory)
if err != nil {
diags.Extend(syntax.Error(nil, err.Error(), ""))
Expand Down
17 changes: 17 additions & 0 deletions pkg/pulumiyaml/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,23 @@ resources:
requireNoErrors(t, tmpl, diags)
}

func TestUnknownResource(t *testing.T) {
t.Parallel()

const text = `
runtime: yaml
config: {}
variables: {}
resources: {badResource}
outputs: {}`

pt, diags, _ := LoadYAMLBytes("<stdin>", []byte(text))
assert.Nil(t, pt)
assert.Len(t, diags, 1)
assert.True(t, diags.HasErrors())
assert.Contains(t, diags[0].Error(), "resources.badResource must be an object")
}

func TestPoisonResult(t *testing.T) {
t.Parallel()

Expand Down
9 changes: 9 additions & 0 deletions pkg/tests/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func TestTypeCheckError(t *testing.T) {
})
}

//nolint:paralleltest // uses parallel programtest
func TestInvalidResourceObject(t *testing.T) {
testWrapper(t, integrationDir("invalid-resource-object"), ExpectFailure, StderrValidator{
f: func(t *testing.T, stderr string) {
assert.Contains(t, stderr, "resources.badResource must be an object")
},
})
}

//nolint:paralleltest // uses parallel programtest
func TestMismatchedConfigType(t *testing.T) {
testWrapper(t, integrationDir("mismatched-config-type"), ExpectFailure, StderrValidator{
Expand Down
6 changes: 6 additions & 0 deletions pkg/tests/testdata/invalid-resource-object/Pulumi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: myymltest
description: A minimal Pulumi YAML program
runtime: yaml
variables: {}
resources: {badResource}
outputs: {}
Loading