Visitar URL original
new: error out on ambiguous templates (#24607) · pulumi/pulumi@1b991f9 · GitHub
Skip to content

Commit 1b991f9

Browse files
authored
new: error out on ambiguous templates (#24607)
Currently when a user runs `pulumi new <ambiguous-template-name>` because there's two templates with the same name, none ends up getting instantiated, which is confusing to the user. Since we can't really disambiguate well, just error out, and make the user pick, either through picking the template interactively, or using the publisher name. Fixes #24502
1 parent 68e69d5 commit 1b991f9

4 files changed

Lines changed: 41 additions & 4 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
component: cli/new
2+
kind: fix
3+
body: Error out on ambiguous template names instead of failing silently
4+
time: 2026-09-09T17:14:31.245335662+02:00

‎pkg/cmd/pulumi/project/newcmd/guided.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func chooseGuidedFromSource(
143143
if len(all) > 1 {
144144
fmt.Fprintln(opts.Stdout, "Falling back to the full template list.")
145145
}
146-
return pickFromSet(all, false /*yes*/, opts, selector)
146+
return pickFromSet(all, "" /*requested*/, false /*yes*/, opts, selector)
147147
}
148148

149149
func (g *guided) choose() (cmdTemplates.Template, error) {

‎pkg/cmd/pulumi/project/newcmd/template.go‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,19 @@ func resolveTemplate(
9494
if err != nil {
9595
return nil, err
9696
}
97-
return declinedToChoose(pickFromSet(all, args.yes, opts, selector))
97+
return declinedToChoose(pickFromSet(all, args.templateNameOrURL, args.yes, opts, selector))
9898
}
9999

100-
// `--yes` never prompts, and takes no template rather than guessing among several.
100+
// `--yes` never prompts: a requested template matching several is an error, and no
101+
// requested template takes none rather than guessing among all of them.
101102
func pickFromSet(
102-
templates []cmdTemplates.Template, yes bool, opts display.Options, selector selectFunc,
103+
templates []cmdTemplates.Template, requested string, yes bool, opts display.Options, selector selectFunc,
103104
) (cmdTemplates.Template, error) {
104105
switch {
105106
case len(templates) == 1:
106107
return templates[0], nil
108+
case yes && requested != "" && len(templates) > 1:
109+
return nil, ambiguousTemplateError(requested, templates)
107110
case yes:
108111
return nil, nil
109112
case len(templates) == 0:
@@ -112,6 +115,17 @@ func pickFromSet(
112115
return chooseTemplateFromList(sortedForDisplay(templates), opts, selector)
113116
}
114117

118+
func ambiguousTemplateError(requested string, templates []cmdTemplates.Template) error {
119+
names := make([]string, 0, len(templates))
120+
for _, t := range sortedForDisplay(templates) {
121+
names = append(names, t.DisplayName())
122+
}
123+
return fmt.Errorf("template %q is ambiguous, it matches: %s; "+
124+
"rerun without --yes to choose one interactively, or use a more specific "+
125+
"name such as <publisher>/%s",
126+
requested, strings.Join(names, ", "), requested)
127+
}
128+
115129
func templateLabeler(templates []cmdTemplates.Template) func(cmdTemplates.Template) string {
116130
maxNameLength := 0
117131
for _, template := range templates {

‎pkg/cmd/pulumi/project/newcmd/template_test.go‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,25 @@ func TestResolveTemplateWithoutGuidedFlowUsesTheFullSetOnly(t *testing.T) {
239239
assert.Nil(t, got, "--yes takes no template rather than guessing among several")
240240
}
241241

242+
func TestResolveTemplateYesErrorsOnAmbiguousName(t *testing.T) {
243+
t.Parallel()
244+
245+
src := unsplitSource{all: []cmdTemplates.Template{
246+
fakeTemplate{name: "aws-typescript"},
247+
fakeRegistryTemplate{fakeTemplate: fakeTemplate{name: "aws-typescript"}, publisher: "pequod"},
248+
}}
249+
250+
noPrompt := func(string, []string, display.Options) (int, error) {
251+
t.Error("--yes must never prompt")
252+
return 0, nil
253+
}
254+
got, err := resolveTemplate(
255+
src, newArgs{yes: true, templateNameOrURL: "aws-typescript"}, display.Options{}, noPrompt)
256+
assert.Nil(t, got)
257+
assert.ErrorContains(t, err, `template "aws-typescript" is ambiguous`)
258+
assert.ErrorContains(t, err, "rerun without --yes")
259+
}
260+
242261
func TestSortedForDisplaySortsAndMarksBroken(t *testing.T) {
243262
t.Parallel()
244263

0 commit comments

Comments
 (0)