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

Begin work on resolving duplicate aliases. #514

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions impl/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ pub struct CrateTargetedDepContext {
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct CrateContext {
pub pkg_name: String,
pub download_name: String,
pub pkg_version: Version,
pub edition: String,
pub raze_settings: CrateSettings,
Expand All @@ -161,6 +162,7 @@ pub struct CrateContext {
pub workspace_member_dependents: Vec<Utf8PathBuf>,
pub workspace_member_dev_dependents: Vec<Utf8PathBuf>,
pub workspace_member_build_dependents: Vec<Utf8PathBuf>,
pub is_workspace_member: bool,
pub is_workspace_member_dependency: bool,
pub is_binary_dependency: bool,
pub targets: Vec<BuildableTarget>,
Expand Down
27 changes: 23 additions & 4 deletions impl/src/planning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@ pub mod tests {
Vec::new(), /* attrs */
)));

assert!(planned_build_res.unwrap().crate_contexts.is_empty());
let contexts: Vec<CrateContext> = planned_build_res
.unwrap()
.crate_contexts
.iter()
.filter(|ctx| !ctx.is_workspace_member)
.cloned()
.collect();
assert!(contexts.is_empty());
}

#[test]
Expand All @@ -145,8 +152,13 @@ pub mod tests {
)));

let planned_build = planned_build_res.unwrap();
assert_eq!(planned_build.crate_contexts.len(), 1);
let dep = planned_build.crate_contexts.get(0).unwrap();
let contexts: Vec<&CrateContext> = planned_build
.crate_contexts
.iter()
.filter(|ctx| !ctx.is_workspace_member)
.collect();
assert_eq!(contexts.len(), 1);
let dep = contexts.get(0).unwrap();
assert_eq!(dep.pkg_name, "test_dep");
assert!(!dep.workspace_member_dependents.is_empty());
assert!(
Expand Down Expand Up @@ -216,7 +228,14 @@ pub mod tests {
"some_target_triple".to_owned(),
Vec::new(), /* attrs */
)));
assert!(planned_build_res.unwrap().crate_contexts.is_empty());
let contexts: Vec<CrateContext> = planned_build_res
.unwrap()
.crate_contexts
.iter()
.filter(|ctx| !ctx.is_workspace_member)
.cloned()
.collect();
assert!(contexts.is_empty());
}

