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

constant-glob validates named include argument #1257

Merged
merged 5 commits into from Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 32 additions & 8 deletions warn/warn_bazel.go
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
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