Skip to content

Commit

Permalink
dockercompose: greenlight dc version 2.2 or later (#5254)
Browse files Browse the repository at this point in the history
* dockercompose: greenlight dc version 2.2 or later

Fixes #4890.

* wordsmith: verbs

* dockercompose: remove TestPreferComposeV1

* dockercompose: rid v1 in variable name

* wordsmith: more english/simplify error message
  • Loading branch information
nicksieger committed Dec 6, 2021
1 parent 96f2496 commit db5135a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 50 deletions.
6 changes: 3 additions & 3 deletions internal/dockercompose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,11 @@ func dcLoaderOption(opts *loader.Options) {
}

func dcExecutablePath() string {
v1Name := "docker-compose-v1"
composeName := "docker-compose"
if runtime.GOOS == "windows" {
v1Name += ".exe"
composeName += ".exe"
}
composePath, err := exec.LookPath(v1Name)
composePath, err := exec.LookPath(composeName)
if err != nil {
composePath = "docker-compose"
}
Expand Down
30 changes: 0 additions & 30 deletions internal/dockercompose/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package dockercompose

import (
"context"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/compose-spec/compose-go/types"
Expand Down Expand Up @@ -71,33 +68,6 @@ func TestVariableInterpolation(t *testing.T) {
}
}

func TestPreferComposeV1(t *testing.T) {
t.Run("v1 Symlink Exists", func(t *testing.T) {
tmpdir := t.TempDir()
v1Name := "docker-compose-v1"
if runtime.GOOS == "windows" {
v1Name += ".exe"
}
binPath := filepath.Join(tmpdir, v1Name)
require.NoError(t, os.WriteFile(binPath, nil, 0777),
"Failed to create fake docker-compose-v1 binary")

testutils.Setenv(t, "PATH", tmpdir)
cli, ok := NewDockerComposeClient(docker.LocalEnv{}).(*cmdDCClient)
require.True(t, ok, "Unexpected type for Compose client: %T", cli)
assert.Equal(t, binPath, cli.composePath)
})

t.Run("No v1 Symlink Exists", func(t *testing.T) {
testutils.Unsetenv(t, "PATH")
cli, ok := NewDockerComposeClient(docker.LocalEnv{}).(*cmdDCClient)
require.True(t, ok, "Unexpected type for Compose client: %T", cli)
// if docker-compose-v1 isn't in path, we just set the path to the unqualified binary name and let it get
// resolved at exec time
assert.Equal(t, "docker-compose", cli.composePath)
})
}

func TestParseComposeVersionOutput(t *testing.T) {
type tc struct {
version string
Expand Down
5 changes: 3 additions & 2 deletions internal/tiltfile/tiltfile_docker_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,9 +764,10 @@ func TestDockerComposeVersionWarnings(t *testing.T) {
}
tcs := []tc{
{version: "v1.28.0", error: "Tilt requires Docker Compose v1.28.3+ (you have v1.28.0). Please upgrade and re-launch Tilt."},
{version: "v2.0.0-rc.3", warning: "Support for Docker Compose v2.x is experimental, and you might encounter errors or broken functionality.\n" +
"For best results, we recommend using Docker Compose v1.x with Tilt."},
{version: "v2.0.0-rc.3", warning: "Using Docker Compose v2.0.0-rc.3 (version < 2.2) may result in errors or broken functionality.\n" +
"For best results, we recommend upgrading to Docker Compose >= v2.2.0."},
{version: "v1.29.2" /* no errors or warnings */},
{version: "v2.2.0" /* no errors or warnings */},
{version: "v1.99.0-beta.4", warning: "You are running a pre-release version of Docker Compose (v1.99.0-beta.4), which is unsupported.\n" +
"You might encounter errors or broken functionality."},
}
Expand Down
18 changes: 3 additions & 15 deletions internal/tiltfile/tiltfile_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"path/filepath"
"runtime"
"strings"
"time"

Expand Down Expand Up @@ -1349,20 +1348,9 @@ func (s *tiltfileState) validateDockerComposeVersion() error {
"Tilt requires Docker Compose %s+ (you have %s). Please upgrade and re-launch Tilt.",
minimumDockerComposeVersion,
dcVersion)
} else if semver.Major(dcVersion) == "v2" {
var downgradeInstructions string
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
// `docker-compose` on Docker Desktop is a shim that conditionally execs either v1 or v2
downgradeInstructions = "Run `docker-compose disable-v2` and re-launch Tilt."
} else {
// Compose v2 on Linux is a Docker plugin, used by running "docker compose" (notice the lack of hyphen -
// it's a subcommand found by searching `~/.docker/cli-plugins`), so if `docker-compose` points to v2,
// there must be a custom symlink; this will likely become more common over time as users want to
// maintain compatibility with existing tooling
downgradeInstructions = "Ensure `docker-compose` in your PATH points to Compose v1 and re-launch Tilt.\n"
}
logger.Get(s.ctx).Warnf("Support for Docker Compose v2.x is experimental, and you might encounter errors or broken functionality.\n"+
"For best results, we recommend using Docker Compose v1.x with Tilt.\n%s", downgradeInstructions)
} else if semver.Major(dcVersion) == "v2" && semver.Compare(dcVersion, "v2.2") < 0 {
logger.Get(s.ctx).Warnf("Using Docker Compose %s (version < 2.2) may result in errors or broken functionality.\n"+
"For best results, we recommend upgrading to Docker Compose >= v2.2.0.", dcVersion)
} else if semver.Prerelease(dcVersion) != "" {
logger.Get(s.ctx).Warnf("You are running a pre-release version of Docker Compose (%s), which is unsupported.\n"+
"You might encounter errors or broken functionality.", dcVersion)
Expand Down

0 comments on commit db5135a

Please sign in to comment.