diff --git a/cli/cliui/select.go b/cli/cliui/select.go index b3222cbbf3a71..f609ca81c3e26 100644 --- a/cli/cliui/select.go +++ b/cli/cliui/select.go @@ -349,7 +349,11 @@ func RichMultiSelect(inv *serpent.Invocation, richOptions RichMultiSelectOptions } // Check selected option, convert descriptions (line) to values - var results []string + // + // The function must return an initialized empty array, since it is later marshaled + // into JSON. Otherwise, `var results []string` would be marshaled to "null". + // See: https://github.com/golang/go/issues/27589 + results := []string{} for _, sel := range selected { custom := true for i, option := range richOptions.Options { diff --git a/cli/cliui/select_test.go b/cli/cliui/select_test.go index 21fc4cb03c398..55ab81f50f01b 100644 --- a/cli/cliui/select_test.go +++ b/cli/cliui/select_test.go @@ -111,6 +111,17 @@ func TestRichMultiSelect(t *testing.T) { allowCustom: true, want: []string{"aaa", "bbb"}, }, + { + name: "NoOptionSelected", + options: []codersdk.TemplateVersionParameterOption{ + {Name: "AAA", Description: "This is AAA", Value: "aaa"}, + {Name: "BBB", Description: "This is BBB", Value: "bbb"}, + {Name: "CCC", Description: "This is CCC", Value: "ccc"}, + }, + defaults: []string{}, + allowCustom: false, + want: []string{}, + }, } for _, tt := range tests {