Add missing keyword-less functions to rule/macro check (#1546) Without this, certain warnings like `unnamed-macro` and `positional-args` false positive on Starlark helpers that call these builtins. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Improved positional-argument linting so native helper calls that are not rules or macros no longer produce false warnings. Calls to loaded macros that invoke rules positionally are still reported. <!-- end of auto-generated comment: release notes by coderabbit.ai --> Co-authored-by: Willow Kapli <wkap@google.com>
diff --git a/warn/warn_bazel_test.go b/warn/warn_bazel_test.go index 736f079..565def3 100644 --- a/warn/warn_bazel_test.go +++ b/warn/warn_bazel_test.go
@@ -112,6 +112,47 @@ scopeBuild) } +func TestPositionalArgumentsWithLoadedHelpersAndMacros(t *testing.T) { + defer setUpFileReader(map[string]string{ + "test/package/helpers.bzl": ` +def _resolve(label): + return str(native.package_relative_label(label)) + +def flags_from_dict(flags): + return { + _resolve(k): v + for k, v in flags.items() + if native.repo_name() != "" or native.module_name() != None or native.module_version() != None + } + +def list_subpackages(pattern): + _ = native.package_default_visibility() + return native.subpackages(include = [pattern]) + +def register_all(tc, ep): + native.register_toolchains(tc) + native.register_execution_platforms(ep) + +def real_macro(name, srcs): + native.cc_library(name = name, srcs = srcs) +`, + })() + + checkFindings(t, "positional-args", ` +load(":helpers.bzl", "flags_from_dict", "list_subpackages", "real_macro", "register_all") + +FLAGS = flags_from_dict({":flag": "on"}) +SUBPKGS = list_subpackages("*") +register_all("//tc:all", "//ep:all") +real_macro("lib", ["foo.cc"]) +`, + []string{ + `6: All calls to rules or macros should pass arguments by keyword (arg_name=value) syntax. +Found call to rule or macro "real_macro" with positional arguments.`, + }, + scopeBuild) +} + func TestPositionalArgumentsWarnsForPositionalMacrosOrRuleCalls(t *testing.T) { checkFindings(t, "positional-args", ` my_macro = macro()
diff --git a/warn/warn_macro.go b/warn/warn_macro.go index 2bab6d7..0d279fb 100644 --- a/warn/warn_macro.go +++ b/warn/warn_macro.go
@@ -227,8 +227,11 @@ // Check for native rules if fn.filename == nativeModule { switch fn.name { - case "glob", "existing_rule", "existing_rules", "package_name", - "repository_name", "exports_files": + case "glob", "subpackages", "existing_rule", "existing_rules", + "package_name", "package_default_visibility", "package_relative_label", + "repo_name", "repository_name", "module_name", "module_version", + "exports_files", "package", "licenses", "distribs", + "register_toolchains", "register_execution_platforms": // Not a rule default: report.isRuleOrMacro = true
diff --git a/warn/warn_macro_test.go b/warn/warn_macro_test.go index 13c6ce3..d6306de 100644 --- a/warn/warn_macro_test.go +++ b/warn/warn_macro_test.go
@@ -31,24 +31,41 @@ def not_macro(x): foo() native.glob() + native.subpackages() native.existing_rule() native.existing_rules() native.package_name() + native.package_default_visibility() + native.package_relative_label() + native.repo_name() native.repository_name() + native.module_name() + native.module_version() native.exports_files() + native.package() + native.licenses() + native.distribs() + native.register_toolchains() + native.register_execution_platforms() return my_rule def another_macro(x): foo() [native.cc_library() for i in x] + +def pkg_group_macro(x): + native.package_group(name = x) `, []string{ `5: The macro "macro" should have a keyword argument called "name". It is considered a macro because it calls a rule or another macro "my_rule" on line 7.`, - `19: The macro "another_macro" should have a keyword argument called "name". + `30: The macro "another_macro" should have a keyword argument called "name". -It is considered a macro because it calls a rule or another macro "native.cc_library" on line 21.`, +It is considered a macro because it calls a rule or another macro "native.cc_library" on line 32.`, + `34: The macro "pkg_group_macro" should have a keyword argument called "name". + +It is considered a macro because it calls a rule or another macro "native.package_group" on line 35.`, }, scopeBzl)