Skip to content

Commit

Permalink
Fix duplicate aliases generated by cargo-bazel when metadata contains… (
Browse files Browse the repository at this point in the history
#3215)

… identical rename

This fixes #3213.
  • Loading branch information
havasd authored Jan 29, 2025
1 parent 7584b08 commit bb78bf0
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 10 deletions.
123 changes: 113 additions & 10 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,16 +221,19 @@ impl Renderer {
});

let shorthand = if let Some(rename) = dep.alias.as_ref() {
dependencies.push(Alias {
rule: alias_rule.rule(),
name: format!("{}-{}", rename, krate.version),
actual: self.crate_label(
&krate.name,
&krate.version.to_string(),
library_target_name,
),
tags: BTreeSet::from(["manual".to_owned()]),
});
// when the alias is the same as the crate name, don't create the alias
if krate.name != *rename {
dependencies.push(Alias {
rule: alias_rule.rule(),
name: format!("{}-{}", rename, krate.version),
actual: self.crate_label(
&krate.name,
&krate.version.to_string(),
library_target_name,
),
tags: BTreeSet::from(["manual".to_owned()]),
});
}
rename
} else {
&krate.name
Expand Down Expand Up @@ -1947,4 +1950,104 @@ mod test {
}
assert!(found);
}

/// Tests a situation where we identical aliases to the crate's name on the
/// package's deps
#[test]
fn crate_with_ambiguous_rename() {
let mut context = Context::default();
let crate_id = CrateId::new("mock_crate".to_owned(), VERSION_ZERO_ONE_ZERO);
context
.workspace_members
.insert(crate_id.clone(), "mock_crate".into());
context.crates.insert(
crate_id.clone(),
CrateContext {
name: crate_id.name.clone(),
version: crate_id.version.clone(),
package_url: Some("http://www.mock_crate.com/".to_owned()),
license_ids: BTreeSet::from(["Apache-2.0".to_owned(), "MIT".to_owned()]),
license_file: None,
additive_build_file_content: None,
disable_pipelining: false,
extra_aliased_targets: BTreeMap::default(),
targets: BTreeSet::from([Rule::Library(mock_target_attributes())]),
library_target_name: Some("library_name".into()),
common_attrs: CommonAttributes {
deps: Select::from_value(BTreeSet::from([CrateDependency {
id: crate_id,
target: "target".into(),
// this is identical to what we have in the `name` attribute
// which creates conflict in `render_module_build_file`
alias: Some("mock_crate".into()),
}])),
..Default::default()
},
build_script_attrs: None,
repository: None,
license: None,
alias_rule: None,
override_targets: BTreeMap::default(),
},
);

let mut render_config = mock_render_config(None);
Arc::get_mut(&mut render_config)
.unwrap()
.generate_rules_license_metadata = true;
let renderer = Renderer::new(render_config, mock_supported_platform_triples());
let output = renderer.render(&context, None).unwrap();

let build_file_content = output.get(&PathBuf::from("BUILD.bazel")).unwrap();

println!("{build_file_content}");
let expected = indoc! {r#"
###############################################################################
# @generated
# DO NOT MODIFY: This file is auto-generated by a crate_universe tool. To
# regenerate this file, run the following:
#
# cargo_bazel_regen_command
###############################################################################
package(default_visibility = ["//visibility:public"])
exports_files(
[
"cargo-bazel.json",
"defs.bzl",
] + glob(
allow_empty = True,
include = ["*.bazel"],
),
)
filegroup(
name = "srcs",
srcs = glob(
allow_empty = True,
include = [
"*.bazel",
"*.bzl",
],
),
)
# Workspace Member Dependencies
alias(
name = "mock_crate-0.1.0",
actual = "@test_rendering__mock_crate-0.1.0//:library_name",
tags = ["manual"],
)
alias(
name = "mock_crate",
actual = "@test_rendering__mock_crate-0.1.0//:library_name",
tags = ["manual"],
)
"#};
assert!(build_file_content
.replace(' ', "")
.contains(&expected.replace(' ', "")));
}
}
1 change: 1 addition & 0 deletions crate_universe/src/utils/starlark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ pub(crate) struct Filegroup {
pub(crate) srcs: Glob,
}

#[derive(Debug)]
pub(crate) struct Alias {
pub(crate) rule: String,
pub(crate) name: String,
Expand Down

0 comments on commit bb78bf0

Please sign in to comment.