Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bazel: support sub directories #2312

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 40 additions & 5 deletions pkg/skaffold/build/bazel/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func query(target string) string {

// GetDependencies finds the sources dependencies for the given bazel artifact.
// All paths are relative to the workspace.
func GetDependencies(ctx context.Context, workspace string, a *latest.BazelArtifact) ([]string, error) {
func GetDependencies(ctx context.Context, dir string, a *latest.BazelArtifact) ([]string, error) {
timer := time.NewTimer(1 * time.Second)
defer timer.Stop()

Expand All @@ -48,8 +48,18 @@ func GetDependencies(ctx context.Context, workspace string, a *latest.BazelArtif
logrus.Warnln("Retrieving Bazel dependencies can take a long time the first time")
}()

topLevelFolder, err := findWorkspace(dir)
if err != nil {
return nil, errors.Wrap(err, "unable to find the WORKSPACE file")
}

absDir, err := filepath.Abs(dir)
if err != nil {
return nil, errors.Wrapf(err, "unable to find absolute path for %s", dir)
}

cmd := exec.CommandContext(ctx, "bazel", "query", query(a.BuildTarget), "--noimplicit_deps", "--order_output=no")
cmd.Dir = workspace
cmd.Dir = dir
stdout, err := util.RunCmdOut(cmd)
if err != nil {
return nil, errors.Wrap(err, "getting bazel dependencies")
Expand All @@ -68,12 +78,18 @@ func GetDependencies(ctx context.Context, workspace string, a *latest.BazelArtif
continue
}

deps = append(deps, depToPath(l))
rel, err := filepath.Rel(absDir, filepath.Join(topLevelFolder, depToPath(l)))
if err != nil {
return nil, errors.Wrap(err, "unable to find absolute path")
}
deps = append(deps, rel)
}

if _, err := os.Stat(filepath.Join(workspace, "WORKSPACE")); err == nil {
deps = append(deps, "WORKSPACE")
rel, err := filepath.Rel(absDir, filepath.Join(topLevelFolder, "WORKSPACE"))
if err != nil {
return nil, errors.Wrap(err, "unable to find absolute path")
}
deps = append(deps, rel)

logrus.Debugf("Found dependencies for bazel artifact: %v", deps)

Expand All @@ -83,3 +99,22 @@ func GetDependencies(ctx context.Context, workspace string, a *latest.BazelArtif
func depToPath(dep string) string {
return strings.TrimPrefix(strings.Replace(strings.TrimPrefix(dep, "//"), ":", "/", 1), "/")
}

func findWorkspace(workingDir string) (string, error) {
dir, err := filepath.Abs(workingDir)
if err != nil {
return "", errors.Wrap(err, "invalid working dir")
}

for {
if _, err := os.Stat(filepath.Join(dir, "WORKSPACE")); err == nil {
return dir, nil
}

parent := filepath.Dir(dir)
if parent == dir {
return "", errors.New("no WORKSPACE file found")
}
dir = parent
}
}
51 changes: 36 additions & 15 deletions pkg/skaffold/build/bazel/dependencies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package bazel

import (
"context"
"path/filepath"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand All @@ -28,41 +29,61 @@ import (
func TestGetDependencies(t *testing.T) {
tests := []struct {
description string
workspace string
target string
files map[string]string
expectedQuery string
output string
expected []string
shouldErr bool
}{
{
description: "with workspace",
target: "target",
files: map[string]string{"WORKSPACE": "content is ignored"},
description: "with WORKSPACE",
workspace: ".",
target: "target",
files: map[string]string{
"WORKSPACE": "",
"BUILD": "",
"dep1": "",
"dep2": "",
},
expectedQuery: "bazel query kind('source file', deps('target')) union buildfiles('target') --noimplicit_deps --order_output=no",
output: "@ignored\n//external/ignored\n\n//:dep1\n//:dep2\n",
expected: []string{"dep1", "dep2", "WORKSPACE"},
output: "@ignored\n//:BUILD\n//external/ignored\n\n//:dep1\n//:dep2\n",
expected: []string{"BUILD", "dep1", "dep2", "WORKSPACE"},
},
{
description: "without workspace",
target: "target2",
files: map[string]string{},
description: "with parent WORKSPACE",
workspace: "./sub/folder",
target: "target2",
files: map[string]string{
"WORKSPACE": "",
"BUILD": "",
"sub/folder/BUILD": "",
"sub/folder/dep1": "",
"sub/folder/dep2": "",
"sub/folder/baz/dep3": "",
},
expectedQuery: "bazel query kind('source file', deps('target2')) union buildfiles('target2') --noimplicit_deps --order_output=no",
output: "@ignored\n//external/ignored\n\n//:dep3\n",
expected: []string{"dep3"},
output: "@ignored\n//:BUILD\n//sub/folder:BUILD\n//external/ignored\n\n//sub/folder:dep1\n//sub/folder:dep2\n//sub/folder/baz:dep3\n",
expected: []string{filepath.Join("..", "..", "BUILD"), "BUILD", "dep1", "dep2", filepath.Join("baz", "dep3"), filepath.Join("..", "..", "WORKSPACE")},
},
{
description: "without WORKSPACE",
workspace: ".",
target: "target",
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, t.FakeRunOut(test.expectedQuery, test.output))
tmpDir := t.NewTempDir().
WriteFiles(test.files)
t.NewTempDir().WriteFiles(test.files).Chdir()

deps, err := GetDependencies(context.Background(), tmpDir.Root(), &latest.BazelArtifact{
deps, err := GetDependencies(context.Background(), test.workspace, &latest.BazelArtifact{
BuildTarget: test.target,
})

t.CheckNoError(err)
t.CheckDeepEqual(test.expected, deps)
t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, deps)
})
}
}
Expand Down