diff --git a/pkg/skaffold/build/bazel/build.go b/pkg/skaffold/build/bazel/build.go index 4e3b5713029..29d2930fdba 100644 --- a/pkg/skaffold/build/bazel/build.go +++ b/pkg/skaffold/build/bazel/build.go @@ -23,6 +23,7 @@ import ( "io" "os" "os/exec" + "path/filepath" "strings" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" @@ -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 { diff --git a/pkg/skaffold/build/bazel/build_test.go b/pkg/skaffold/build/bazel/build_test.go index 5e376582a5d..0fc4a7e1adb 100644 --- a/pkg/skaffold/build/bazel/build_test.go +++ b/pkg/skaffold/build/bazel/build_test.go @@ -18,7 +18,9 @@ package bazel import ( "context" + "fmt" "io/ioutil" + "path/filepath" "testing" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" @@ -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{ @@ -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{ @@ -70,11 +95,27 @@ 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"}, @@ -82,7 +123,8 @@ func TestBazelTarPath(t *testing.T) { }) 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) }) }