diff --git a/crate_universe/src/context.rs b/crate_universe/src/context.rs index 47dc994a78..dc9ea80fa2 100644 --- a/crate_universe/src/context.rs +++ b/crate_universe/src/context.rs @@ -291,7 +291,7 @@ mod test { } #[test] - fn seralization() { + fn serialization() { let context = mock_context_aliases(); // Seralize and deseralize the context object diff --git a/crate_universe/src/rendering.rs b/crate_universe/src/rendering.rs index 8a2f9fee75..2c83c7c40f 100644 --- a/crate_universe/src/rendering.rs +++ b/crate_universe/src/rendering.rs @@ -153,6 +153,7 @@ impl Renderer { let mut exports_files = ExportsFiles { paths: BTreeSet::from(["cargo-bazel.json".to_owned(), "defs.bzl".to_owned()]), globs: Glob { + allow_empty: true, include: BTreeSet::from(["*.bazel".to_owned()]), exclude: BTreeSet::new(), }, @@ -165,6 +166,7 @@ impl Renderer { let filegroup = Filegroup { name: "srcs".to_owned(), srcs: Glob { + allow_empty: true, include: BTreeSet::from(["*.bazel".to_owned(), "*.bzl".to_owned()]), exclude: BTreeSet::new(), }, @@ -873,6 +875,7 @@ fn make_data( Data { glob: Glob { + allow_empty: true, include: glob, exclude: COMMON_GLOB_EXCLUDES .iter() diff --git a/crate_universe/src/utils/starlark/glob.rs b/crate_universe/src/utils/starlark/glob.rs index a7bcebbbad..46d4836624 100644 --- a/crate_universe/src/utils/starlark/glob.rs +++ b/crate_universe/src/utils/starlark/glob.rs @@ -7,6 +7,7 @@ use serde::ser::{Serialize, SerializeStruct, Serializer}; #[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone)] pub struct Glob { + pub allow_empty: bool, pub include: BTreeSet, pub exclude: BTreeSet, } @@ -14,12 +15,13 @@ pub struct Glob { impl Glob { pub fn new_rust_srcs() -> Self { Self { + allow_empty: false, include: BTreeSet::from(["**/*.rs".to_owned()]), exclude: BTreeSet::new(), } } - pub fn is_empty(&self) -> bool { + pub fn has_any_include(&self) -> bool { self.include.is_empty() // Note: self.exclude intentionally not considered. A glob is empty if // there are no included globs. A glob cannot have only excludes. @@ -31,16 +33,17 @@ impl Serialize for Glob { where S: Serializer, { - if self.exclude.is_empty() { - // Serialize as glob([...]). - serializer.serialize_newtype_struct("glob", &self.include) - } else { - // Serialize as glob(include = [...], exclude = [...]). - let mut call = serializer.serialize_struct("glob", 2)?; - call.serialize_field("include", &self.include)?; + let has_exclude = !self.exclude.is_empty(); + let len = 2 + if has_exclude { 1 } else { 0 }; + + // Serialize as glob(allow_empty = False, include = [...], exclude = [...]). + let mut call = serializer.serialize_struct("glob", len)?; + call.serialize_field("allow_empty", &self.allow_empty)?; + call.serialize_field("include", &self.include)?; + if has_exclude { call.serialize_field("exclude", &self.exclude)?; - call.end() } + call.end() } } @@ -68,6 +71,9 @@ impl<'de> Visitor<'de> for GlobVisitor { A: SeqAccess<'de>, { Ok(Glob { + // At time of writing the default value of allow_empty is true. + // We may want to change this if the default changes in Bazel. + allow_empty: true, include: BTreeSet::deserialize(SeqAccessDeserializer::new(seq))?, exclude: BTreeSet::new(), }) @@ -78,14 +84,22 @@ impl<'de> Visitor<'de> for GlobVisitor { where A: MapAccess<'de>, { + fn default_true() -> bool { + true + } + #[derive(serde::Deserialize)] struct GlobMap { + #[serde(default = "default_true")] + allow_empty: bool, include: BTreeSet, + #[serde(default)] exclude: BTreeSet, } let glob_map = GlobMap::deserialize(MapAccessDeserializer::new(map))?; Ok(Glob { + allow_empty: glob_map.allow_empty, include: glob_map.include, exclude: glob_map.exclude, }) diff --git a/crate_universe/src/utils/starlark/serialize.rs b/crate_universe/src/utils/starlark/serialize.rs index 1368f7bbaf..e6c082d773 100644 --- a/crate_universe/src/utils/starlark/serialize.rs +++ b/crate_universe/src/utils/starlark/serialize.rs @@ -130,7 +130,7 @@ impl Serialize for ExportsFiles { impl Data { pub fn is_empty(&self) -> bool { - self.glob.is_empty() && self.select.is_empty() + self.glob.has_any_include() && self.select.is_empty() } } @@ -140,10 +140,10 @@ impl Serialize for Data { S: Serializer, { let mut plus = serializer.serialize_tuple_struct("+", MULTILINE)?; - if !self.glob.is_empty() { + if !self.glob.has_any_include() { plus.serialize_field(&self.glob)?; } - if !self.select.is_empty() || self.glob.is_empty() { + if !self.select.is_empty() || self.glob.has_any_include() { plus.serialize_field(&self.select)?; } plus.end() diff --git a/examples/.bazelrc b/examples/.bazelrc index aeb9ab4385..05952d1165 100644 --- a/examples/.bazelrc +++ b/examples/.bazelrc @@ -18,6 +18,9 @@ build:clippy --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/android/.bazelrc b/examples/android/.bazelrc index 52bb082c05..97fd915342 100644 --- a/examples/android/.bazelrc +++ b/examples/android/.bazelrc @@ -8,3 +8,6 @@ build --fat_apk_cpu=arm64-v8a --android_crosstool_top=@androidndk//:toolchain # TODO: migrate all dependencies from WORKSPACE to MODULE.bazel # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/bzlmod/all_crate_deps/.bazelrc b/examples/bzlmod/all_crate_deps/.bazelrc index 825e3155e5..c18c1cbeca 100644 --- a/examples/bzlmod/all_crate_deps/.bazelrc +++ b/examples/bzlmod/all_crate_deps/.bazelrc @@ -4,3 +4,6 @@ startup --windows_enable_symlinks build:windows --enable_runfiles build --experimental_enable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/bzlmod/cross_compile/.bazelrc b/examples/bzlmod/cross_compile/.bazelrc index e2ece0c386..1f17c8b416 100644 --- a/examples/bzlmod/cross_compile/.bazelrc +++ b/examples/bzlmod/cross_compile/.bazelrc @@ -1 +1,4 @@ build --experimental_enable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/bzlmod/hello_world/.bazelrc b/examples/bzlmod/hello_world/.bazelrc index 825e3155e5..c18c1cbeca 100644 --- a/examples/bzlmod/hello_world/.bazelrc +++ b/examples/bzlmod/hello_world/.bazelrc @@ -4,3 +4,6 @@ startup --windows_enable_symlinks build:windows --enable_runfiles build --experimental_enable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/cargo_manifest_dir/external_crate/.bazelrc b/examples/cargo_manifest_dir/external_crate/.bazelrc index 710c4d81ab..7344b7f363 100644 --- a/examples/cargo_manifest_dir/external_crate/.bazelrc +++ b/examples/cargo_manifest_dir/external_crate/.bazelrc @@ -1 +1,4 @@ common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/crate_universe/.bazelrc b/examples/crate_universe/.bazelrc index 7b05ff98bd..117415bf76 100644 --- a/examples/crate_universe/.bazelrc +++ b/examples/crate_universe/.bazelrc @@ -22,6 +22,9 @@ build --nolegacy_external_runfiles # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/crate_universe/cargo_aliases/.bazelrc b/examples/crate_universe/cargo_aliases/.bazelrc index d7379de699..a67f1baf7f 100644 --- a/examples/crate_universe/cargo_aliases/.bazelrc +++ b/examples/crate_universe/cargo_aliases/.bazelrc @@ -17,6 +17,9 @@ build:strict --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/crate_universe/cargo_remote/.bazelrc b/examples/crate_universe/cargo_remote/.bazelrc index d7379de699..a67f1baf7f 100644 --- a/examples/crate_universe/cargo_remote/.bazelrc +++ b/examples/crate_universe/cargo_remote/.bazelrc @@ -17,6 +17,9 @@ build:strict --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/crate_universe/cargo_workspace/.bazelrc b/examples/crate_universe/cargo_workspace/.bazelrc index d7379de699..a67f1baf7f 100644 --- a/examples/crate_universe/cargo_workspace/.bazelrc +++ b/examples/crate_universe/cargo_workspace/.bazelrc @@ -17,6 +17,9 @@ build:strict --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/crate_universe/multi_package/.bazelrc b/examples/crate_universe/multi_package/.bazelrc index d7379de699..a67f1baf7f 100644 --- a/examples/crate_universe/multi_package/.bazelrc +++ b/examples/crate_universe/multi_package/.bazelrc @@ -17,6 +17,9 @@ build:strict --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/crate_universe/no_cargo_manifests/.bazelrc b/examples/crate_universe/no_cargo_manifests/.bazelrc index d7379de699..a67f1baf7f 100644 --- a/examples/crate_universe/no_cargo_manifests/.bazelrc +++ b/examples/crate_universe/no_cargo_manifests/.bazelrc @@ -17,6 +17,9 @@ build:strict --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/crate_universe_unnamed/.bazelrc b/examples/crate_universe_unnamed/.bazelrc index aeb9ab4385..05952d1165 100644 --- a/examples/crate_universe_unnamed/.bazelrc +++ b/examples/crate_universe_unnamed/.bazelrc @@ -18,6 +18,9 @@ build:clippy --output_groups=+clippy_checks # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # This import should always be last to allow users to override # settings for local development. try-import %workspace%/user.bazelrc diff --git a/examples/ios/.bazelrc b/examples/ios/.bazelrc index 7c4827ae80..da35d76a8d 100644 --- a/examples/ios/.bazelrc +++ b/examples/ios/.bazelrc @@ -11,3 +11,6 @@ build --host_crosstool_top=@local_config_apple_cc//:toolchain # TODO: migrate all dependencies from WORKSPACE to MODULE.bazel # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/ios_build/.bazelrc b/examples/ios_build/.bazelrc index 7c4827ae80..da35d76a8d 100644 --- a/examples/ios_build/.bazelrc +++ b/examples/ios_build/.bazelrc @@ -11,3 +11,6 @@ build --host_crosstool_top=@local_config_apple_cc//:toolchain # TODO: migrate all dependencies from WORKSPACE to MODULE.bazel # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/examples/nix_cross_compiling/.bazelrc b/examples/nix_cross_compiling/.bazelrc index 6686d26d11..9be4b2cea3 100644 --- a/examples/nix_cross_compiling/.bazelrc +++ b/examples/nix_cross_compiling/.bazelrc @@ -4,6 +4,9 @@ build --incompatible_strict_action_env build --action_env=BAZEL_DO_NOT_DETECT_CPP_TOOLCHAIN=1 build --incompatible_enable_cc_toolchain_resolution=true +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob + # Require Platform Transitions ## This works by setting the targte platform to an invalid platform ## and each `x_binary()` and `x_library()` rule unfortunately needs diff --git a/examples/zig_cross_compiling/.bazelrc b/examples/zig_cross_compiling/.bazelrc index 5a1a8fd026..0be7ebb3a6 100644 --- a/examples/zig_cross_compiling/.bazelrc +++ b/examples/zig_cross_compiling/.bazelrc @@ -8,3 +8,6 @@ build --incompatible_enable_cc_toolchain_resolution # TODO: migrate all dependencies from WORKSPACE to MODULE.bazel # https://github.com/bazelbuild/rules_rust/issues/2181 common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/test/cc_common_link/.bazelrc b/test/cc_common_link/.bazelrc index 710c4d81ab..7344b7f363 100644 --- a/test/cc_common_link/.bazelrc +++ b/test/cc_common_link/.bazelrc @@ -1 +1,4 @@ common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/test/cc_common_link/with_global_alloc/.bazelrc b/test/cc_common_link/with_global_alloc/.bazelrc index 710c4d81ab..7344b7f363 100644 --- a/test/cc_common_link/with_global_alloc/.bazelrc +++ b/test/cc_common_link/with_global_alloc/.bazelrc @@ -1 +1,4 @@ common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob diff --git a/test/no_std/.bazelrc b/test/no_std/.bazelrc index 710c4d81ab..7344b7f363 100644 --- a/test/no_std/.bazelrc +++ b/test/no_std/.bazelrc @@ -1 +1,4 @@ common --noenable_bzlmod + +# This isn't currently the defaut in Bazel, but we enable it to test we'll be ready if/when it flips. +build --incompatible_disallow_empty_glob