Skip to content

Commit bec8ddd

Browse files
committed
let resourceloader compute relative working dir
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent e2eab1f commit bec8ddd

File tree

5 files changed

+57
-28
lines changed

5 files changed

+57
-28
lines changed

loader/include.go

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -60,43 +60,41 @@ func ApplyInclude(ctx context.Context, configDetails types.ConfigDetails, model
6060
})
6161
}
6262

63+
var relworkingdir string
6364
for i, p := range r.Path {
6465
for _, loader := range options.ResourceLoaders {
65-
if loader.Accept(p) {
66-
path, err := loader.Load(ctx, p)
67-
if err != nil {
68-
return err
66+
if !loader.Accept(p) {
67+
continue
68+
}
69+
path, err := loader.Load(ctx, p)
70+
if err != nil {
71+
return err
72+
}
73+
p = path
74+
75+
if i == 0 { // This is the "main" file, used to define project-directory. Others are overrides
76+
relworkingdir = loader.Dir(path)
77+
if r.ProjectDirectory == "" {
78+
r.ProjectDirectory = filepath.Dir(path)
79+
}
80+
81+
for _, f := range included {
82+
if f == path {
83+
included = append(included, path)
84+
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
85+
}
6986
}
70-
p = path
71-
break
7287
}
7388
}
7489
r.Path[i] = p
7590
}
7691

77-
mainFile := r.Path[0]
78-
for _, f := range included {
79-
if f == mainFile {
80-
included = append(included, mainFile)
81-
return fmt.Errorf("include cycle detected:\n%s\n include %s", included[0], strings.Join(included[1:], "\n include "))
82-
}
83-
}
84-
85-
if r.ProjectDirectory == "" {
86-
r.ProjectDirectory = filepath.Dir(mainFile)
87-
}
88-
relworkingdir, err := filepath.Rel(configDetails.WorkingDir, r.ProjectDirectory)
89-
if err != nil {
90-
// included file path is not inside project working directory => use absolute path
91-
relworkingdir = r.ProjectDirectory
92-
}
93-
9492
loadOptions := options.clone()
9593
loadOptions.ResolvePaths = true
9694
loadOptions.SkipNormalization = true
9795
loadOptions.SkipConsistencyCheck = true
9896
loadOptions.ResourceLoaders = append(loadOptions.RemoteResourceLoaders(), localResourceLoader{
99-
WorkingDir: relworkingdir,
97+
WorkingDir: r.ProjectDirectory,
10098
})
10199

102100
if len(r.EnvFile) == 0 {

loader/include_test.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,21 @@ import (
2626
"gotest.tools/v3/assert"
2727
)
2828

29+
func TestLoadIncludeExtendsCombined(t *testing.T) {
30+
_, err := LoadWithContext(context.Background(), types.ConfigDetails{
31+
WorkingDir: "testdata/combined",
32+
ConfigFiles: []types.ConfigFile{
33+
{
34+
Filename: "testdata/combined/compose.yaml",
35+
},
36+
},
37+
}, withProjectName("test-load-combined", true))
38+
assert.NilError(t, err)
39+
}
40+
2941
func TestLoadWithMultipleInclude(t *testing.T) {
3042
// include same service twice should not trigger an error
31-
p, err := Load(buildConfigDetails(`
43+
details := buildConfigDetails(`
3244
name: 'test-multi-include'
3345
3446
include:
@@ -41,17 +53,21 @@ services:
4153
image: busybox
4254
depends_on:
4355
- imported
44-
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
56+
`, map[string]string{"SOURCE": "override"})
57+
58+
p, err := Load(details, func(options *Options) {
4559
options.SkipNormalization = true
4660
options.ResolvePaths = true
4761
})
4862
assert.NilError(t, err)
4963
imported, err := p.GetService("imported")
5064
assert.NilError(t, err)
5165
assert.Equal(t, imported.ContainerName, "override")
66+
}
5267

68+
func TestLoadWithMultipleIncludeConflict(t *testing.T) {
5369
// include 2 different services with same name should trigger an error
54-
_, err = Load(buildConfigDetails(`
70+
details := buildConfigDetails(`
5571
name: 'test-multi-include'
5672
5773
include:
@@ -64,7 +80,8 @@ include:
6480
services:
6581
bar:
6682
image: busybox
67-
`, map[string]string{"SOURCE": "override"}), func(options *Options) {
83+
`, map[string]string{"SOURCE": "override"})
84+
_, err := Load(details, func(options *Options) {
6885
options.SkipNormalization = true
6986
options.ResolvePaths = true
7087
})

loader/testdata/combined/compose.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
include:
2+
- path:
3+
- dir/included.yaml
4+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
services:
2+
service:
3+
build: .
4+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
services:
2+
service:
3+
extends:
4+
file: extended.yaml
5+
service: service
6+

0 commit comments

Comments
 (0)