Skip to content

Commit

Permalink
env: fix path resolution with extends
Browse files Browse the repository at this point in the history
* Fix an issue with `WithEnvFile` helper to not pass empty values
* Rewrite `env_file` paths from `extends` file to be relative to
  the included file rather than parent (this was a regression)
* Normalize `extends.file` path to be absolute when doing the same
  for other paths

See docker/compose#10258.

Signed-off-by: Milas Bowman <[email protected]>
  • Loading branch information
milas committed Feb 15, 2023
1 parent 99db19a commit 7a934aa
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 6 deletions.
6 changes: 5 additions & 1 deletion cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,11 @@ func WithOsEnv(o *ProjectOptions) error {
// WithEnvFile set an alternate env file
// deprecated - use WithEnvFiles
func WithEnvFile(file string) ProjectOptionsFn {
return WithEnvFiles(file)
var files []string
if file != "" {
files = []string{file}
}
return WithEnvFiles(files...)
}

// WithEnvFiles set alternate env files
Expand Down
4 changes: 4 additions & 0 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,10 @@ func loadServiceWithExtends(filename, name string, servicesDict map[string]inter
}
baseService.Volumes[i].Source = resolveMaybeUnixPath(vol.Source, baseFileParent, lookupEnv)
}

for i, envFile := range baseService.EnvFile {
baseService.EnvFile[i] = resolveMaybeUnixPath(envFile, baseFileParent, lookupEnv)
}
}

serviceConfig, err = _merge(baseService, serviceConfig)
Expand Down
27 changes: 23 additions & 4 deletions loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1853,16 +1853,29 @@ func TestLoadWithExtends(t *testing.T) {
actual, err := Load(configDetails)
assert.NilError(t, err)

extendsDir, err := filepath.Abs(filepath.Join("testdata", "subdir"))
assert.NilError(t, err)

expectedEnvFilePath := filepath.Join(extendsDir, "extra.env")

expectedExtendsPath := filepath.Join(
extendsDir,
"compose-test-extends-imported.yaml",
)

expServices := types.Services{
{
Name: "importer",
Image: "nginx",
Extends: types.ExtendsConfig{
"file": strPtr("compose-test-extends-imported.yaml"),
"file": &expectedExtendsPath,
"service": strPtr("imported"),
},
Environment: types.MappingWithEquals{},
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
Environment: types.MappingWithEquals{
"SOURCE": strPtr("extends"),
},
EnvFile: []string{expectedEnvFilePath},
Networks: map[string]*types.ServiceNetworkConfig{"default": nil},
Volumes: []types.ServiceVolumeConfig{{
Type: "bind",
Source: "/opt/data",
Expand All @@ -1889,6 +1902,12 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
actual, err := Load(configDetails)
assert.NilError(t, err)

expectedExtendsPath, err := filepath.Abs(
filepath.Join(
"testdata",
"compose-test-extends-with-context-url-imported.yaml",
),
)
expServices := types.Services{
{
Name: "importer-with-https-url",
Expand All @@ -1897,7 +1916,7 @@ func TestLoadWithExtendsWithContextUrl(t *testing.T) {
Dockerfile: "Dockerfile",
},
Extends: types.ExtendsConfig{
"file": strPtr("compose-test-extends-with-context-url-imported.yaml"),
"file": &expectedExtendsPath,
"service": strPtr("imported-with-https-url"),
},
Environment: types.MappingWithEquals{},
Expand Down
5 changes: 5 additions & 0 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ func normalize(project *types.Project, resolvePaths bool) error {
}
s.Environment = s.Environment.Resolve(fn)

if extendFile := s.Extends["file"]; extendFile != nil && *extendFile != "" {
p := absPath(project.WorkingDir, *extendFile)
s.Extends["file"] = &p
}

err := relocateLogDriver(&s)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion loader/testdata/compose-test-extends.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
services:
importer:
extends:
file: compose-test-extends-imported.yaml
file: subdir/compose-test-extends-imported.yaml
service: imported
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
services:
imported:
image: nginx
env_file:
- extra.env
volumes:
- /opt/data:/var/lib/mysql
1 change: 1 addition & 0 deletions loader/testdata/subdir/extra.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SOURCE=extends

0 comments on commit 7a934aa

Please sign in to comment.