Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: backport Bazel build context fixes to v1 #7866

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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