diff --git a/pkg/skaffold/build/local/bazel.go b/pkg/skaffold/build/local/bazel.go index 47c8f1f6fdf..205a93faea0 100644 --- a/pkg/skaffold/build/local/bazel.go +++ b/pkg/skaffold/build/local/bazel.go @@ -27,6 +27,7 @@ import ( "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" "github.com/pkg/errors" ) @@ -46,7 +47,12 @@ func (b *Builder) buildBazel(ctx context.Context, out io.Writer, workspace strin tarPath := buildTarPath(a.BuildTarget) imageTag := buildImageTag(a.BuildTarget) - imageTar, err := os.Open(filepath.Join(workspace, "bazel-bin", tarPath)) + bazelBin, err := bazelBin(ctx, workspace) + if err != nil { + return "", errors.Wrap(err, "getting path of bazel-bin") + } + + imageTar, err := os.Open(filepath.Join(bazelBin, tarPath)) if err != nil { return "", errors.Wrap(err, "opening image tarball") } @@ -66,6 +72,18 @@ func (b *Builder) buildBazel(ctx context.Context, out io.Writer, workspace strin return fmt.Sprintf("bazel%s", imageTag), nil } +func bazelBin(ctx context.Context, workspace string) (string, error) { + cmd := exec.CommandContext(ctx, "bazel", "info", "bazel-bin") + cmd.Dir = workspace + + buf, err := util.RunCmdOut(cmd) + if err != nil { + return "", err + } + + return strings.TrimSpace(string(buf)), nil +} + func trimTarget(buildTarget string) string { //TODO(r2d4): strip off leading //:, bad trimmedTarget := strings.TrimPrefix(buildTarget, "//") diff --git a/pkg/skaffold/build/local/bazel_test.go b/pkg/skaffold/build/local/bazel_test.go new file mode 100644 index 00000000000..5536e2c3039 --- /dev/null +++ b/pkg/skaffold/build/local/bazel_test.go @@ -0,0 +1,38 @@ +/* +Copyright 2018 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package local + +import ( + "context" + "testing" + + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util" + "github.com/GoogleContainerTools/skaffold/testutil" +) + +func TestBazelBin(t *testing.T) { + defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand) + util.DefaultExecCommand = testutil.NewFakeCmdOut( + "bazel info bazel-bin", + "/absolute/path/bin\n", + nil, + ) + + bazelBin, err := bazelBin(context.Background(), ".") + + testutil.CheckErrorAndDeepEqual(t, false, err, "/absolute/path/bin", bazelBin) +}