#[test]
Expand Down
58 changes: 54 additions & 4 deletions impl/src/planning/subplanners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ impl<'planner> WorkspaceSubplanner<'planner> {
let own_crate_catalog_entry = catalog.entry_for_package_id(&node.id)?;
let own_package = own_crate_catalog_entry.package();

// This section prevents produce_crate_contexts from functioning
// properly. To build the root_ctxs list, it checks whether the
// dependency is a workspace crate.
/*
let is_binary_dep = self
.settings
.binary_deps
Expand All @@ -145,7 +149,7 @@ impl<'planner> WorkspaceSubplanner<'planner> {
// Skip workspace members unless they are binary dependencies
if own_crate_catalog_entry.is_workspace_crate() && !is_binary_dep {
return None;
}
} */

// UNWRAP: Safe given unwrap during serialize step of metadata
let own_source_id = own_package
Expand Down Expand Up @@ -175,6 +179,8 @@ impl<'planner> WorkspaceSubplanner<'planner> {
.produce_context(&self.metadata.cargo_workspace_root)
.map(|x| (x, own_crate_catalog_entry.is_workspace_crate()));

eprintln!("res: {:#?}", res);

Some(res)
}

Expand Down Expand Up @@ -220,6 +226,12 @@ impl<'planner> WorkspaceSubplanner<'planner> {
.filter_map(|node| self.create_crate_context(node, self.crate_catalog))
.collect::<Result<Vec<_>>>()?;

eprintln!("contexts len: {:?}", contexts.len());
for (ctx, is_w) in contexts.iter() {
eprintln!("context: {}", ctx.pkg_name);
eprintln!("is_w: {}", is_w);
}

let root_ctxs = contexts
.iter()
.filter_map(|(ctx, is_workspace)| match is_workspace {
Expand All @@ -231,21 +243,45 @@ impl<'planner> WorkspaceSubplanner<'planner> {

let contexts = contexts.into_iter().map(|(ctx, _)| ctx).collect_vec();
let aliases = self.produce_workspace_aliases(root_ctxs, &contexts);
let renamed_contexts = contexts
.into_iter()
.map(|mut ctx| {
if let Some(alias) = aliases.iter().find(|alias| {
eprintln!("alias.target {:?}", alias.alias);
eprintln!("alias.target {:?}", alias.target);
if ctx.pkg_name == "rand" {
let alias_name = alias.target.clone();
eprintln!("alias_name: {}", alias_name);
}

Ok((contexts, aliases))
format!("{}:{}", ctx.workspace_path_to_crate, ctx.pkg_name) == alias.target
}) {
ctx.pkg_name = alias.alias.clone();
ctx
} else {
ctx
}
})
.collect();

Ok((renamed_contexts, aliases))
}

fn produce_workspace_aliases(
&self,
root_dep_aliases: BTreeMap<String, DependencyAlias>,
all_packages: &[CrateContext],
) -> Vec<DependencyAlias> {
eprintln!("!!! root_dep_aliases: {:#?}", root_dep_aliases);

let renames = root_dep_aliases
.iter()
.map(|(_name, rename)| (&rename.target, &rename.alias))
.collect::<HashMap<_, _>>();

all_packages
eprintln!("!!! renames: {:#?}", renames);

let dep_aliases = all_packages
.iter()
.filter(|to_alias| {
to_alias.lib_target_name.is_some()
Expand All @@ -256,12 +292,20 @@ impl<'planner> WorkspaceSubplanner<'planner> {
|| !to_alias.raze_settings.extra_aliased_targets.is_empty()
})
.flat_map(|to_alias| {
eprintln!(
"!!! to_alias: {:#?}, version: {:?}",
to_alias.pkg_name, to_alias.pkg_version
);

let pkg_name = to_alias.pkg_name.replace('-', "_");
let target = format!("{}:{}", &to_alias.workspace_path_to_crate, &pkg_name);
let alias = renames
.get(&target)
.map(|x| x.to_string())
.unwrap_or(pkg_name);

eprintln!("!!! alias: {:#?}", alias);

let dep_alias = DependencyAlias { target, alias };

to_alias
Expand All @@ -275,7 +319,11 @@ impl<'planner> WorkspaceSubplanner<'planner> {
.chain(std::iter::once(dep_alias))
})
.sorted()
.collect_vec()
.collect_vec();

eprintln!("!!! dep_aliases: {:#?}", dep_aliases);

dep_aliases
}
}

Expand Down Expand Up @@ -394,13 +442,15 @@ impl<'planner> CrateSubplanner<'planner> {

let context = CrateContext {
pkg_name: package.name.clone(),
download_name: package.name.clone(),
pkg_version: package.version.clone(),
edition: package.edition.clone(),
license: self.produce_license(),
features: self.features.unwrap_or(&Features::empty()).clone(),
workspace_member_dependents,
workspace_member_dev_dependents,
workspace_member_build_dependents,
is_workspace_member: self.crate_catalog_entry.is_workspace_crate(),
is_workspace_member_dependency,
is_binary_dependency,
is_proc_macro,
Expand Down
13 changes: 12 additions & 1 deletion impl/src/rendering/bazel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,13 @@ impl BazelRenderer {
experimental_api: bool,
) -> Result<String, tera::Error> {
let mut context = Context::new();
// Filter out workspace members, they don't need to be downloaded.
let crates: Vec<&CrateContext> = all_packages
.iter()
.filter(|crate_context| !crate_context.is_workspace_member)
.collect();
context.insert("workspace", &workspace_context);
context.insert("crates", &all_packages);
context.insert("crates", &crates);
context.insert("bazel_package_name", &bazel_package_name);
context.insert("is_remote_genmode", &is_remote_genmode);
context.insert("experimental_api", &experimental_api);
Expand Down Expand Up @@ -531,6 +536,7 @@ mod tests {
fn dummy_binary_crate_with_name(buildfile_suffix: &str) -> CrateContext {
CrateContext {
pkg_name: "test-binary".to_owned(),
download_name: "test-binary".to_owned(),
pkg_version: Version::parse("1.1.1").unwrap(),
edition: "2015".to_owned(),
features: Features {
Expand All @@ -546,6 +552,7 @@ mod tests {
workspace_member_dependents: Vec::new(),
workspace_member_dev_dependents: Vec::new(),
workspace_member_build_dependents: Vec::new(),
is_workspace_member: false,
is_workspace_member_dependency: false,
is_binary_dependency: false,
is_proc_macro: false,
Expand Down Expand Up @@ -578,6 +585,7 @@ mod tests {
fn dummy_library_crate_with_name(buildfile_suffix: &str) -> CrateContext {
CrateContext {
pkg_name: "test-library".to_owned(),
download_name: "test-library".to_owned(),
pkg_version: Version::parse("1.1.1").unwrap(),
edition: "2015".to_owned(),
license: LicenseData::default(),
Expand All @@ -593,6 +601,7 @@ mod tests {
workspace_member_dependents: Vec::new(),
workspace_member_dev_dependents: Vec::new(),
workspace_member_build_dependents: Vec::new(),
is_workspace_member: false,
is_workspace_member_dependency: false,
is_binary_dependency: false,
is_proc_macro: false,
Expand Down Expand Up @@ -625,6 +634,7 @@ mod tests {
fn dummy_proc_macro_crate_with_name(buildfile_suffix: &str) -> CrateContext {
CrateContext {
pkg_name: "test-proc-macro".to_owned(),
download_name: "test-proc-macro".to_owned(),
pkg_version: Version::parse("1.1.1").unwrap(),
edition: "2015".to_owned(),
license: LicenseData::default(),
Expand All @@ -650,6 +660,7 @@ mod tests {
workspace_member_dependents: Vec::new(),
workspace_member_dev_dependents: Vec::new(),
workspace_member_build_dependents: Vec::new(),
is_workspace_member: false,
is_workspace_member_dependency: false,
is_proc_macro: true,
is_binary_dependency: false,
Expand Down
10 changes: 5 additions & 5 deletions impl/src/rendering/templates/remote_crates.bzl.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ def {{workspace.gen_workspace_prefix}}_fetch_remote_crates():
{%- if crate.source_details.git_data %}
maybe(
new_git_repository,
name = "{{workspace.gen_workspace_prefix}}__{{crate.pkg_name | replace(from="-", to="_")}}__{{crate.pkg_version | slugify | replace(from="-", to="_")}}",
name = "{{workspace.gen_workspace_prefix}}__{{crate.download_name | replace(from="-", to="_")}}__{{crate.pkg_version | slugify | replace(from="-", to="_")}}",
remote = "{{crate.source_details.git_data.remote}}",
commit = "{{crate.source_details.git_data.commit}}",
build_file = Label("{{workspace.workspace_path}}/remote:BUILD.{{crate.pkg_name}}-{{crate.pkg_version}}.bazel"),
build_file = Label("{{workspace.workspace_path}}/remote:BUILD.{{crate.download_name}}-{{crate.pkg_version}}.bazel"),
init_submodules = True,
{%- include "templates/partials/remote_crates_patch.template" %}
)
{%- else %}
maybe(
http_archive,
name = "{{workspace.gen_workspace_prefix}}__{{crate.pkg_name | replace(from="-", to="_")}}__{{crate.pkg_version | slugify | replace(from="-", to="_")}}",
name = "{{workspace.gen_workspace_prefix}}__{{crate.download_name | replace(from="-", to="_")}}__{{crate.pkg_version | slugify | replace(from="-", to="_")}}",
url = "{{ crate.source_details.download_url }}",
type = "tar.gz",
{%- if crate.sha256 %}
sha256 = "{{crate.sha256}}",
{%- endif %}
strip_prefix = "{{crate.pkg_name}}-{{crate.pkg_version}}",
strip_prefix = "{{crate.download_name}}-{{crate.pkg_version}}",
{%- include "templates/partials/remote_crates_patch.template" %}
build_file = Label("{{workspace.workspace_path}}/remote:BUILD.{{crate.pkg_name}}-{{crate.pkg_version}}.bazel"),
build_file = Label("{{workspace.workspace_path}}/remote:BUILD.{{crate.download_name}}-{{crate.pkg_version}}.bazel"),
)
{%- endif %}
{% endfor %}
Expand Down