From 0baf2db4a456c7dea47d649afff8031556325951 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Thu, 15 Nov 2018 04:43:25 -0800 Subject: [PATCH] Expand the set of file names considered to be build files. (#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. --- build/BUILD.bazel | 2 ++ build/lex.go | 21 +++++++++++++++------ build/lex_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 build/lex_test.go diff --git a/build/BUILD.bazel b/build/BUILD.bazel index ca70fb11d..945288a8e 100644 --- a/build/BUILD.bazel +++ b/build/BUILD.bazel @@ -29,6 +29,7 @@ go_test( size = "small", srcs = [ "checkfile_test.go", + "lex_test.go", "parse_test.go", "print_test.go", "quote_test.go", @@ -44,6 +45,7 @@ go_test( ], embed = [":go_default_library"], deps = [ + "//tables:go_default_library", "//testutils", ], ) diff --git a/build/lex.go b/build/lex.go index 61b0b79c6..f698e2667 100644 --- a/build/lex.go +++ b/build/lex.go @@ -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. @@ -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) diff --git a/build/lex_test.go b/build/lex_test.go new file mode 100644 index 000000000..801bbf6de --- /dev/null +++ b/build/lex_test.go @@ -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) + } + } +}