Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Crate Universe] Add support for package overrides #1616

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def _annotation(
data = None,
data_glob = None,
deps = None,
dep_overrides = None,
gen_build_script = None,
patch_args = None,
patch_tool = None,
Expand Down Expand Up @@ -114,6 +115,8 @@ def _annotation(
data (list, optional): A list of labels to add to a crate's `rust_library::data` attribute.
data_glob (list, optional): A list of glob patterns to add to a crate's `rust_library::data` attribute.
deps (list, optional): A list of labels to add to a crate's `rust_library::deps` attribute.
dep_overrides (dict, optional): A dictionary of of crate names that should be replaced with a Bazel dependency
in the `rust_library::deps` attribute.
gen_build_script (bool, optional): An authorative flag to determine whether or not to produce
`cargo_build_script` targets for the current crate.
patch_args (list, optional): The `patch_args` attribute of a Bazel repository rule. See
Expand Down Expand Up @@ -159,6 +162,7 @@ def _annotation(
data = data,
data_glob = data_glob,
deps = deps,
dep_overrides = dep_overrides,
gen_build_script = gen_build_script,
patch_args = patch_args,
patch_tool = patch_tool,
Expand Down
5 changes: 5 additions & 0 deletions crate_universe/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ pub struct CrateAnnotations {
/// [deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-deps) attribute.
pub deps: Option<BTreeSet<String>>,

/// A dictionary of of crate names that should be replaced with a Bazel dependency in the
/// [deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-deps) attribute.
pub dep_overrides: Option<BTreeMap<String, String>>,

/// Additional data to pass to
/// [proc_macro_deps](https://bazelbuild.github.io/rules_rust/defs.html#rust_library-proc_macro_deps) attribute.
pub proc_macro_deps: Option<BTreeSet<String>>,
Expand Down Expand Up @@ -293,6 +297,7 @@ impl Add for CrateAnnotations {
let output = CrateAnnotations {
gen_build_script,
deps: joined_extra_member!(self.deps, rhs.deps, BTreeSet::new, BTreeSet::extend),
dep_overrides: joined_extra_member!(self.dep_overrides, rhs.dep_overrides, BTreeMap::new, BTreeMap::extend),
proc_macro_deps: joined_extra_member!(self.proc_macro_deps, rhs.proc_macro_deps, BTreeSet::new, BTreeSet::extend),
crate_features: joined_extra_member!(self.crate_features, rhs.crate_features, BTreeSet::new, BTreeSet::extend),
data: joined_extra_member!(self.data, rhs.data, BTreeSet::new, BTreeSet::extend),
Expand Down
11 changes: 11 additions & 0 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,17 @@ impl CrateContext {
}
}

// Dependency overrides
if let Some(dep_overrides) = &crate_extra.dep_overrides {
for (crate_name, new_dep) in dep_overrides {
let dep_filter = |dep: &CrateDependency| &dep.id.name == crate_name;
if self.common_attrs.deps.any_matches(dep_filter) {
self.common_attrs.deps.remove_if(dep_filter);
self.common_attrs.extra_deps.insert(new_dep.clone());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like it should be an atomic replacement, rather than a remove+insert? In particular, it feels like if the old dep was in a select, so should the new dep be, rather than put into an extra_deps list?

I'd suggest instead adding a replace<T: Clone>(&mut self, old: &T, new: &T) to SelectList which walks all of the relevant BTreeSets doing replacement in each?

}
}
}

// Compile data glob
if let Some(extra) = &crate_extra.compile_data_glob {
self.common_attrs.compile_data_glob.extend(extra.clone());
Expand Down
2 changes: 1 addition & 1 deletion crate_universe/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ mod test {

assert_eq!(
digest,
Digest("33dbf61e3b2aabacadaf7ff0c9862af25703cb851436efcbdf8552735be844ba".to_owned())
Digest("4593a4859b4f52115068f9f583a5f1977e7bc645e004297c7274e158a3215706".to_owned())
);
}

Expand Down
25 changes: 25 additions & 0 deletions crate_universe/src/utils/starlark/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,31 @@ impl<T: Ord> SelectList<T> {
};
}

pub fn any_matches<F>(&mut self, mut match_fn: F) -> bool
where
F: FnMut(&T) -> bool,
{
self.common.iter().any(&mut match_fn)
|| self
.selects
.iter()
.any(|(_, set)| set.iter().any(&mut match_fn))
}

pub fn remove_if<F>(&mut self, mut filter: F)
where
F: FnMut(&T) -> bool,
{
for (_cfg, set) in self.selects.iter_mut() {
if set.iter().any(&mut filter) {
set.retain(|item| !filter(item));
}
}
if self.common.iter().any(&mut filter) {
self.common.retain(|item| !filter(item));
}
}

// TODO: This should probably be added to the [Select] trait
pub fn get_iter<'a>(&'a self, config: Option<&String>) -> Option<btree_set::Iter<T>> {
match config {
Expand Down
5 changes: 3 additions & 2 deletions docs/crate_universe.md
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,8 @@ crate.annotation(<a href="#crate.annotation-version">version</a>, <a href="#crat
<a href="#crate.annotation-build_script_tools">build_script_tools</a>, <a href="#crate.annotation-build_script_data_glob">build_script_data_glob</a>, <a href="#crate.annotation-build_script_deps">build_script_deps</a>, <a href="#crate.annotation-build_script_env">build_script_env</a>,
<a href="#crate.annotation-build_script_proc_macro_deps">build_script_proc_macro_deps</a>, <a href="#crate.annotation-build_script_rustc_env">build_script_rustc_env</a>, <a href="#crate.annotation-build_script_toolchains">build_script_toolchains</a>,
<a href="#crate.annotation-compile_data">compile_data</a>, <a href="#crate.annotation-compile_data_glob">compile_data_glob</a>, <a href="#crate.annotation-crate_features">crate_features</a>, <a href="#crate.annotation-data">data</a>, <a href="#crate.annotation-data_glob">data_glob</a>, <a href="#crate.annotation-deps">deps</a>,
<a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>, <a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>, <a href="#crate.annotation-rustc_env">rustc_env</a>,
<a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>)
<a href="#crate.annotation-dep_overrides">dep_overrides</a>, <a href="#crate.annotation-gen_build_script">gen_build_script</a>, <a href="#crate.annotation-patch_args">patch_args</a>, <a href="#crate.annotation-patch_tool">patch_tool</a>, <a href="#crate.annotation-patches">patches</a>, <a href="#crate.annotation-proc_macro_deps">proc_macro_deps</a>,
<a href="#crate.annotation-rustc_env">rustc_env</a>, <a href="#crate.annotation-rustc_env_files">rustc_env_files</a>, <a href="#crate.annotation-rustc_flags">rustc_flags</a>, <a href="#crate.annotation-shallow_since">shallow_since</a>)
</pre>

A collection of extra attributes and settings for a particular crate
Expand All @@ -532,6 +532,7 @@ A collection of extra attributes and settings for a particular crate
| <a id="crate.annotation-data"></a>data | A list of labels to add to a crate's <code>rust_library::data</code> attribute. | <code>None</code> |
| <a id="crate.annotation-data_glob"></a>data_glob | A list of glob patterns to add to a crate's <code>rust_library::data</code> attribute. | <code>None</code> |
| <a id="crate.annotation-deps"></a>deps | A list of labels to add to a crate's <code>rust_library::deps</code> attribute. | <code>None</code> |
| <a id="crate.annotation-dep_overrides"></a>dep_overrides | A dictionary of of crate names that should be replaced with a Bazel dependency in the <code>rust_library::deps</code> attribute. | <code>None</code> |
| <a id="crate.annotation-gen_build_script"></a>gen_build_script | An authorative flag to determine whether or not to produce <code>cargo_build_script</code> targets for the current crate. | <code>None</code> |
| <a id="crate.annotation-patch_args"></a>patch_args | The <code>patch_args</code> attribute of a Bazel repository rule. See [http_archive.patch_args](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_args) | <code>None</code> |
| <a id="crate.annotation-patch_tool"></a>patch_tool | The <code>patch_tool</code> attribute of a Bazel repository rule. See [http_archive.patch_tool](https://docs.bazel.build/versions/main/repo/http.html#http_archive-patch_tool) | <code>None</code> |
Expand Down
24 changes: 12 additions & 12 deletions examples/crate_universe/cargo_aliases/Cargo.Bazel.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ dependencies = [

[[package]]
name = "env_logger"
version = "0.9.1"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c90bf5f19754d10198ccb95b70664fc925bd1fc090a0fd9a6ebc54acc8cd6272"
checksum = "a12e6657c4c97ebab115a42dcee77225f7f482cdd841cf7088c657a42e9e00e7"
dependencies = [
"atty",
"humantime",
Expand Down Expand Up @@ -154,9 +154,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"

[[package]]
name = "indexmap"
version = "1.9.1"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
Expand Down Expand Up @@ -219,15 +219,15 @@ checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"

[[package]]
name = "os_str_bytes"
version = "6.3.1"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3baf96e39c5359d2eb0dd6ccb42c62b91d9678aa68160d261b9e0ccbf9e9dea9"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"

[[package]]
name = "ppv-lite86"
version = "0.2.16"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"

[[package]]
name = "proc-macro-error"
Expand Down Expand Up @@ -303,9 +303,9 @@ dependencies = [

[[package]]
name = "regex"
version = "1.6.0"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"
checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
dependencies = [
"aho-corasick",
"memchr",
Expand All @@ -314,9 +314,9 @@ dependencies = [

[[package]]
name = "regex-syntax"
version = "0.6.27"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"

[[package]]
name = "strsim"
Expand Down
Loading