Skip to content

Commit

Permalink
fix: backport Bazel build context fixes to v1 (GoogleContainerTools#7866
Browse files Browse the repository at this point in the history
)

* fix: running skaffold from a directory other than repository root (GoogleContainerTools#7430)

* use absolute paths to the container_image.tar in bazel build. (GoogleContainerTools#7671)

* fix: use absolute paths to the container_image.tar in bazel build.

* fix unit tests

Co-authored-by: tejal29 <tejal29@gmail.com>

Co-authored-by: Sergei Morozov <morozov@tut.by>
Co-authored-by: Aaron Son <aaron@dolthub.com>
Co-authored-by: tejal29 <tejal29@gmail.com>
  • Loading branch information
4 people authored Sep 21, 2022
1 parent 7ed1d72 commit 894fb67
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
15 changes: 14 additions & 1 deletion pkg/skaffold/build/bazel/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"io"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
Expand Down Expand Up @@ -128,7 +129,19 @@ func bazelTarPath(ctx context.Context, workspace string, a *latest.BazelArtifact
return "", err
}

return strings.TrimSpace(string(buf)), nil
targetPath := strings.TrimSpace(string(buf))

cmd = exec.CommandContext(ctx, "bazel", "info", "execution_root")
cmd.Dir = workspace

buf, err = util.RunCmdOut(ctx, cmd)
if err != nil {
return "", err
}

execRoot := strings.TrimSpace(string(buf))

return filepath.Join(execRoot, targetPath), nil
}

func trimTarget(buildTarget string) string {
Expand Down
52 changes: 47 additions & 5 deletions pkg/skaffold/build/bazel/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package bazel

import (
"context"
"fmt"
"io/ioutil"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
Expand All @@ -33,7 +35,7 @@ func TestBuildBazel(t *testing.T) {
t.NewTempDir().Mkdir("bin").Chdir()
t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
"bin/app.tar"))
"bin/app.tar").AndRunOut("bazel info execution_root", ""))
testutil.CreateFakeImageTar("bazel:app", "bin/app.tar")

artifact := &latest.Artifact{
Expand All @@ -52,6 +54,29 @@ func TestBuildBazel(t *testing.T) {
})
}

func TestBazelTarPathPrependExecutionRoot(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, testutil.CmdRun("bazel build //:app.tar --color=no").AndRunOut(
"bazel cquery //:app.tar --output starlark --starlark:expr target.files.to_list()[0].path",
"app.tar").AndRunOut("bazel info execution_root", ".."))
testutil.CreateFakeImageTar("bazel:app", "../app.tar")

artifact := &latest.Artifact{
Workspace: "..",
ArtifactType: latest.ArtifactType{
BazelArtifact: &latest.BazelArtifact{
BuildTarget: "//:app.tar",
},
},
}

builder := NewArtifactBuilder(fakeLocalDaemon(), &mockConfig{}, false)
_, err := builder.Build(context.Background(), ioutil.Discard, artifact, "img:tag", platform.Matcher{})

t.CheckNoError(err)
})
}

func TestBuildBazelFailInvalidTarget(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
artifact := &latest.Artifact{
Expand All @@ -70,19 +95,36 @@ func TestBuildBazelFailInvalidTarget(t *testing.T) {
}

func TestBazelTarPath(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
testutil.Run(t, "EmptyExecutionRoot", func(t *testutil.T) {
osSpecificPath := filepath.Join("absolute", "path", "bin")
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
fmt.Sprintf("%s\n", osSpecificPath),
).AndRunOut("bazel info execution_root", ""))

bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
BuildArgs: []string{"--arg1", "--arg2"},
BuildTarget: "//:skaffold_example.tar",
})

t.CheckNoError(err)
t.CheckDeepEqual(osSpecificPath, bazelBin)
})
testutil.Run(t, "AbsoluteExecutionRoot", func(t *testutil.T) {
osSpecificPath := filepath.Join("var", "tmp", "bazel-execution-roots", "abcdefg", "execroot", "workspace_name")
t.Override(&util.DefaultExecCommand, testutil.CmdRunOut(
"bazel cquery //:skaffold_example.tar --output starlark --starlark:expr target.files.to_list()[0].path --arg1 --arg2",
"/absolute/path/bin\n",
))
"bazel-bin/darwin-fastbuild-ST-confighash/path/to/bin\n",
).AndRunOut("bazel info execution_root", osSpecificPath))

bazelBin, err := bazelTarPath(context.Background(), ".", &latest.BazelArtifact{
BuildArgs: []string{"--arg1", "--arg2"},
BuildTarget: "//:skaffold_example.tar",
})

t.CheckNoError(err)
t.CheckDeepEqual("/absolute/path/bin", bazelBin)
expected := filepath.Join(osSpecificPath, "bazel-bin", "darwin-fastbuild-ST-confighash", "path", "to", "bin")
t.CheckDeepEqual(expected, bazelBin)
})
}

Expand Down

0 comments on commit 894fb67

Please sign in to comment.