Skip to content

Commit

Permalink
loader: restore implicit default of . for build context
Browse files Browse the repository at this point in the history
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 <milas.bowman@docker.com>
  • Loading branch information
milas committed Jun 28, 2023
1 parent 169d13b commit 4719844
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
14 changes: 7 additions & 7 deletions loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,13 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
Extensions: model.Extensions,
}

if !opts.SkipNormalization {
err = Normalize(project)
if err != nil {
return nil, err
}
}

if opts.ResolvePaths {
err = ResolveRelativePaths(project)
if err != nil {
Expand All @@ -277,13 +284,6 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types.
}
}

if !opts.SkipNormalization {
err = Normalize(project)
if err != nil {
return nil, err
}
}

if !opts.SkipConsistencyCheck {
err = checkConsistency(project)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion loader/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1928,7 +1928,8 @@ func TestLoadWithExtends(t *testing.T) {

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

expectedEnvFilePath := filepath.Join(extendsDir, "extra.env")
expectedEnvFilePath, err := filepath.Abs(filepath.Join(extendsDir, "extra.env"))
assert.NilError(t, err)

expServices := types.Services{
{
Expand Down
7 changes: 5 additions & 2 deletions loader/normalize.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (

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

err = relocateExternalName(project)
if err != nil {
if err := relocateExternalName(project); err != nil {
return err
}

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

if s.Build != nil {
if s.Build.Context == "" {
s.Build.Context = "."
}
if s.Build.Dockerfile == "" && s.Build.DockerfileInline == "" {
s.Build.Dockerfile = "Dockerfile"
}
Expand Down
14 changes: 14 additions & 0 deletions loader/normalize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,17 @@ func TestNormalizeImplicitDependencies(t *testing.T) {
assert.NilError(t, err)
assert.DeepEqual(t, expected, project.Services[0].DependsOn)
}

func TestImplicitContextPath(t *testing.T) {
project := &types.Project{
Name: "myProject",
Services: types.Services{
types.ServiceConfig{
Name: "test",
Build: &types.BuildConfig{},
},
},
}
assert.NilError(t, Normalize(project))
assert.Equal(t, ".", project.Services[0].Build.Context)
}

0 comments on commit 4719844

Please sign in to comment.