Skip to content

Commit ba6d569

Browse files
ndeloofglours
authored andcommitted
Always set COMPOSE_PROJECT_NAME
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 25fe179 commit ba6d569

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

loader/loader.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -329,19 +329,11 @@ func loadModelWithContext(ctx context.Context, configDetails *types.ConfigDetail
329329
return nil, errors.New("No files specified")
330330
}
331331

332-
err := projectName(*configDetails, opts)
332+
err := projectName(configDetails, opts)
333333
if err != nil {
334334
return nil, err
335335
}
336336

337-
// TODO(milas): this should probably ALWAYS set (overriding any existing)
338-
if _, ok := configDetails.Environment[consts.ComposeProjectName]; !ok && opts.projectName != "" {
339-
if configDetails.Environment == nil {
340-
configDetails.Environment = map[string]string{}
341-
}
342-
configDetails.Environment[consts.ComposeProjectName] = opts.projectName
343-
}
344-
345337
return load(ctx, *configDetails, opts, nil)
346338
}
347339

@@ -601,10 +593,14 @@ func InvalidProjectNameErr(v string) error {
601593
// projectName determines the canonical name to use for the project considering
602594
// the loader Options as well as `name` fields in Compose YAML fields (which
603595
// also support interpolation).
604-
//
605-
// TODO(milas): restructure loading so that we don't need to re-parse the YAML
606-
// here, as it's both wasteful and makes this code error-prone.
607-
func projectName(details types.ConfigDetails, opts *Options) error {
596+
func projectName(details *types.ConfigDetails, opts *Options) error {
597+
defer func() {
598+
if details.Environment == nil {
599+
details.Environment = map[string]string{}
600+
}
601+
details.Environment[consts.ComposeProjectName] = opts.projectName
602+
}()
603+
608604
if opts.projectNameImperativelySet {
609605
if NormalizeProjectName(opts.projectName) != opts.projectName {
610606
return InvalidProjectNameErr(opts.projectName)

loader/loader_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,20 +2376,21 @@ services:
23762376
assert.Equal(t, "overrided-proxy", svc.ContainerName)
23772377
})
23782378

2379-
t.Run("project name env variable interpolation", func(t *testing.T) {
2379+
t.Run("project name override", func(t *testing.T) {
23802380
yaml := `
2381-
name: interpolated
2381+
name: another_name
23822382
services:
23832383
web:
23842384
image: web
23852385
container_name: ${COMPOSE_PROJECT_NAME}-web
23862386
`
2387-
configDetails := buildConfigDetails(yaml, map[string]string{"COMPOSE_PROJECT_NAME": "env-var"})
2388-
actual, err := LoadWithContext(context.TODO(), configDetails)
2387+
configDetails := buildConfigDetails(yaml, map[string]string{})
2388+
2389+
actual, err := Load(configDetails, withProjectName("interpolated", true))
23892390
assert.NilError(t, err)
23902391
svc, err := actual.GetService("web")
23912392
assert.NilError(t, err)
2392-
assert.Equal(t, "env-var-web", svc.ContainerName)
2393+
assert.Equal(t, "interpolated-web", svc.ContainerName)
23932394
})
23942395
}
23952396

0 commit comments

Comments
 (0)