Skip to content

Commit

Permalink
fix 'failed to get console mode' errors on Windows
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Feb 2, 2024
1 parent 2c1f464 commit b057f28
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cmd/buildx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cliflags "github.com/docker/cli/cli/flags"
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/util/stack"
"github.com/moby/term"

//nolint:staticcheck // vendored dependencies may still use this
"github.com/containerd/containerd/pkg/seed"
Expand Down Expand Up @@ -88,7 +89,7 @@ func main() {
fmt.Fprintf(cmd.Err(), "ERROR: %v\n", err)
}
if ebr, ok := err.(*desktop.ErrorWithBuildRef); ok {
ebr.Print(cmd.Err())
ebr.Print(cmd.Err(), term.IsTerminal(os.Stderr.Fd()))
}

os.Exit(1)
Expand Down
9 changes: 2 additions & 7 deletions commands/bake.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"os"
"strings"

"github.com/containerd/console"
"github.com/containerd/containerd/platforms"
"github.com/docker/buildx/bake"
"github.com/docker/buildx/build"
Expand All @@ -24,6 +23,7 @@ import (
"github.com/docker/cli/cli/command"
"github.com/moby/buildkit/identity"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/moby/term"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -119,11 +119,6 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
progressTextDesc = fmt.Sprintf("building with %q instance using %s driver", b.Name, b.Driver)
}

var term bool
if _, err := console.ConsoleFromFile(os.Stderr); err == nil {
term = true
}

progressMode := progressui.DisplayMode(cFlags.progress)
printer, err := progress.NewPrinter(ctx2, os.Stderr, progressMode,
progress.WithDesc(progressTextDesc, progressConsoleDesc),
Expand All @@ -139,7 +134,7 @@ func runBake(ctx context.Context, dockerCli command.Cli, targets []string, in ba
err = err1
}
if err == nil && progressMode != progressui.QuietMode {
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term.IsTerminal(os.Stderr.Fd()))
}
}
}()
Expand Down
8 changes: 2 additions & 6 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/moby/buildkit/solver/errdefs"
"github.com/moby/buildkit/util/grpcerrors"
"github.com/moby/buildkit/util/progress/progressui"
"github.com/moby/term"
"github.com/morikuni/aec"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -254,11 +255,6 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
return err
}

var term bool
if _, err := console.ConsoleFromFile(os.Stderr); err == nil {
term = true
}

ctx2, cancel := context.WithCancel(context.TODO())
defer cancel()
progressMode, err := options.toDisplayMode()
Expand Down Expand Up @@ -295,7 +291,7 @@ func runBuild(ctx context.Context, dockerCli command.Cli, options buildOptions)
}

if progressMode != progressui.QuietMode {
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term)
desktop.PrintBuildDetails(os.Stderr, printer.BuildRefs(), term.IsTerminal(os.Stderr.Fd()))
} else {
fmt.Println(getImageID(resp.ExporterResponse))
}
Expand Down
4 changes: 2 additions & 2 deletions controller/pb/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"strconv"

"github.com/containerd/console"
"github.com/moby/buildkit/client"
"github.com/moby/term"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -68,7 +68,7 @@ func CreateExports(entries []*ExportEntry) ([]client.ExportEntry, error) {
entry.Destination = "-"
}
if entry.Destination == "-" {
if _, err := console.ConsoleFromFile(os.Stdout); err == nil {
if term.IsTerminal(os.Stdout.Fd()) {
return nil, errors.Errorf("dest file is required for %s exporter. refusing to write to console", out.Type)
}
out.Output = wrapWriteCloser(os.Stdout)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/moby/buildkit v0.13.0-beta1.0.20240126101002-6bd81372ad6f // master (v0.13.0-dev)
github.com/moby/sys/mountinfo v0.7.1
github.com/moby/sys/signal v0.7.0
github.com/moby/term v0.5.0
github.com/morikuni/aec v1.0.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0-rc5
Expand Down Expand Up @@ -123,7 +124,6 @@ require (
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
Expand Down
8 changes: 1 addition & 7 deletions util/desktop/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"os"
"path/filepath"
"sync"

"github.com/containerd/console"
)

var (
Expand Down Expand Up @@ -76,11 +74,7 @@ func (e *ErrorWithBuildRef) Unwrap() error {
return e.Err
}

func (e *ErrorWithBuildRef) Print(w io.Writer) error {
var term bool
if _, err := console.ConsoleFromFile(os.Stderr); err == nil {
term = true
}
func (e *ErrorWithBuildRef) Print(w io.Writer, term bool) error {
fmt.Fprintf(w, "\n%s\n", BuildDetailsOutput(map[string]string{"default": e.Ref}, term))
return nil
}

0 comments on commit b057f28

Please sign in to comment.