Skip to content

Commit

Permalink
Fix panic when resource shape is invalid (#677)
Browse files Browse the repository at this point in the history
Fixes #668 

I believe the issue is that we assign packages to the parsed template
without checking diagnostics:
```
packages, err := packages.SearchPackageDecls(directory)
template.Packages = packages
```
  • Loading branch information
Zaid-Ajaj authored Nov 16, 2024
1 parent 429991d commit 3c4dea6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
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() {
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: {}

0 comments on commit 3c4dea6

Please sign in to comment.