Skip to content

Commit a5e535f

Browse files
committed
Describe valid project name on error
PR #261 changed `docker compose` behavior to require normalized project names as input, instead of normalizing project names automatically. This landed in compose-spec/compose-go v1.2.5 and docker/compose v2.5.1. However, the previous error message gave no indication why a name isn't valid. This is surprising for users whose previously working project names no longer work with newer versions of `docker compose`. As of compose-spec/compose-spec#314, the spec now contains the text: > Project name MUST contain only lowercase letters, decimal digits, > dashes, and underscores, and MUST begin with a lowercase letter or > decimal digit. This updated error message provides a concise version of this requirement based on regular expression character range notation. See also: - compose-spec/compose-spec#311 - docker/compose#9741 Signed-off-by: Mike Bland <[email protected]>
1 parent 5837d67 commit a5e535f

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

cli/options.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func NewProjectOptions(configs []string, opts ...ProjectOptionsFn) (*ProjectOpti
6565
func WithName(name string) ProjectOptionsFn {
6666
return func(o *ProjectOptions) error {
6767
if name != loader.NormalizeProjectName(name) {
68-
return fmt.Errorf("%q is not a valid project name", name)
68+
return fmt.Errorf("%q is not a valid project name: it must contain "+
69+
"only characters from [a-z0-9_-] and start with [a-z0-9]", name)
6970
}
7071
o.Name = name
7172
return nil

cli/options_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func TestProjectName(t *testing.T) {
5454

5555
t.Run("by name start with invalid char '-'", func(t *testing.T) {
5656
_, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("-my_project"))
57-
assert.Error(t, err, `"-my_project" is not a valid project name`)
57+
assert.ErrorContains(t, err, `"-my_project" is not a valid project name`)
5858

5959
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
6060
fmt.Sprintf("%s=%s", consts.ComposeProjectName, "-my_project"),
@@ -67,7 +67,7 @@ func TestProjectName(t *testing.T) {
6767

6868
t.Run("by name start with invalid char '_'", func(t *testing.T) {
6969
_, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("_my_project"))
70-
assert.Error(t, err, `"_my_project" is not a valid project name`)
70+
assert.ErrorContains(t, err, `"_my_project" is not a valid project name`)
7171

7272
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
7373
fmt.Sprintf("%s=%s", consts.ComposeProjectName, "_my_project"),
@@ -80,7 +80,7 @@ func TestProjectName(t *testing.T) {
8080

8181
t.Run("by name contains dots", func(t *testing.T) {
8282
_, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("www.my.project"))
83-
assert.Error(t, err, `"www.my.project" is not a valid project name`)
83+
assert.ErrorContains(t, err, `"www.my.project" is not a valid project name`)
8484

8585
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
8686
fmt.Sprintf("%s=%s", consts.ComposeProjectName, "www.my.project"),
@@ -93,7 +93,7 @@ func TestProjectName(t *testing.T) {
9393

9494
t.Run("by name uppercase", func(t *testing.T) {
9595
_, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithName("MY_PROJECT"))
96-
assert.Error(t, err, `"MY_PROJECT" is not a valid project name`)
96+
assert.ErrorContains(t, err, `"MY_PROJECT" is not a valid project name`)
9797

9898
opts, err := NewProjectOptions([]string{"testdata/simple/compose.yaml"}, WithEnv([]string{
9999
fmt.Sprintf("%s=%s", consts.ComposeProjectName, "_my_project"),

0 commit comments

Comments
 (0)