Skip to content

Commit

Permalink
Merge pull request #1340 from dgageot/fix-1319
Browse files Browse the repository at this point in the history
 Don’t assume bazel-bin is symlinked in workspace
  • Loading branch information
dgageot authored Nov 30, 2018
2 parents f437bfc + 1bdf3df commit 04431e2
Show file tree
Hide file tree
Showing 2 changed files with 73 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
54 changes: 54 additions & 0 deletions pkg/skaffold/build/local/bazel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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)
}

func TestBuildTarPath(t *testing.T) {
buildTarget := "//:skaffold_example.tar"

tarPath := buildTarPath(buildTarget)

testutil.CheckDeepEqual(t, "skaffold_example.tar", tarPath)
}

func TestBuildImageTag(t *testing.T) {
buildTarget := "//:skaffold_example.tar"

imageTag := buildImageTag(buildTarget)

testutil.CheckDeepEqual(t, ":skaffold_example", imageTag)
}

0 comments on commit 04431e2

Please sign in to comment.