Skip to content
Open
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
14 changes: 14 additions & 0 deletions buildifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ in a directory recursively:
buildifier -r path/to/dir
```

Paths can be excluded from recursive discovery with repeatable `-exclude`
patterns. Patterns use the syntax of Bazel's `glob()`: `*` and `?` match within a
single path segment and `**` matches any number of segments. They are relative to
the workspace root, wherever buildifier runs, and use forward slashes on every
platform. A pattern matching a directory excludes everything below it:

```bash
buildifier -r -exclude='third_party/**/vendor' -exclude='**/node_modules' .
```

By default, the paths listed in the workspace's `.bazelignore` and the patterns
passed to `ignore_directories()` in its `REPO.bazel` are excluded too. Use
`-respect_bazelignore=false` to discover them anyway.

Buildifier supports the following file types: `BUILD`, `WORKSPACE`, `.bzl`, and
default, the latter is reserved for Starlark files buildifier doesn't know about
(e.g. configuration files for third-party projects that use Starlark). The
Expand Down
8 changes: 6 additions & 2 deletions buildifier/buildifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,12 @@ func (b *buildifier) run(args []string) int {
} else {
files := args
if b.config.Recursive {
var err error
files, err = utils.ExpandDirectories(&args)
excluder, err := utils.NewExcluder(b.config.Exclude, b.config.RespectBazelignore)
if err != nil {
fmt.Fprintf(os.Stderr, "buildifier: %v\n", err)
return 3
}
files, err = utils.ExpandDirectories(&args, excluder)
if err != nil {
fmt.Fprintf(os.Stderr, "buildifier: %v\n", err)
return 3
Expand Down
12 changes: 11 additions & 1 deletion buildifier/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const buildifierJSONFilename = ".buildifier.json"
// New constructs a Config with default values.
func New() *Config {
return &Config{
InputType: "auto",
InputType: "auto",
RespectBazelignore: true,
}
}

Expand Down Expand Up @@ -111,6 +112,13 @@ type Config struct {
WarningsList []string `json:"warningsList,omitempty"`
// Recursive instructs buildifier to find starlark files recursively
Recursive bool `json:"recursive,omitempty"`
// Exclude contains glob() patterns, relative to the workspace root, to skip
// during recursive file discovery
Exclude ArrayFlags `json:"exclude,omitempty"`
// RespectBazelignore also skips the paths ignored by the workspace's
// .bazelignore and the ignore_directories() of its REPO.bazel during
// recursive file discovery
RespectBazelignore bool `json:"respectBazelignore"`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we use a different name here? In a future where ignore_directories is much more widely adopted, this name wouldn't fit well. How about -respect_ignores?

// Verbose instructs buildifier to output verbose diagnostics
Verbose bool `json:"verbose,omitempty"`
// DiffCommand is the command to run when the formatting mode is diff
Expand Down Expand Up @@ -177,6 +185,8 @@ func (c *Config) FlagSet(name string, errorHandling flag.ErrorHandling) *flag.Fl
flags.BoolVar(&c.Verbose, "v", c.Verbose, "print verbose information to standard error")
flags.BoolVar(&c.DiffMode, "d", c.DiffMode, "alias for -mode=diff")
flags.BoolVar(&c.Recursive, "r", c.Recursive, "find starlark files recursively")
flags.Var(&c.Exclude, "exclude", "glob() pattern, relative to the workspace root, to exclude when finding starlark files recursively")
flags.BoolVar(&c.RespectBazelignore, "respect_bazelignore", c.RespectBazelignore, "also exclude the paths ignored by .bazelignore and the ignore_directories() of REPO.bazel when finding starlark files recursively")
flags.BoolVar(&c.MultiDiff, "multi_diff", c.MultiDiff, "the command specified by the -diff_command flag can diff multiple files in the style of tkdiff (default false)")
flags.StringVar(&c.Mode, "mode", c.Mode, "formatting mode: check, diff, or fix (default fix)")
flags.StringVar(&c.Format, "format", c.Format, "diagnostics format: text, json or sarif (default text)")
Expand Down
15 changes: 13 additions & 2 deletions buildifier/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func ExampleNew() {
fmt.Print(c.String())
// Output:
// {
// "type": "auto"
// "type": "auto",
// "respectBazelignore": true
// }
}

Expand Down Expand Up @@ -142,7 +143,8 @@ func ExampleExample() {
// "unreachable",
// "unsorted-dict-items",
// "unused-variable"
// ]
// ],
// "respectBazelignore": true
// }
}

Expand All @@ -160,13 +162,15 @@ func ExampleFlagSet() {
// d: alias for -mode=diff ("false")
// diff_command: command to run when the formatting mode is diff (default uses the BUILDIFIER_DIFF, BUILDIFIER_MULTIDIFF, and DISPLAY environment variables to create the diff command) ("")
// disable_symlink_safety: per default Buildifier will not write to symlinks pointing outside of the Bazel workspace. Setting this to true will disable this behavior ("false")
// exclude: glob() pattern, relative to the workspace root, to exclude when finding starlark files recursively ("")
// format: diagnostics format: text, json or sarif (default text) ("")
// help: print usage information ("false")
// lint: lint mode: off, warn, or fix (default off) ("")
// mode: formatting mode: check, diff, or fix (default fix) ("")
// multi_diff: the command specified by the -diff_command flag can diff multiple files in the style of tkdiff (default false) ("false")
// path: assume BUILD file has this path relative to the workspace directory ("")
// r: find starlark files recursively ("false")
// respect_bazelignore: also exclude the paths ignored by .bazelignore and the ignore_directories() of REPO.bazel when finding starlark files recursively ("true")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A number of projects format everything but exclude their e2e or examples directories via .bazelignore, which makes me think we shouldn't make this the default.

// tables: path to JSON file with custom table definitions which will replace the built-in tables ("")
// type: Input file type: build (for BUILD files), bzl (for .bzl files), workspace (for WORKSPACE files), module (for MODULE.bazel files), repo (for REPO.bazel files), vendor (for VENDOR.bazel files), default (for generic Starlark files) or auto (default, based on the filename) ("auto")
// v: print verbose information to standard error ("false")
Expand All @@ -186,6 +190,8 @@ func ExampleFlagSet_parse() {
"-d",
"--diff_command=diff",
"--disable_symlink_safety=true",
"--exclude=vendor/*",
"--exclude=third_party/*",
"--format=json",
"--help",
"--lint=fix",
Expand Down Expand Up @@ -215,6 +221,11 @@ func ExampleFlagSet_parse() {
// "lint": "fix",
// "warnings": "+print,-no-effect",
// "recursive": true,
// "exclude": [
// "vendor/*",
// "third_party/*"
// ],
// "respectBazelignore": true,
// "verbose": true,
// "diffCommand": "diff",
// "multiDiff": true,
Expand Down
9 changes: 7 additions & 2 deletions buildifier/integration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ echo -e '{ "type": "build" }' > test_dir/.buildifier.test.json # demonstrate con
mkdir test_dir/workspace # name of a starlark file, but a directory
mkdir test_dir/.git # contents should be ignored
echo -e "a+b" > test_dir/.git/git.bzl
mkdir -p test_dir/excluded/nested
echo -e "$INPUT" > test_dir/excluded/nested/excluded.bzl
cat > test_dir/MODULE.bazel <<'EOF'
module(name='my-module',version='1.0',compatibility_level=1)
include("cpp.MODULE.bazel")
Expand Down Expand Up @@ -134,9 +136,10 @@ EOF
cp test_dir/foo.bar golden/foo.bar
cp test_dir/subdir/build golden/build
cp test_dir/.git/git.bzl golden/git.bzl
cp test_dir/excluded/nested/excluded.bzl golden/excluded.bzl

"$buildifier" < test_dir/BUILD > stdout
"$buildifier" -r test_dir
"$buildifier" -r --exclude='test_dir/excluded/*' test_dir
"$buildifier" test.bzl
"$buildifier" --path=foo.bzl test2.bzl
"$buildifier" --config=test_dir/.buildifier.test.json < test_dir/test.bzl > test_dir/test.bzl.BUILD.out
Expand Down Expand Up @@ -376,7 +379,8 @@ cat > golden/.buildifier.example.json <<EOF
"unreachable",
"unsorted-dict-items",
"unused-variable"
]
],
"respectBazelignore": true
}
EOF

Expand All @@ -390,6 +394,7 @@ diff -u test.bzl golden/test.bzl.golden
diff -u test2.bzl golden/test.bzl.golden
diff -u stdout golden/test.bzl.golden
diff -u test_dir/.git/git.bzl golden/git.bzl
diff -u test_dir/excluded/nested/excluded.bzl golden/excluded.bzl
diff -u test_dir/MODULE.bazel golden/MODULE.bazel.golden
diff -u test_dir/REPO.bazel golden/REPO.bazel.golden
diff -u test_dir/VENDOR.bazel golden/VENDOR.bazel.golden
Expand Down
3 changes: 3 additions & 0 deletions buildifier/utils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "utils",
srcs = [
"diagnostics.go",
"exclude.go",
"sarif.go",
"tempfile.go",
"utils.go",
Expand All @@ -13,12 +14,14 @@ go_library(
deps = [
"//build",
"//warn",
"//wspace",
],
)

go_test(
name = "utils_test",
srcs = [
"exclude_test.go",
"sarif_test.go",
"utils_test.go",
],
Expand Down
Loading
Loading