Introduce flag --incompatible_disallow_empty_glob=true

Implements https://github.com/bazelbuild/bazel/issues/8195

When the flag is enabled, the default value of allow_empty in glob is False.

RELNOTES: New flag `--incompatible_disallow_empty_glob`. See https://github.com/bazelbuild/bazel/issues/8195
PiperOrigin-RevId: 250263059
diff --git a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
index bdd7d35..7583924 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/PackageFactoryTest.java
@@ -661,7 +661,7 @@
         "glob()",
         "insufficient arguments received by glob(include: sequence of strings, "
             + "*, exclude: sequence of strings = [], exclude_directories: int = 1, "
-            + "allow_empty: bool = True) (got 0, expected at least 1)");
+            + "allow_empty: bool = <unbound>) (got 0, expected at least 1)");
   }
 
   @Test
@@ -671,7 +671,7 @@
         "glob(['a'], ['b'])",
         "too many (2) positional arguments in call to glob(include: sequence of strings, "
             + "*, exclude: sequence of strings = [], exclude_directories: int = 1, "
-            + "allow_empty: bool = True)");
+            + "allow_empty: bool = <unbound>)");
   }
 
   @Test
@@ -681,7 +681,7 @@
         "glob(1,2,3,4)",
         "too many (4) positional arguments in call to glob(include: sequence of strings, "
             + "*, exclude: sequence of strings = [], exclude_directories: int = 1, "
-            + "allow_empty: bool = True)");
+            + "allow_empty: bool = <unbound>)");
   }
 
   @Test
@@ -809,6 +809,48 @@
   }
 
   @Test
+  public void testGlobDisallowEmpty() throws Exception {
+    Path buildFile = scratch.file("/pkg/BUILD", "x = " + "glob(['*.foo'])");
+    Package pkg =
+        packages.createPackage(
+            "pkg",
+            RootedPath.toRootedPath(root, buildFile),
+            "--incompatible_disallow_empty_glob=false");
+    assertThat(pkg.containsErrors()).isFalse();
+  }
+
+  @Test
+  public void testGlobAllowEmpty() throws Exception {
+    events.setFailFast(false);
+
+    Path buildFile = scratch.file("/pkg/BUILD", "x = " + "glob(['*.foo'])");
+    Package pkg =
+        packages.createPackage(
+            "pkg",
+            RootedPath.toRootedPath(root, buildFile),
+            "--incompatible_disallow_empty_glob=true");
+
+    assertThat(pkg.containsErrors()).isTrue();
+    events.assertContainsError(
+        "glob pattern '*.foo' didn't match anything, but allow_empty is set to False");
+  }
+
+  @Test
+  public void testGlobNotBoolean() throws Exception {
+    events.setFailFast(false);
+
+    Path buildFile = scratch.file("/pkg/BUILD", "x = " + "glob(['*.foo'], allow_empty = 5)");
+    Package pkg =
+        packages.createPackage(
+            "pkg",
+            RootedPath.toRootedPath(root, buildFile),
+            "--incompatible_disallow_empty_glob=true");
+
+    assertThat(pkg.containsErrors()).isTrue();
+    events.assertContainsError("expected boolean for argument `allow_empty`, got `5`");
+  }
+
+  @Test
   public void testNativeModuleIsAvailable() throws Exception {
     Path buildFile = scratch.file("/pkg/BUILD", "native.cc_library(name='bar')");
     Package pkg =
diff --git a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
index d202f70..bdd8af9 100644
--- a/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
+++ b/src/test/java/com/google/devtools/build/lib/packages/SkylarkSemanticsConsistencyTest.java
@@ -140,6 +140,7 @@
         "--incompatible_disable_objc_provider_resources=" + rand.nextBoolean(),
         "--incompatible_disable_third_party_license_checking=" + rand.nextBoolean(),
         "--incompatible_disallow_dict_plus=" + rand.nextBoolean(),
+        "--incompatible_disallow_empty_glob=" + rand.nextBoolean(),
         "--incompatible_disallow_filetype=" + rand.nextBoolean(),
         "--incompatible_disallow_legacy_javainfo=" + rand.nextBoolean(),
         "--incompatible_disallow_legacy_java_provider=" + rand.nextBoolean(),
@@ -191,6 +192,7 @@
         .incompatibleDisableObjcProviderResources(rand.nextBoolean())
         .incompatibleDisableThirdPartyLicenseChecking(rand.nextBoolean())
         .incompatibleDisallowDictPlus(rand.nextBoolean())
+        .incompatibleDisallowEmptyGlob(rand.nextBoolean())
         .incompatibleDisallowFileType(rand.nextBoolean())
         .incompatibleDisallowLegacyJavaInfo(rand.nextBoolean())
         .incompatibleDisallowLegacyJavaProvider(rand.nextBoolean())