Skip to content

Commit

Permalink
constant-glob validates named include argument (bazelbuild#1257)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-thm authored and apattidb committed May 10, 2024
1 parent 410ff0a commit cc3d358
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
40 changes: 32 additions & 8 deletions warn/warn_bazel.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ var functionsWithPositionalArguments = map[string]bool{
"vardef": true,
}

func constantGlobPatternWarning(patterns *build.ListExpr) []*LinterFinding {
findings := []*LinterFinding{}
for _, expr := range patterns.List {
str, ok := expr.(*build.StringExpr)
if !ok {
continue
}
if !strings.Contains(str.Value, "*") {
message := fmt.Sprintf(
`Glob pattern %q has no wildcard ('*'). Constant patterns can be error-prone, move the file outside the glob.`, str.Value)
findings = append(findings, makeLinterFinding(expr, message))
return findings // at most one warning per glob
}
}
return findings
}

func constantGlobWarning(f *build.File) []*LinterFinding {
switch f.Type {
case build.TypeBuild, build.TypeWorkspace, build.TypeBzl:
Expand All @@ -53,18 +70,25 @@ func constantGlobWarning(f *build.File) []*LinterFinding {
return
}
patterns, ok := call.List[0].(*build.ListExpr)
if !ok {
return
if ok {
// first arg is unnamed and is a list
findings = append(findings, constantGlobPatternWarning(patterns)...)
return // at most one warning per glob
}
for _, expr := range patterns.List {
str, ok := expr.(*build.StringExpr)

// look for named args called include
for _, arg := range call.List {
assign_expr, ok := arg.(*build.AssignExpr)
if !ok {
continue
}
if !strings.Contains(str.Value, "*") {
message := fmt.Sprintf(
`Glob pattern %q has no wildcard ('*'). Constant patterns can be error-prone, move the file outside the glob.`, str.Value)
findings = append(findings, makeLinterFinding(expr, message))
str, ok := assign_expr.LHS.(*build.Ident)
if !ok || str.Name != "include" {
continue
}
patterns, ok := assign_expr.RHS.(*build.ListExpr)
if ok {
findings = append(findings, constantGlobPatternWarning(patterns)...)
return // at most one warning per glob
}
}
Expand Down
23 changes: 16 additions & 7 deletions warn/warn_bazel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,24 @@ import "testing"
func TestConstantGlob(t *testing.T) {
checkFindings(t, "constant-glob", `
cc_library(srcs = glob(["foo.cc"]))
cc_library(srcs = glob(["*.cc"]))
cc_library(srcs = glob(include = ["foo.cc"]))
cc_library(srcs = glob(include = ["foo.cc"], exclude = ["bar.cc"]))
cc_library(srcs = glob(exclude = ["bar.cc"], include = ["foo.cc"]))
cc_library(srcs =
["constant"] + glob([
"*.cc",
"test.cpp",
])
)`,
["constant"] + glob([
"*.cc",
"test.cpp",
])
)
cc_library(srcs = glob(["*.cc"]))
cc_library(srcs = glob(["*.cc"], exclude = ["bar.cc"]))
cc_library(srcs = glob(include = ["*.cc"], exclude = ["bar.cc"]))
cc_library(srcs = glob(exclude = ["bar.cc"], include = ["*.cc"]))`,
[]string{`:1: Glob pattern "foo.cc" has no wildcard`,
`:6: Glob pattern "test.cpp" has no wildcard`},
`:2: Glob pattern "foo.cc" has no wildcard`,
`:3: Glob pattern "foo.cc" has no wildcard`,
`:4: Glob pattern "foo.cc" has no wildcard`,
`:8: Glob pattern "test.cpp" has no wildcard`},
scopeBuild|scopeBzl|scopeWorkspace)
}

Expand Down

0 comments on commit cc3d358

Please sign in to comment.