Repository: compose-spec/compose-go
Pull request #198 (2021-10-25 18:04:34)

 Fixing variable substitution when using compose v2.
This fixes https://github.com/docker/compose/issues/8798, variable substitution not working. In godotenv, there seems to be an empty envMap, which should be in my opinion the lookup function, then checks the environment variables. Also to trigger this env lookup, compose-go has to call godotenv.ParseWithLookup. See also https://github.com/compose-spec/godotenv/pull/9


Patch:
diff --git a/go.mod b/go.mod
index aa9e124..91bc13e 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/compose-spec/compose-go
 go 1.16
 
 require (
-	github.com/compose-spec/godotenv v1.0.0
+	github.com/compose-spec/godotenv v1.1.0
 	github.com/distribution/distribution/v3 v3.0.0-20210316161203-a01c71e2477e
 	github.com/docker/go-connections v0.4.0
 	github.com/docker/go-units v0.4.0
diff --git a/go.sum b/go.sum
index 9b6e04d..4b09240 100644
--- a/go.sum
+++ b/go.sum
@@ -14,8 +14,8 @@ github.com/bshuster-repo/logrus-logstash-hook v1.0.0/go.mod h1:zsTqEiSzDgAa/8GZR
 github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd/go.mod h1:2oa8nejYd4cQ/b0hMIopN0lCRxU0bueqREvZLWFrtK8=
 github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b/go.mod h1:obH5gd0BsqsP2LwDJ9aOkm/6J86V6lyAXCoQWGw3K50=
 github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0/go.mod h1:D/8v3kj0zr8ZAKg1AQ6crr+5VwKN5eIywRkfhyM/+dE=
-github.com/compose-spec/godotenv v1.0.0 h1:TV24JYhh5GCC1G14npQVhCtxeoiwd0NcT0VdwcCQyXU=
-github.com/compose-spec/godotenv v1.0.0/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
+github.com/compose-spec/godotenv v1.1.0 h1:wzShe5P6L/Aw3wsV357eWlZdMcPaOe2V2+3+qGwMEL4=
+github.com/compose-spec/godotenv v1.1.0/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
diff --git a/loader/loader.go b/loader/loader.go
index 57505ee..159e9c7 100644
--- a/loader/loader.go
+++ b/loader/loader.go
@@ -559,7 +559,7 @@ func resolveEnvironment(serviceConfig *types.ServiceConfig, workingDir string, l
 				return err
 			}
 			defer file.Close()
-			fileVars, err := godotenv.Parse(file)
+			fileVars, err := godotenv.ParseWithLookup(file, godotenv.LookupFn(lookupEnv))
 			if err != nil {
 				return err
 			}


Test patch:
diff --git a/loader/loader_test.go b/loader/loader_test.go
index b814d81..ead2e17 100644
--- a/loader/loader_test.go
+++ b/loader/loader_test.go
@@ -1811,3 +1811,22 @@ services:
 `)
 	assert.NilError(t, err)
 }
+
+func TestLoadService(t *testing.T) {
+	file, err := os.CreateTemp("", "test-compose-go")
+	assert.NilError(t, err)
+	defer os.Remove(file.Name())
+
+	_, err = file.Write([]byte("HALLO=$TEST"))
+	assert.NilError(t, err)
+
+	m := map[string]interface{}{
+		"env_file": file.Name(),
+	}
+	s, err := LoadService("Test Name", m, ".", func(s string) (string, bool) {
+		assert.Equal(t, "TEST", s)
+		return "YES", true
+	}, true)
+	assert.NilError(t, err)
+	assert.Equal(t, "YES", *s.Environment["HALLO"])
+}