Skip to content

Commit 8c4b79e

Browse files
authored
load: move env var profile detection to option (#446)
Instead of reading `COMPOSE_PROFILES` environment variable directly during `load()`, rely exclusively on `opts.Profiles`. A new loader option, `WithDefaultProfiles`, allows passing profile name(s) via varargs. If none are provided, it will fallback to using any profiles set via `COMPOSE_PROFILES`. This improves purity of the loader and fixes issues with profiles being ignored for `include` (docker/compose#10906). Signed-off-by: Milas Bowman <[email protected]>
1 parent 1be5aca commit 8c4b79e

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

cli/options.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func WithEnv(env []string) ProjectOptionsFn {
194194
}
195195
}
196196

197-
// WithDiscardEnvFiles sets discards the `env_file` section after resolving to
197+
// WithDiscardEnvFile sets discards the `env_file` section after resolving to
198198
// the `environment` section
199199
func WithDiscardEnvFile(o *ProjectOptions) error {
200200
o.loadOptions = append(o.loadOptions, loader.WithDiscardEnvFiles)
@@ -209,6 +209,15 @@ func WithLoadOptions(loadOptions ...func(*loader.Options)) ProjectOptionsFn {
209209
}
210210
}
211211

212+
// WithDefaultProfiles uses the provided profiles (if any), and falls back to
213+
// profiles specified via the COMPOSE_PROFILES environment variable otherwise.
214+
func WithDefaultProfiles(profile ...string) ProjectOptionsFn {
215+
if len(profile) == 0 {
216+
profile = strings.Split(os.Getenv(consts.ComposeProfiles), ",")
217+
}
218+
return WithProfiles(profile)
219+
}
220+
212221
// WithProfiles sets profiles to be activated
213222
func WithProfiles(profiles []string) ProjectOptionsFn {
214223
return func(o *ProjectOptions) error {
@@ -228,8 +237,9 @@ func WithOsEnv(o *ProjectOptions) error {
228237
return nil
229238
}
230239

231-
// WithEnvFile set an alternate env file
232-
// deprecated - use WithEnvFiles
240+
// WithEnvFile sets an alternate env file.
241+
//
242+
// Deprecated: use WithEnvFiles instead.
233243
func WithEnvFile(file string) ProjectOptionsFn {
234244
var files []string
235245
if file != "" {

loader/loader.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,6 @@ func load(ctx context.Context, configDetails types.ConfigDetails, opts *Options,
360360
}
361361
}
362362

363-
if profiles, ok := project.Environment[consts.ComposeProfiles]; ok && len(opts.Profiles) == 0 {
364-
opts.Profiles = strings.Split(profiles, ",")
365-
}
366363
project.ApplyProfiles(opts.Profiles)
367364

368365
err := project.ResolveServicesEnvironment(opts.discardEnvFiles)

0 commit comments

Comments
 (0)