Skip to content

Commit

Permalink
Expand the set of file names considered to be build files. (bazelbuil…
Browse files Browse the repository at this point in the history
…d#400)

When using Bazel external workspaces, it's common to have files named like something.BUILD or BUILD.something, which become the BUILD file for the @something repository. This CL modifies build.ParseDefault so that it considers any filename with an extension or base called "BUILD" to be a BUILD file. While workspace file naming in the wild is less creative, I didn't see a reason not to extend the same rule to workspace files.
  • Loading branch information
benjaminp authored and vladmos committed Nov 15, 2018
1 parent 186dcfc commit 0baf2db
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
2 changes: 2 additions & 0 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ go_test(
size = "small",
srcs = [
"checkfile_test.go",
"lex_test.go",
"parse_test.go",
"print_test.go",
"quote_test.go",
Expand All @@ -44,6 +45,7 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//tables:go_default_library",
"//testutils",
],
)
21 changes: 15 additions & 6 deletions build/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,7 @@ import (

// BuildFilenames is a collection of filenames in lowercase that are treated as BUILD files.
var BuildFilenames = map[string]bool{
"build": true,
"build.bazel": true,
"workspace": true,
"workspace.bazel": true,
"stdin": true,
"stdin": true,
}

// ParseBuild parses a file, marks it as a BUILD file and returns the corresponding parse tree.
Expand All @@ -59,13 +55,26 @@ func ParseDefault(filename string, data []byte) (*File, error) {
return f, err
}

func isBuildFilename(basename string) bool {
basename = strings.ToLower(basename)
if isBuild, ok := BuildFilenames[basename]; ok {
return isBuild
}
ext := filepath.Ext(basename)
if ext == ".bzl" || ext == ".sky" {
return false
}
base := basename[:len(basename)-len(ext)]
return ext == ".build" || ext == ".workspace" || base == "build" || base == "workspace"
}

// Parse parses the input data and returns the corresponding parse tree.
//
// Uses the filename to detect the formatting type (either build or default) and calls
// either ParseBuild or to ParseDefault correspondingly.
func Parse(filename string, data []byte) (*File, error) {
basename := filepath.Base(filename)
if BuildFilenames[strings.ToLower(basename)] {
if isBuildFilename(basename) {
return ParseBuild(filename, data)
}
return ParseDefault(filename, data)
Expand Down
46 changes: 46 additions & 0 deletions build/lex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2018 Bazel contributors. All Rights Reserved.
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 build

import (
"testing"
)

func TestIsBuildFilename(t *testing.T) {
cases := map[string]bool{
"BUILD": true,
"build": true,
"bUIld": true,
"BUILD.bazel": true,
"build.bzl": false,
"build.sky": false,
"WORKSPACE": true,
"external.BUILD": true,
"BUILD.external": true,
"aBUILD": false,
"thing.sky": false,
"my.WORKSPACE": true,
"thing.bzl": false,
"workspace.bazel": true,
}
for name, isBuild := range cases {
res := isBuildFilename(name)
if res != isBuild {
t.Errorf("isBuildFilename(%q) should be %v but was %v", name, isBuild, res)
}
}
}

0 comments on commit 0baf2db

Please sign in to comment.