Skip to content

Commit

Permalink
Set allow_empty on unknown globs
Browse files Browse the repository at this point in the history
This means that if people build with
`--incompatible_disallow_empty_glob` things will still work.
  • Loading branch information
illicitonion committed Feb 22, 2024
1 parent e404e51 commit e63c6aa
Show file tree
Hide file tree
Showing 24 changed files with 90 additions and 13 deletions.
2 changes: 1 addition & 1 deletion crate_universe/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ mod test {
}

#[test]
fn seralization() {
fn serialization() {
let context = mock_context_aliases();

// Seralize and deseralize the context object
Expand Down
3 changes: 3 additions & 0 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand All @@ -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(),
},
Expand Down Expand Up @@ -873,6 +875,7 @@ fn make_data(

Data {
glob: Glob {
allow_empty: true,
include: glob,
exclude: COMMON_GLOB_EXCLUDES
.iter()
Expand Down
32 changes: 23 additions & 9 deletions crate_universe/src/utils/starlark/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ use serde::ser::{Serialize, SerializeStruct, Serializer};

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct Glob {
pub allow_empty: bool,
pub include: BTreeSet<String>,
pub exclude: BTreeSet<String>,
}

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.
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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(),
})
Expand All @@ -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<String>,
#[serde(default)]
exclude: BTreeSet<String>,
}

let glob_map = GlobMap::deserialize(MapAccessDeserializer::new(map))?;
Ok(Glob {
allow_empty: glob_map.allow_empty,
include: glob_map.include,
exclude: glob_map.exclude,
})
Expand Down
6 changes: 3 additions & 3 deletions crate_universe/src/utils/starlark/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand All @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions examples/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/android/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/bzlmod/all_crate_deps/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/bzlmod/cross_compile/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/bzlmod/hello_world/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/cargo_manifest_dir/external_crate/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions examples/crate_universe/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions examples/crate_universe/cargo_aliases/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/crate_universe/cargo_remote/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/crate_universe/cargo_workspace/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/crate_universe/multi_package/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/crate_universe/no_cargo_manifests/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/crate_universe_unnamed/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/ios/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/ios_build/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions examples/nix_cross_compiling/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions examples/zig_cross_compiling/.bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions test/cc_common_link/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/cc_common_link/with_global_alloc/.bazelrc
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions test/no_std/.bazelrc
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e63c6aa

Please sign in to comment.