Skip to content

Commit

Permalink
Don’t assume bazel-bin is symlinked in workspace
Browse files Browse the repository at this point in the history
Fix #1339

Signed-off-by: David Gageot <david@gageot.net>
  • Loading branch information
dgageot committed Nov 30, 2018
1 parent 9eb0dfc commit d01b706
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pkg/skaffold/build/local/bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
}
Expand All @@ -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, "//")
Expand Down
38 changes: 38 additions & 0 deletions pkg/skaffold/build/local/bazel_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit d01b706

Please sign in to comment.