Skip to content

Commit 4719844

Browse files
committed
loader: restore implicit default of . for build context
In the Compose spec, as well as Compose v1.x, the `build.context` field is mandatory in the object form of a `service.build` definition. However, in compose-go (and thus Compose v2.x), this has never been enforced, and an empty `build.context` field was implicitly set to `.` aka the project directory. Restore that behavior pending a decision on whether we want to make the spec less restrictive here so that this is acceptable or change `compose-go` to begin emitting a warning and eventually reject this. Note that the order of normalization and resolving paths has been switched so that normalization occurs first, and then paths can be resolved across all fields consistently. This resulted in a small test update where this was incorrect before - it was loading with path resolution enabled but then asserting it had a relative path for the env file. Signed-off-by: Milas Bowman <[email protected]>
1 parent 169d13b commit 4719844

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

loader/loader.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,13 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
261261
Extensions: model.Extensions,
262262
}
263263

264+
if !opts.SkipNormalization {
265+
err = Normalize(project)
266+
if err != nil {
267+
return nil, err
268+
}
269+
}
270+
264271
if opts.ResolvePaths {
265272
err = ResolveRelativePaths(project)
266273
if err != nil {
@@ -277,13 +284,6 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
277284
}
278285
}
279286

280-
if !opts.SkipNormalization {
281-
err = Normalize(project)
282-
if err != nil {
283-
return nil, err
284-
}
285-
}
286-
287287
if !opts.SkipConsistencyCheck {
288288
err = checkConsistency(project)
289289
if err != nil {

loader/loader_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,8 @@ func TestLoadWithExtends(t *testing.T) {
19281928

19291929
extendsDir := filepath.Join("testdata", "subdir")
19301930

1931-
expectedEnvFilePath := filepath.Join(extendsDir, "extra.env")
1931+
expectedEnvFilePath, err := filepath.Abs(filepath.Join(extendsDir, "extra.env"))
1932+
assert.NilError(t, err)
19321933

19331934
expServices := types.Services{
19341935
{

loader/normalize.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929

3030
// Normalize compose project by moving deprecated attributes to their canonical position and injecting implicit defaults
3131
func Normalize(project *types.Project) error {
32+
// TODO(milas): this really belongs in ResolveRelativePaths
3233
absWorkingDir, err := filepath.Abs(project.WorkingDir)
3334
if err != nil {
3435
return err
@@ -44,8 +45,7 @@ func Normalize(project *types.Project) error {
4445
project.Networks["default"] = types.NetworkConfig{}
4546
}
4647

47-
err = relocateExternalName(project)
48-
if err != nil {
48+
if err := relocateExternalName(project); err != nil {
4949
return err
5050
}
5151

@@ -65,6 +65,9 @@ func Normalize(project *types.Project) error {
6565
}
6666

6767
if s.Build != nil {
68+
if s.Build.Context == "" {
69+
s.Build.Context = "."
70+
}
6871
if s.Build.Dockerfile == "" && s.Build.DockerfileInline == "" {
6972
s.Build.Dockerfile = "Dockerfile"
7073
}

loader/normalize_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,17 @@ func TestNormalizeImplicitDependencies(t *testing.T) {
218218
assert.NilError(t, err)
219219
assert.DeepEqual(t, expected, project.Services[0].DependsOn)
220220
}
221+
222+
func TestImplicitContextPath(t *testing.T) {
223+
project := &types.Project{
224+
Name: "myProject",
225+
Services: types.Services{
226+
types.ServiceConfig{
227+
Name: "test",
228+
Build: &types.BuildConfig{},
229+
},
230+
},
231+
}
232+
assert.NilError(t, Normalize(project))
233+
assert.Equal(t, ".", project.Services[0].Build.Context)
234+
}

0 commit comments

Comments
 (0)