Repository: compose-spec/compose-go
Pull request #98 (2021-01-21 00:05:37)

Use absolute path for config files
Should solve  https://github.com/docker/compose-cli/issues/1139 after the bump


Patch:
diff --git a/cli/options.go b/cli/options.go
index 08c0415..6aeee71 100644
--- a/cli/options.go
+++ b/cli/options.go
@@ -167,7 +167,7 @@ func (o ProjectOptions) GetWorkingDir() (string, error) {
 
 // ProjectFromOptions load a compose project based on command line options
 func ProjectFromOptions(options *ProjectOptions) (*types.Project, error) {
-	configPaths, specifiedComposeFiles, err := getConfigPathsFromOptions(options)
+	configPaths, err := getConfigPathsFromOptions(options)
 	if err != nil {
 		return nil, err
 	}
@@ -207,18 +207,18 @@ func ProjectFromOptions(options *ProjectOptions) (*types.Project, error) {
 		return nil, err
 	}
 
-	project.ComposeFiles = specifiedComposeFiles
+	project.ComposeFiles = configPaths
 	return project, nil
 }
 
 // getConfigPathsFromOptions retrieves the config files for project based on project options
-func getConfigPathsFromOptions(options *ProjectOptions) ([]string, []string, error) {
+func getConfigPathsFromOptions(options *ProjectOptions) ([]string, error) {
 	paths := []string{}
 	pwd := options.WorkingDir
 	if pwd == "" {
 		wd, err := os.Getwd()
 		if err != nil {
-			return nil, nil, err
+			return nil, err
 		}
 		pwd = wd
 	}
@@ -233,11 +233,18 @@ func getConfigPathsFromOptions(options *ProjectOptions) ([]string, []string, err
 				f = filepath.Join(pwd, f)
 			}
 			if _, err := os.Stat(f); err != nil {
-				return nil, nil, err
+				return nil, err
+			}
+			if !filepath.IsAbs(f) {
+				fAbs, err := filepath.Abs(f)
+				if err != nil {
+					return nil, err
+				}
+				f = fAbs
 			}
 			paths = append(paths, f)
 		}
-		return paths, options.ConfigPaths, nil
+		return paths, nil
 	}
 
 	sep := os.Getenv(ComposeFileSeparator)
@@ -246,7 +253,7 @@ func getConfigPathsFromOptions(options *ProjectOptions) ([]string, []string, err
 	}
 	f := os.Getenv(ComposeFilePath)
 	if f != "" {
-		return strings.Split(f, sep), strings.Split(f, sep), nil
+		return strings.Split(f, sep), nil
 	}
 
 	for {
@@ -263,11 +270,11 @@ func getConfigPathsFromOptions(options *ProjectOptions) ([]string, []string, err
 				logrus.Warnf("Found multiple config files with supported names: %s", strings.Join(candidates, ", "))
 				logrus.Warnf("Using %s", winner)
 			}
-			return []string{winner}, []string{winner}, nil
+			return []string{winner}, nil
 		}
 		parent := filepath.Dir(pwd)
 		if parent == pwd {
-			return nil, nil, errors.Wrap(errdefs.ErrNotFound, "can't find a suitable configuration file in this directory or any parent")
+			return nil, errors.Wrap(errdefs.ErrNotFound, "can't find a suitable configuration file in this directory or any parent")
 		}
 		pwd = parent
 	}
@@ -292,6 +299,13 @@ func parseConfigs(configPaths []string) ([]types.ConfigFile, error) {
 		if err != nil {
 			return nil, err
 		}
+		if !filepath.IsAbs(f) {
+			fAbs, err := filepath.Abs(f)
+			if err != nil {
+				return nil, err
+			}
+			f = fAbs
+		}
 		files = append(files, types.ConfigFile{Filename: f, Config: config})
 	}
 	return files, nil


Test patch:
diff --git a/cli/options_test.go b/cli/options_test.go
index 48bfa56..4e99c6f 100644
--- a/cli/options_test.go
+++ b/cli/options_test.go
@@ -81,7 +81,11 @@ func TestProjectComposefilesFromWorkingDir(t *testing.T) {
 	assert.NilError(t, err)
 	p, err := ProjectFromOptions(opts)
 	assert.NilError(t, err)
-	assert.DeepEqual(t, p.ComposeFiles, []string{"testdata/simple/compose.yaml", "testdata/simple/compose-with-overrides.yaml"})
+	currentDir, _ := os.Getwd()
+	assert.DeepEqual(t, p.ComposeFiles, []string{
+		filepath.Join(currentDir, "testdata/simple/compose.yaml"),
+		filepath.Join(currentDir, "testdata/simple/compose-with-overrides.yaml"),
+	})
 }
 
 func TestProjectWithDotEnv(t *testing.T) {