diff --git a/crate_universe/Cargo.lock b/crate_universe/Cargo.lock index f6fca5f6cd..199ed9716a 100644 --- a/crate_universe/Cargo.lock +++ b/crate_universe/Cargo.lock @@ -198,7 +198,7 @@ dependencies = [ "tempfile", "tera", "textwrap", - "toml 0.8.10", + "toml", "tracing", "tracing-subscriber", "url", @@ -206,13 +206,13 @@ dependencies = [ [[package]] name = "cargo-lock" -version = "9.0.0" +version = "10.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e11c675378efb449ed3ce8de78d75d0d80542fc98487c26aba28eb3b82feac72" +checksum = "6469776d007022d505bbcc2be726f5f096174ae76d710ebc609eb3029a45b551" dependencies = [ "semver", "serde", - "toml 0.7.6", + "toml", "url", ] @@ -246,7 +246,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a98356df42a2eb1bd8f1793ae4ee4de48e384dd974ce5eac8eee802edb7492be" dependencies = [ "serde", - "toml 0.8.10", + "toml", ] [[package]] @@ -336,7 +336,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", ] [[package]] @@ -389,7 +389,7 @@ dependencies = [ "serde_json", "smol_str", "thiserror", - "toml 0.8.10", + "toml", ] [[package]] @@ -986,7 +986,7 @@ checksum = "9d8acb5ee668d55f0f2d19a320a3f9ef67a6999ad483e11135abcc2464ed18b6" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", ] [[package]] @@ -1838,7 +1838,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", ] [[package]] @@ -1910,9 +1910,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.64" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78803b62cbf1f46fde80d7c0e803111524b9877184cfe7c3033659490ac7a7da" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" dependencies = [ "unicode-ident", ] @@ -1925,9 +1925,9 @@ checksum = "794b5bf8e2d19b53dcdcec3e4bba628e20f5b6062503ba89281fa7037dd7bbcf" [[package]] name = "quote" -version = "1.0.29" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "573015e8ab27661678357f27dc26460738fd2b6c86e46f386fde94cb5d913105" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -2134,31 +2134,31 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "semver" -version = "1.0.20" +version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "836fa6a3e1e547f9a2c4040802ec865b5d85f4014efe00555d7090a3dcaa1090" +checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" dependencies = [ "serde", ] [[package]] name = "serde" -version = "1.0.190" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "91d3c334ca1ee894a2c6f6ad698fe8c435b76d504b13d436f0685d648d6d96f7" +checksum = "f55c3193aca71c12ad7890f1785d2b73e1b9f63a0bbc353c08ef26fe03fc56b5" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.190" +version = "1.0.214" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67c5609f394e5c2bd7fc51efda478004ea80ef42fee983d5c67a65e34f32c0e3" +checksum = "de523f781f095e28fa605cdce0f8307e451cc0fd14e2eb4cd2e98a355b147766" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", ] [[package]] @@ -2289,9 +2289,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.32" +version = "2.0.85" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" +checksum = "5023162dfcd14ef8f32034d8bcd4cc5ddc61ef7a247c024a33e24e1f24d21b56" dependencies = [ "proc-macro2", "quote", @@ -2361,7 +2361,7 @@ checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", ] [[package]] @@ -2421,18 +2421,6 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" -[[package]] -name = "toml" -version = "0.7.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c17e963a819c331dcacd7ab957d80bc2b9a9c1e71c804826d2f283dd65306542" -dependencies = [ - "serde", - "serde_spanned", - "toml_datetime", - "toml_edit 0.19.13", -] - [[package]] name = "toml" version = "0.8.10" @@ -2442,7 +2430,7 @@ dependencies = [ "serde", "serde_spanned", "toml_datetime", - "toml_edit 0.22.4", + "toml_edit", ] [[package]] @@ -2454,19 +2442,6 @@ dependencies = [ "serde", ] -[[package]] -name = "toml_edit" -version = "0.19.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f8751d9c1b03c6500c387e96f81f815a4f8e72d142d2d4a9ffa6fedd51ddee7" -dependencies = [ - "indexmap", - "serde", - "serde_spanned", - "toml_datetime", - "winnow", -] - [[package]] name = "toml_edit" version = "0.22.4" @@ -2499,7 +2474,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", ] [[package]] @@ -2723,7 +2698,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", "wasm-bindgen-shared", ] @@ -2745,7 +2720,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.32", + "syn 2.0.85", "wasm-bindgen-backend", "wasm-bindgen-shared", ] diff --git a/crate_universe/Cargo.toml b/crate_universe/Cargo.toml index a038b79d29..03215c37c7 100644 --- a/crate_universe/Cargo.toml +++ b/crate_universe/Cargo.toml @@ -24,7 +24,7 @@ anyhow = "1.0.75" camino = "1.1.6" cargo_metadata = "0.18.1" cargo_toml = "0.19.2" -cargo-lock = "9.0.0" +cargo-lock = "10.0.0" cargo-platform = "0.1.4" cfg-expr = "0.15.5" clap = { version = "4.3.11", features = ["derive", "env"] } diff --git a/crate_universe/private/crates_vendor.bzl b/crate_universe/private/crates_vendor.bzl index a20904fc90..1c3b6db853 100644 --- a/crate_universe/private/crates_vendor.bzl +++ b/crate_universe/private/crates_vendor.bzl @@ -498,12 +498,35 @@ directory next to where the target is defined. To run it, simply call: bazel run //3rdparty:crates_vendor ``` +### Patching + +If you need to replace crates in your third_party dependency tree with forks, the `patch` section can be used in your cargo manifest: + +```toml +[patch.crates-io] +fuse3 = { path = "forks/fuse3" } +``` + +A build file for the patched crate will still be generated under the vendor path +(such as `crates/fuse3-0.6.1/BUILD.bazel`), but `cargo vendor` will NOT download the +crate to that location, and the generated BUILD file will point to your copy. For this +to work, you need to define the following filegroups in your patch crate's BUILD.bazel: +`:srcs`, `:crate_root`, `:compile_data`, and (if the crate has a build script) +`:build_script_crate_root`. See the +[patching example](https://github.com/bazelbuild/rules_rust/tree/main/examples/crate_universe/vendor_local_patching) +for an example of what those BUILD files look like. + +This feature can also be used to avoid vendoring source code for crates that you never +actually use for the platforms you're targeting. In that case you don't need an extra +BUILD file, just crate a empty/stub Cargo.toml and `lib.rs`, and list the path to that +in your `patch` section. + ### Repinning / Updating Dependencies Repinning dependencies is controlled by both the `CARGO_BAZEL_REPIN` environment variable or the `--repin` -flag to the `crates_vendor` binary. To update dependencies, simply add the flag ro your `bazel run` invocation. +flag to the `crates_vendor` binary. To update dependencies, simply add the flag to your `bazel run` invocation. ```shell bazel run //3rdparty:crates_vendor -- --repin diff --git a/crate_universe/src/cli/vendor.rs b/crate_universe/src/cli/vendor.rs index 036b47d50d..4384d26eca 100644 --- a/crate_universe/src/cli/vendor.rs +++ b/crate_universe/src/cli/vendor.rs @@ -125,6 +125,7 @@ pub fn vendor(opt: VendorOptions) -> Result<()> { // Generate a splicer for creating a Cargo workspace manifest let splicer = Splicer::new(PathBuf::from(temp_dir.as_ref()), splicing_manifest) .context("Failed to create splicer")?; + // std::mem::forget(temp_dir); let cargo = Cargo::new(opt.cargo, opt.rustc.clone()); diff --git a/crate_universe/src/context.rs b/crate_universe/src/context.rs index 204bdaff3c..a8acfd5b24 100644 --- a/crate_universe/src/context.rs +++ b/crate_universe/src/context.rs @@ -61,7 +61,7 @@ impl Context { .map(|annotation| { let context = CrateContext::new( annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, diff --git a/crate_universe/src/context/crate_context.rs b/crate_universe/src/context/crate_context.rs index 88cb11bfa2..550f0f34e8 100644 --- a/crate_universe/src/context/crate_context.rs +++ b/crate_universe/src/context/crate_context.rs @@ -1,17 +1,20 @@ //! Crate specific information embedded into [crate::context::Context] objects. use std::collections::{BTreeMap, BTreeSet}; +use std::path::{Path, PathBuf}; -use cargo_metadata::{Node, Package, PackageId}; +use cargo_metadata::{Node, Package, PackageId, Target}; use serde::{Deserialize, Serialize}; use crate::config::{AliasRule, CrateId, GenBinaries}; use crate::metadata::{ - CrateAnnotation, Dependency, PairedExtras, SourceAnnotation, TreeResolverMetadata, + CrateAnnotation, Dependency, MetadataAnnotation, PairedExtras, SourceAnnotation, + TreeResolverMetadata, }; use crate::select::Select; +use crate::splicing::WorkspaceMetadata; use crate::utils::sanitize_module_name; -use crate::utils::starlark::{Glob, Label}; +use crate::utils::starlark::{Glob, GlobOrLabels, Label, Repository}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] pub struct CrateDependency { @@ -42,8 +45,12 @@ pub(crate) struct TargetAttributes { /// The path to the crate's root source file, relative to the manifest. pub(crate) crate_root: Option, - /// A glob pattern of all source files required by the target - pub(crate) srcs: Glob, + /// A glob pattern of all source files required by the target or a label + /// pointing to a filegroup containing said glob (used for patching) + pub(crate) srcs: GlobOrLabels, + + /// A label for overriding compile_data, used for patching + pub compile_data: Option, } #[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)] @@ -345,7 +352,7 @@ impl CrateContext { #[allow(clippy::too_many_arguments)] pub(crate) fn new( annotation: &CrateAnnotation, - packages: &BTreeMap, + metadata: &MetadataAnnotation, source_annotations: &BTreeMap, extras: &BTreeMap, resolver_data: &TreeResolverMetadata, @@ -353,11 +360,11 @@ impl CrateContext { include_build_scripts: bool, sources_are_present: bool, ) -> Self { - let package: &Package = &packages[&annotation.node.id]; + let package: &Package = &metadata.packages[&annotation.node.id]; let current_crate_id = CrateId::new(package.name.clone(), package.version.clone()); let new_crate_dep = |dep: Dependency| -> CrateDependency { - let pkg = &packages[&dep.package_id]; + let pkg = &metadata.packages[&dep.package_id]; // Unfortunately, The package graph and resolve graph of cargo metadata have different representations // for the crate names (resolve graph sanitizes names to match module names) so to get the rest of this @@ -426,7 +433,8 @@ impl CrateContext { // Iterate over each target and produce a Bazel target for all supported "kinds" let targets = Self::collect_targets( &annotation.node, - packages, + &metadata.packages, + &metadata.workspace_metadata, gen_binaries, include_build_scripts, sources_are_present, @@ -697,6 +705,7 @@ impl CrateContext { patch_tool.clone_from(&crate_extra.patch_tool); patches.clone_from(&crate_extra.patches); } + SourceAnnotation::Path => {} } } @@ -752,6 +761,7 @@ impl CrateContext { fn collect_targets( node: &Node, packages: &BTreeMap, + workspace: &WorkspaceMetadata, gen_binaries: &GenBinaries, include_build_scripts: bool, sources_are_present: bool, @@ -768,66 +778,102 @@ impl CrateContext { .targets .iter() .flat_map(|target| { + let attrs = get_attributes(target, package, workspace, package_root); target.kind.iter().filter_map(move |kind| { - // Unfortunately, The package graph and resolve graph of cargo metadata have different representations - // for the crate names (resolve graph sanitizes names to match module names) so to get the rest of this - // content to align when rendering, the package target names are always sanitized. - let crate_name = sanitize_module_name(&target.name); - - // Locate the crate's root source file relative to the package root normalized for unix - let crate_root = pathdiff::diff_paths(&target.src_path, package_root).map( - // Normalize the path so that it always renders the same regardless of platform - |root| root.to_string_lossy().replace('\\', "/"), - ); - - // Conditionally check to see if the dependencies is a build-script target - if include_build_scripts && kind == "custom-build" { - return Some(Rule::BuildScript(TargetAttributes { - crate_name, - crate_root, - srcs: Glob::new_rust_srcs(!sources_are_present), - })); - } - - // Check to see if the dependencies is a proc-macro target if kind == "proc-macro" { - return Some(Rule::ProcMacro(TargetAttributes { - crate_name, - crate_root, - srcs: Glob::new_rust_srcs(!sources_are_present), - })); - } - - // Check to see if the dependencies is a library target - if ["lib", "rlib"].contains(&kind.as_str()) { - return Some(Rule::Library(TargetAttributes { - crate_name, - crate_root, - srcs: Glob::new_rust_srcs(!sources_are_present), - })); - } - - // Check if the target kind is binary and is one of the ones included in gen_binaries - if kind == "bin" - && match gen_binaries { + Some(Rule::ProcMacro(attrs.clone())) + } else if ["lib", "rlib"].contains(&kind.as_str()) { + Some(Rule::Library(attrs.clone())) + } else if include_build_scripts && kind == "custom-build" { + let build_script_crate_root = attrs + .crate_root + .clone() + .map(|s| s.replace(":crate_root", ":build_script_crate_root")); + Some(Rule::BuildScript(TargetAttributes { + crate_root: build_script_crate_root, + ..attrs.clone() + })) + } else if kind == "bin" { + match gen_binaries { GenBinaries::All => true, GenBinaries::Some(set) => set.contains(&target.name), } - { - return Some(Rule::Binary(TargetAttributes { - crate_name: target.name.clone(), - crate_root, - srcs: Glob::new_rust_srcs(!sources_are_present), - })); + .then(|| { + Rule::Binary(TargetAttributes { + crate_name: target.name.clone(), + ..attrs.clone() + }) + }) + } else { + None } - - None }) }) .collect() } } +fn get_attributes( + target: &Target, + package: &Package, + workspace: &WorkspaceMetadata, + package_root: &Path, +) -> TargetAttributes { + // Unfortunately, The package graph and resolve graph of cargo metadata have + // different representations for the crate names (resolve graph sanitizes + // names to match module names) so to get the rest of this content to align + // when rendering, the package target names are always sanitized. + let crate_name = sanitize_module_name(&target.name); + + // Locate the crate's root source file relative to the package root normalized + // for unix + let crate_root = pathdiff::diff_paths(&target.src_path, package_root).map( + // Normalize the path so that it always renders the same regardless of platform + |root| root.to_string_lossy().replace('\\', "/"), + ); + let path_dep = package.id.repr.starts_with("path+file://"); + let temp_components = std::env::temp_dir().components().count() + 1; + let real_root: PathBuf = package_root.components().skip(temp_components).collect(); + if !path_dep || real_root.as_os_str().is_empty() { + TargetAttributes { + crate_name, + crate_root, + srcs: Glob::new_rust_srcs(true).into(), + compile_data: None, + } + } else { + let root = real_root.display(); + let pkg = if let Some(workspace) = &workspace.workspace_prefix { + format!("{workspace}/{}", root) + } else { + root.to_string() + }; + // TODO: remove once added to help-docs + println!("\nThere's a patch crate at '//{pkg}'."); + println!("Make sure that '//{pkg}/BUILD.bazel' exposes the following filegroups:"); + println!( + "'crate_root', 'srcs', 'compile_data', and (if necessary) 'build_script_crate_root'" + ); + let srcs = GlobOrLabels::Labels(vec![Label::Absolute { + repository: Repository::Local, + package: pkg.clone(), + target: "srcs".to_string(), + }]); + let compile_data = Some(GlobOrLabels::Labels(vec![Label::Absolute { + repository: Repository::Local, + package: pkg.clone(), + target: "compile_data".to_string(), + }])); + + TargetAttributes { + crate_name, + crate_root: Some(format!("//{pkg}:crate_root")), + srcs, + compile_data, + } + } +} + #[cfg(test)] mod test { use super::*; @@ -859,7 +905,7 @@ mod test { let are_sources_present = false; let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, @@ -874,7 +920,8 @@ mod test { BTreeSet::from([Rule::Library(TargetAttributes { crate_name: "common".to_owned(), crate_root: Some("lib.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, })]), ); } @@ -907,7 +954,7 @@ mod test { let are_sources_present = false; let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, @@ -923,12 +970,14 @@ mod test { Rule::Library(TargetAttributes { crate_name: "common".to_owned(), crate_root: Some("lib.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, }), Rule::Binary(TargetAttributes { crate_name: "common-bin".to_owned(), crate_root: Some("main.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, }), ]), ); @@ -972,7 +1021,7 @@ mod test { let are_sources_present = false; let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, @@ -989,12 +1038,14 @@ mod test { Rule::Library(TargetAttributes { crate_name: "openssl_sys".to_owned(), crate_root: Some("src/lib.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, }), Rule::BuildScript(TargetAttributes { crate_name: "build_script_main".to_owned(), crate_root: Some("build/main.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, }) ]), ); @@ -1019,7 +1070,7 @@ mod test { let are_sources_present = false; let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, @@ -1035,7 +1086,8 @@ mod test { BTreeSet::from([Rule::Library(TargetAttributes { crate_name: "openssl_sys".to_owned(), crate_root: Some("src/lib.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, })]), ); } @@ -1055,7 +1107,7 @@ mod test { let are_sources_present = false; let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, @@ -1071,7 +1123,8 @@ mod test { BTreeSet::from([Rule::Library(TargetAttributes { crate_name: "sysinfo".to_owned(), crate_root: Some("src/lib.rs".to_owned()), - srcs: Glob::new_rust_srcs(!are_sources_present), + srcs: Glob::new_rust_srcs(!are_sources_present).into(), + compile_data: None, })]), ); } @@ -1097,7 +1150,7 @@ mod test { let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, @@ -1229,7 +1282,7 @@ mod test { let context = CrateContext::new( crate_annotation, - &annotations.metadata.packages, + &annotations.metadata, &annotations.lockfile.crates, &annotations.pairred_extras, &annotations.metadata.workspace_metadata.tree_metadata, diff --git a/crate_universe/src/metadata/metadata_annotation.rs b/crate_universe/src/metadata/metadata_annotation.rs index 2de93d10fd..dfbbbbf20d 100644 --- a/crate_universe/src/metadata/metadata_annotation.rs +++ b/crate_universe/src/metadata/metadata_annotation.rs @@ -162,6 +162,7 @@ pub(crate) enum SourceAnnotation { #[serde(default, skip_serializing_if = "Option::is_none")] patches: Option>, }, + Path, } /// Additional information related to [Cargo.lock](https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html) @@ -238,11 +239,19 @@ impl LockfileAnnotation { patches: None, }) } - None => bail!( - "The package '{:?} {:?}' has no source info so no annotation can be made", - lock_pkg.name, - lock_pkg.version - ), + None => { + // this is a hack to determine whether a dep is patched. cargo + // metadata doesn't expose that info other than as part of the + // technically opaque id field + if pkg.id.repr.starts_with("path+file://") { + return Ok(SourceAnnotation::Path); + } + bail!( + "The package '{:?} {:?}' has no source info so no annotation can be made", + lock_pkg.name, + lock_pkg.version + ); + } }, }; diff --git a/crate_universe/src/rendering.rs b/crate_universe/src/rendering.rs index 2b6ca320cf..38b56c6785 100644 --- a/crate_universe/src/rendering.rs +++ b/crate_universe/src/rendering.rs @@ -454,13 +454,19 @@ impl Renderer { .unwrap_or_default(), platforms, ), - compile_data: make_data( - platforms, - Default::default(), - attrs - .map(|attrs| attrs.compile_data.clone()) - .unwrap_or_default(), - ), + compile_data: { + let mut data = make_data( + platforms, + Default::default(), + attrs + .map(|attrs| attrs.compile_data.clone()) + .unwrap_or_default(), + ); + if let Some(cd) = &target.compile_data { + data.glob = cd.clone(); + } + data + }, crate_features: SelectSet::new(krate.common_attrs.crate_features.clone(), platforms), crate_name: utils::sanitize_module_name(&target.crate_name), crate_root: target.crate_root.clone(), @@ -650,11 +656,17 @@ impl Renderer { target: &TargetAttributes, ) -> Result { Ok(CommonAttrs { - compile_data: make_data( - platforms, - krate.common_attrs.compile_data_glob.clone(), - krate.common_attrs.compile_data.clone(), - ), + compile_data: { + let mut data = make_data( + platforms, + krate.common_attrs.compile_data_glob.clone(), + krate.common_attrs.compile_data.clone(), + ); + if let Some(cd) = &target.compile_data { + data.glob = cd.clone(); + } + data + }, crate_features: SelectSet::new(krate.common_attrs.crate_features.clone(), platforms), crate_root: target.crate_root.clone(), data: make_data( @@ -892,7 +904,8 @@ fn make_data( .iter() .map(|&glob| glob.to_owned()) .collect(), - }, + } + .into(), select: SelectSet::new(select, platforms), } } diff --git a/crate_universe/src/utils.rs b/crate_universe/src/utils.rs index d625e0572c..9c4b297651 100644 --- a/crate_universe/src/utils.rs +++ b/crate_universe/src/utils.rs @@ -8,6 +8,7 @@ pub(crate) const CRATES_IO_INDEX_URL: &str = "https://github.com/rust-lang/crate use std::collections::BTreeMap; use std::path::{Path, PathBuf}; +use std::fs; /// Convert a string into a valid crate module name by applying transforms to invalid characters pub(crate) fn sanitize_module_name(name: &str) -> String { @@ -23,7 +24,7 @@ pub(crate) fn sanitize_repository_name(name: &str) -> String { /// Vendored crates are generated by cargo itself in `src/metadata.rs` in the /// `VendorGenerator::generate()` method. This means that the semver metadata will -/// always contain a (+) symbol, which is not compatible with bazel's labels. +/// always contain a (+) symbol, which is not compatible with bazel's labels. /// This function will rename the cargo vendor generated file paths to be compatible with bazel /// labels by simply replacing the (+) with a (-). If this file is called by any other cli mod, /// it just simply joins the out dir to the path @@ -45,8 +46,10 @@ pub(crate) fn normalize_cargo_file_paths( let new_path = if original_parent_path_str.contains('+') { let new_parent_file_path = sanitize_repository_name(original_parent_path_str); - std::fs::rename(original_parent_path_str, new_parent_file_path) - .expect("Could not rename paths"); + if fs::exists(original_parent_path_str).unwrap_or_default() { + std::fs::rename(original_parent_path_str, new_parent_file_path) + .expect("Could not rename paths"); + } PathBuf::from(&original_path_str.replace('+', "-")) } else { path diff --git a/crate_universe/src/utils/starlark.rs b/crate_universe/src/utils/starlark.rs index 55ec583c0a..33eaecf9ec 100644 --- a/crate_universe/src/utils/starlark.rs +++ b/crate_universe/src/utils/starlark.rs @@ -126,7 +126,7 @@ pub(crate) struct CargoBuildScript { pub(crate) rustc_env_files: SelectSet, #[serde(skip_serializing_if = "SelectList::is_empty")] pub(crate) rustc_flags: SelectList, - pub(crate) srcs: Glob, + pub(crate) srcs: GlobOrLabels, #[serde(skip_serializing_if = "Set::is_empty")] pub(crate) tags: Set, #[serde(skip_serializing_if = "SelectSet::is_empty")] @@ -197,7 +197,7 @@ pub(crate) struct CommonAttrs { pub(crate) rustc_env_files: SelectSet, #[serde(skip_serializing_if = "SelectList::is_empty")] pub(crate) rustc_flags: SelectList, - pub(crate) srcs: Glob, + pub(crate) srcs: GlobOrLabels, #[serde(skip_serializing_if = "Set::is_empty")] pub(crate) tags: Set, #[serde(skip_serializing_if = "Option::is_none")] @@ -206,7 +206,7 @@ pub(crate) struct CommonAttrs { } pub(crate) struct Data { - pub(crate) glob: Glob, + pub(crate) glob: GlobOrLabels, pub(crate) select: SelectSet