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

unused_deps: Support BUILD.bazel files #728

Merged
merged 12 commits into from
Oct 9, 2019
16 changes: 12 additions & 4 deletions edit/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,21 +136,29 @@ func InterpretLabelForWorkspaceLocation(root string, target string) (buildFile s
}

if strings.HasPrefix(target, "//") {
buildFile = path.Join(rootDir, pkg, "BUILD")
buildFile = filepath.Join(rootDir, pkg, "BUILD")
if !isFile(buildFile) {
// try it with the .bazel extension
buildFile += ".bazel"
}
return
}
if isFile(pkg) {
// allow operation on other files like WORKSPACE
buildFile = pkg
pkg = path.Join(relativePath, filepath.Dir(pkg))
pkg = filepath.Join(relativePath, filepath.Dir(pkg))
return
}
if pkg != "" {
buildFile = pkg + "/BUILD"
buildFile = filepath.Join(pkg, "/BUILD")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, but now the tests fail on Windows, you probably need to update them by replacing a slash with filepath.Separator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, shouldn't it be "BUILD" instead of "/BUILD"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only need to remove the / because Join already adds that. (I don't have windows so I only notice it from the buildkite run. Getting this to pass on windows is a bit tricky, I'll notify you again once I have it figured out.

I think the gist of it is that ParseLabel does not work for absolute windows paths. It was only tested on unix-style paths:

{"/abs/path/to/WORKSPACE:rule", "", "/abs/path/to/WORKSPACE", "rule"},
. The reason is that the code simply splits on the :, which is a part of the path on windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's probably the reason, at least it can explain the following error: https://storage.googleapis.com/bazel-untrusted-buildkite-artifacts/bf49dbeb-eb10-4ee6-bbdc-669c127fbc3f/edit%5Cgo_default_test%5Cattempt_1.log

Feel free to just revert the change to this line if you don't want to fix it now, it's not directly relevant to your pull request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another suggestion is to ignore this issue for now, remove the last two test cases, and create a new issue for fixing the parsing issues on windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the unrelated change and also removed the test cases (that I added) which are failing on windows. I can add a new ticket for these issues afterwards if you'd like that.

Also: please let me know if I should rebase/squash this once you're done with the review. I'm not familiar with how you handle that on this project.

} else {
buildFile = "BUILD"
}
pkg = path.Join(relativePath, pkg)
if !isFile(buildFile) {
// try it with the .bazel extension
buildFile += ".bazel"
}
pkg = filepath.Join(relativePath, pkg)
return
}

Expand Down
49 changes: 49 additions & 0 deletions edit/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ package edit

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"reflect"
"regexp"
"strings"
Expand Down Expand Up @@ -673,3 +676,49 @@ x = 2`,
}
}
}

type testCase struct {
inputRoot, inputTarget string
expectedBuildFile, expectedPkg, expectedRule string
}

func runTestInterpretLabelForWorkspaceLocation(t *testing.T, buildFileName string) {
tmp, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmp)
if err := os.MkdirAll(filepath.Join(tmp, "a", "b"), 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(tmp, "WORKSPACE"), nil, 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(tmp, buildFileName), nil, 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(tmp, "a", buildFileName), nil, 0755); err != nil {
t.Fatal(err)
}
if err := ioutil.WriteFile(filepath.Join(tmp, "a", "b", buildFileName), nil, 0755); err != nil {
t.Fatal(err)
}

for _, tc := range []testCase{
{tmp, "//", filepath.Join(tmp, buildFileName), "", "."},
{tmp, "//a", filepath.Join(tmp, "a", buildFileName), "a", "a"},
{tmp, "//a:a", filepath.Join(tmp, "a", buildFileName), "a", "a"},
{tmp, "//a/b", filepath.Join(tmp, "a", "b", buildFileName), "a/b", "b"},
{tmp, "//a/b:b", filepath.Join(tmp, "a", "b", buildFileName), "a/b", "b"},
} {
buildFile, pkg, rule := InterpretLabelForWorkspaceLocation(tc.inputRoot, tc.inputTarget)
if buildFile != tc.expectedBuildFile || pkg != tc.expectedPkg || rule != tc.expectedRule {
t.Errorf("InterpretLabelForWorkspaceLocation(%q, %q) = %q, %q, %q; want %q, %q, %q", tc.inputRoot, tc.inputTarget, buildFile, pkg, rule, tc.expectedBuildFile, tc.expectedPkg, tc.expectedRule)
}
}
}

func TestInterpretLabelForWorkspaceLocation(t *testing.T) {
runTestInterpretLabelForWorkspaceLocation(t, "BUILD")
runTestInterpretLabelForWorkspaceLocation(t, "BUILD.bazel")
}