@@ -26,6 +26,7 @@ import (
26
26
"strings"
27
27
28
28
"github.com/sirupsen/logrus"
29
+ "gopkg.in/yaml.v3"
29
30
30
31
"github.com/compose-spec/compose-go/v2/consts"
31
32
"github.com/compose-spec/compose-go/v2/dotenv"
@@ -482,8 +483,27 @@ func (o *ProjectOptions) prepare(ctx context.Context) (*types.ConfigDetails, err
482
483
return configDetails , err
483
484
}
484
485
486
+ isNamed := false
487
+ if o .Name == "" {
488
+ type named struct {
489
+ Name string `yaml:"name,omitempty"`
490
+ }
491
+ // if any of the compose file is named, this is equivalent to user passing --project-name
492
+ for _ , cfg := range configDetails .ConfigFiles {
493
+ var n named
494
+ err = yaml .Unmarshal (cfg .Content , & n )
495
+ if err != nil {
496
+ return nil , err
497
+ }
498
+ if n .Name != "" {
499
+ isNamed = true
500
+ break
501
+ }
502
+ }
503
+ }
504
+
485
505
o .loadOptions = append (o .loadOptions ,
486
- withNamePrecedenceLoad (defaultDir , o ),
506
+ withNamePrecedenceLoad (defaultDir , isNamed , o ),
487
507
withConvertWindowsPaths (o ),
488
508
withListeners (o ))
489
509
@@ -496,13 +516,13 @@ func ProjectFromOptions(ctx context.Context, options *ProjectOptions) (*types.Pr
496
516
return options .LoadProject (ctx )
497
517
}
498
518
499
- func withNamePrecedenceLoad (absWorkingDir string , options * ProjectOptions ) func (* loader.Options ) {
519
+ func withNamePrecedenceLoad (absWorkingDir string , namedInYaml bool , options * ProjectOptions ) func (* loader.Options ) {
500
520
return func (opts * loader.Options ) {
501
521
if options .Name != "" {
502
522
opts .SetProjectName (options .Name , true )
503
523
} else if nameFromEnv , ok := options .Environment [consts .ComposeProjectName ]; ok && nameFromEnv != "" {
504
524
opts .SetProjectName (nameFromEnv , true )
505
- } else {
525
+ } else if ! namedInYaml {
506
526
dirname := filepath .Base (absWorkingDir )
507
527
symlink , err := filepath .EvalSymlinks (absWorkingDir )
508
528
if err == nil && filepath .Base (symlink ) != dirname {
0 commit comments