Skip to content

Commit

Permalink
Merge pull request #402 from pulumi/aqiu/401
Browse files Browse the repository at this point in the history
Do not error on duplicate config keys
  • Loading branch information
AaronFriel committed Nov 8, 2022
2 parents a0fbe76 + eb76087 commit e1986dd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
[#393](https://github.com/pulumi/pulumi-yaml/pull/393)

### Bug Fixes

- Do not error on duplicate config keys.
[#402](https://github.com/pulumi/pulumi-yaml/pull/402)
9 changes: 5 additions & 4 deletions pkg/pulumiyaml/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,9 @@ func LoadYAMLBytes(filename string, source []byte) (*ast.TemplateDecl, syntax.Di
if tdiags.HasErrors() {
return nil, diags, nil
}
// TODO: warn if using old configuration block
// if t.Configuration.Entries != nil {
// diags = append(diags, syntax.Warning(nil, "Pulumi.yaml: root-level `configuration` field is deprecated; please use `config` instead.", ""))
// }
if t.Configuration.Entries != nil {
diags = append(diags, syntax.Warning(nil, "Pulumi.yaml: root-level `configuration` field is deprecated; please use `config` instead.", ""))
}

return t, diags, nil
}
Expand Down Expand Up @@ -710,6 +709,8 @@ func getPulumiConfNodes(config map[string]string) ([]configNode, error) {
var errors multierror.Error
idx := 0
for k, v := range config {
// Strip the project prefix
k := strings.Split(k, ":")[1]
// We default types to strings to avoid error cascades on mis-typed values.
typ := ctypes.String
var value interface{} = v
Expand Down
11 changes: 10 additions & 1 deletion pkg/pulumiyaml/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type configNodeEnv struct {
}

func (e configNodeEnv) valueKind() string {
return "config"
return "configEnv"
}

func (e configNodeEnv) key() *ast.StringExpr {
Expand Down Expand Up @@ -238,6 +238,10 @@ func checkUniqueNode(intermediates map[string]graphNode, node graphNode) syntax.
}

if other, found := intermediates[name]; found {
// if duplicate key from config/ configuration, do not warn about using configuration again
if isConfigNodeEnv(node) || isConfigNodeEnv(other) {
return diags
}
if node.valueKind() == other.valueKind() {
diags.Extend(ast.ExprError(key, fmt.Sprintf("found duplicate %s %s", node.valueKind(), name), ""))
} else {
Expand All @@ -247,3 +251,8 @@ func checkUniqueNode(intermediates map[string]graphNode, node graphNode) syntax.
}
return diags
}

func isConfigNodeEnv(n graphNode) bool {
_, ok := n.(configNodeEnv)
return ok
}
10 changes: 9 additions & 1 deletion pkg/tests/example_transpile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,15 @@ func TestGenerateExamples(t *testing.T) {
_, template, diags, err := codegen.LoadTemplate(exampleProjectDir)
require.NoError(t, err, "Loading project %v", dir)
require.False(t, diags.HasErrors(), diags.Error())
assert.Len(t, diags, 0, "Should have neither warnings nor errors")
// TODO: update examples to use `config` instead of `configuration`. For now, filter out the "`configuration` field is deprecated" warning.
var filteredDiags hcl.Diagnostics
for _, d := range diags {
if strings.Contains(d.Summary, "`configuration` field is deprecated") {
continue
}
filteredDiags = append(filteredDiags, d)
}
assert.Len(t, filteredDiags, 0, "Should have neither warnings nor errors")
if t.Failed() {
return
}
Expand Down

0 comments on commit e1986dd

Please sign in to comment.