Skip to content

Commit d01b706

Browse files
committed
Don’t assume bazel-bin is symlinked in workspace
Fix #1339 Signed-off-by: David Gageot <[email protected]>
1 parent 9eb0dfc commit d01b706

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

pkg/skaffold/build/local/bazel.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727

2828
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
2929
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
30+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
3031
"github.com/pkg/errors"
3132
)
3233

@@ -46,7 +47,12 @@ func (b *Builder) buildBazel(ctx context.Context, out io.Writer, workspace strin
4647
tarPath := buildTarPath(a.BuildTarget)
4748
imageTag := buildImageTag(a.BuildTarget)
4849

49-
imageTar, err := os.Open(filepath.Join(workspace, "bazel-bin", tarPath))
50+
bazelBin, err := bazelBin(ctx, workspace)
51+
if err != nil {
52+
return "", errors.Wrap(err, "getting path of bazel-bin")
53+
}
54+
55+
imageTar, err := os.Open(filepath.Join(bazelBin, tarPath))
5056
if err != nil {
5157
return "", errors.Wrap(err, "opening image tarball")
5258
}
@@ -66,6 +72,18 @@ func (b *Builder) buildBazel(ctx context.Context, out io.Writer, workspace strin
6672
return fmt.Sprintf("bazel%s", imageTag), nil
6773
}
6874

75+
func bazelBin(ctx context.Context, workspace string) (string, error) {
76+
cmd := exec.CommandContext(ctx, "bazel", "info", "bazel-bin")
77+
cmd.Dir = workspace
78+
79+
buf, err := util.RunCmdOut(cmd)
80+
if err != nil {
81+
return "", err
82+
}
83+
84+
return strings.TrimSpace(string(buf)), nil
85+
}
86+
6987
func trimTarget(buildTarget string) string {
7088
//TODO(r2d4): strip off leading //:, bad
7189
trimmedTarget := strings.TrimPrefix(buildTarget, "//")
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2018 The Skaffold Authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package local
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
24+
"github.com/GoogleContainerTools/skaffold/testutil"
25+
)
26+
27+
func TestBazelBin(t *testing.T) {
28+
defer func(c util.Command) { util.DefaultExecCommand = c }(util.DefaultExecCommand)
29+
util.DefaultExecCommand = testutil.NewFakeCmdOut(
30+
"bazel info bazel-bin",
31+
"/absolute/path/bin\n",
32+
nil,
33+
)
34+
35+
bazelBin, err := bazelBin(context.Background(), ".")
36+
37+
testutil.CheckErrorAndDeepEqual(t, false, err, "/absolute/path/bin", bazelBin)
38+
}

0 commit comments

Comments
 (0)