python: Improve the error message when there isn't a generator function that
created the offending target.

Otherwise the error has confusing text like "Generator: (name=)".

PiperOrigin-RevId: 522698198
Change-Id: I4eae8552ea8f25cf063e1de4f4de67a693c0563d
diff --git a/src/main/starlark/builtins_bzl/common/python/common.bzl b/src/main/starlark/builtins_bzl/common/python/common.bzl
index 229f392..a114721 100644
--- a/src/main/starlark/builtins_bzl/common/python/common.bzl
+++ b/src/main/starlark/builtins_bzl/common/python/common.bzl
@@ -498,9 +498,18 @@
         allowlist_help = ("no allowlist specified; all disallowed; specify one " +
                           "with --python_native_rules_allowlist")
     if not allowed:
+        if ctx.attr.generator_function:
+            generator = "{generator_function}(name={generator_name}) in {generator_location}".format(
+                generator_function = ctx.attr.generator_function,
+                generator_name = ctx.attr.generator_name,
+                generator_location = ctx.attr.generator_location,
+            )
+        else:
+            generator = "No generator (called directly in BUILD file)"
+
         msg = (
             "{target} not allowed to use native.{rule}\n" +
-            "Generated by: {generator_function}(name={generator_name}) in {generator_location}\n" +
+            "Generated by: {generator}\n" +
             "Allowlist: {allowlist}\n" +
             "Migrate to using @rules_python, see {help_url}\n" +
             "FIXCMD: {fix_cmd} --target={target} --rule={rule} " +
@@ -509,8 +518,8 @@
         fail(msg.format(
             target = str(ctx.label).replace("@//", "//"),
             rule = _py_builtins.get_rule_name(ctx),
+            generator = generator,
             allowlist = allowlist_help,
-            generator_function = ctx.attr.generator_function,
             generator_name = ctx.attr.generator_name,
             generator_location = ctx.attr.generator_location,
             help_url = NATIVE_RULES_MIGRATION_HELP_URL,