Skip to content

Support local path patching in vendor mode #3274

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

Merged
merged 3 commits into from
Mar 10, 2025
Merged
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
1 change: 1 addition & 0 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ crate_universe_vendor_example_targets: &crate_universe_vendor_example_targets
- "//vendor_external:crates_vendor"
- "//vendor_local_manifests:crates_vendor"
- "//vendor_local_pkgs:crates_vendor"
- "//vendor_local_patching:crates_vendor"
- "//vendor_remote_manifests:crates_vendor"
- "//vendor_remote_pkgs:crates_vendor"
- "@rules_rust//tools/rust_analyzer:gen_rust_project"
Expand Down
1 change: 1 addition & 0 deletions crate_universe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ exclude = [
"test_data/metadata/multi_kind_proc_macro_dep",
"test_data/metadata/nested_build_dependencies",
"test_data/metadata/no_deps",
"test_data/metadata/path_patching",
"test_data/metadata/resolver_2_deps",
"test_data/metadata/target_cfg_features",
"test_data/metadata/target_features",
Expand Down
3 changes: 2 additions & 1 deletion crate_universe/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -713,9 +713,9 @@ def _generate_hub_and_spokes(
version = version.replace("+", "-"),
)

build_file_content = module_ctx.read(crates_dir.get_child("BUILD.%s-%s.bazel" % (name, version)))
if "Http" in repo:
# Replicates functionality in repo_http.j2.
build_file_content = module_ctx.read(crates_dir.get_child("BUILD.%s-%s.bazel" % (name, version)))
repo = repo["Http"]
http_archive(
name = crate_repo_name,
Expand All @@ -731,6 +731,7 @@ def _generate_hub_and_spokes(
)
elif "Git" in repo:
# Replicates functionality in repo_git.j2
build_file_content = module_ctx.read(crates_dir.get_child("BUILD.%s-%s.bazel" % (name, version)))
repo = repo["Git"]
kwargs = {}
for k, v in repo["commitish"].items():
Expand Down
2 changes: 2 additions & 0 deletions crate_universe/src/api/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ mod test {
},
target: String::from("anyhow"),
alias: None,
source_annotation: None,
},
CrateDependency {
id: CrateId {
Expand All @@ -184,6 +185,7 @@ mod test {
},
target: String::from("reqwest"),
alias: None,
source_annotation: None,
},
],
);
Expand Down
13 changes: 12 additions & 1 deletion crate_universe/src/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,22 @@ pub fn generate(opt: GenerateOptions) -> Result<()> {
};

// Load Metadata and Lockfile
let (cargo_metadata, cargo_lockfile) = load_metadata(metadata_path)?;
let lockfile_path = metadata_path
.parent()
.expect("metadata files should always have parents")
.join("Cargo.lock");
if !lockfile_path.exists() {
bail!(
"The metadata file at {} is not next to a `Cargo.lock` file.",
metadata_path.display()
)
}
let (cargo_metadata, cargo_lockfile) = load_metadata(metadata_path, &lockfile_path)?;

// Annotate metadata
let annotations = Annotations::new(
cargo_metadata,
&Some(lockfile_path),
cargo_lockfile.clone(),
config.clone(),
&opt.nonhermetic_root_bazel_workspace_dir,
Expand Down
1 change: 1 addition & 0 deletions crate_universe/src/cli/vendor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub fn vendor(opt: VendorOptions) -> anyhow::Result<()> {
// Annotate metadata
let annotations = Annotations::new(
cargo_metadata,
&opt.cargo_lockfile,
cargo_lockfile.clone(),
config.clone(),
&opt.nonhermetic_root_bazel_workspace_dir,
Expand Down
3 changes: 3 additions & 0 deletions crate_universe/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ mod test {
fn mock_context_common() -> Context {
let annotations = Annotations::new(
crate::test::metadata::common(),
&None,
crate::test::lockfile::common(),
Config::default(),
Utf8Path::new("/tmp/bazelworkspace"),
Expand All @@ -281,6 +282,7 @@ mod test {
fn mock_context_aliases() -> Context {
let annotations = Annotations::new(
crate::test::metadata::alias(),
&None,
crate::test::lockfile::alias(),
Config::default(),
Utf8Path::new("/tmp/bazelworkspace"),
Expand All @@ -293,6 +295,7 @@ mod test {
fn mock_context_workspace_build_scripts_deps() -> Context {
let annotations = Annotations::new(
crate::test::metadata::workspace_build_scripts_deps(),
&None,
crate::test::lockfile::workspace_build_scripts_deps(),
Config {
generate_build_scripts: true,
Expand Down
10 changes: 10 additions & 0 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub struct CrateDependency {
/// Some dependencies are assigned aliases. This is tracked here
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,

/// Where to acquire the source of this dependency.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) source_annotation: Option<SourceAnnotation>,
}

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -378,6 +382,7 @@ impl CrateContext {
id: CrateId::new(pkg.name.clone(), pkg.version.clone()),
target,
alias: dep.alias,
source_annotation: Some(source_annotations[&dep.package_id].clone()),
}
};

Expand Down Expand Up @@ -475,6 +480,7 @@ impl CrateContext {
id: current_crate_id,
target: target.crate_name.clone(),
alias: None,
source_annotation: source_annotations.get(&annotation.node.id).cloned(),
},
None,
);
Expand Down Expand Up @@ -874,6 +880,7 @@ mod test {
fn common_annotations() -> Annotations {
Annotations::new(
crate::test::metadata::common(),
&None,
crate::test::lockfile::common(),
crate::config::Config::default(),
Utf8Path::new("/tmp/bazelworkspace"),
Expand Down Expand Up @@ -978,6 +985,7 @@ mod test {
fn build_script_annotations() -> Annotations {
Annotations::new(
crate::test::metadata::build_scripts(),
&None,
crate::test::lockfile::build_scripts(),
crate::config::Config::default(),
Utf8Path::new("/tmp/bazelworkspace"),
Expand All @@ -988,6 +996,7 @@ mod test {
fn crate_type_annotations() -> Annotations {
Annotations::new(
crate::test::metadata::crate_types(),
&None,
crate::test::lockfile::crate_types(),
crate::config::Config::default(),
Utf8Path::new("/tmp/bazelworkspace"),
Expand Down Expand Up @@ -1292,6 +1301,7 @@ mod test {
fn absolute_paths_for_srcs_are_errors() {
let annotations = Annotations::new(
crate::test::metadata::abspath(),
&None,
crate::test::lockfile::abspath(),
crate::config::Config::default(),
Utf8Path::new("/tmp/bazelworkspace"),
Expand Down
4 changes: 4 additions & 0 deletions crate_universe/src/context/platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
source_annotation: None,
},
None,
);
Expand Down Expand Up @@ -193,6 +194,7 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
source_annotation: None,
},
Some(configuration),
);
Expand Down Expand Up @@ -279,6 +281,7 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
source_annotation: None,
},
Some(configuration.clone()),
);
Expand Down Expand Up @@ -345,6 +348,7 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
source_annotation: None,
},
Some(configuration.clone()),
);
Expand Down
15 changes: 2 additions & 13 deletions crate_universe/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,26 +353,15 @@ pub(crate) fn write_metadata(path: &Path, metadata: &cargo_metadata::Metadata) -
/// A helper function for deserializing Cargo metadata and lockfiles
pub(crate) fn load_metadata(
metadata_path: &Path,
lockfile_path: &Path,
) -> Result<(cargo_metadata::Metadata, cargo_lock::Lockfile)> {
// Locate the Cargo.lock file related to the metadata file.
let lockfile_path = metadata_path
.parent()
.expect("metadata files should always have parents")
.join("Cargo.lock");
if !lockfile_path.exists() {
bail!(
"The metadata file at {} is not next to a `Cargo.lock` file.",
metadata_path.display()
)
}

let content = fs::read_to_string(metadata_path)
.with_context(|| format!("Failed to load Cargo Metadata: {}", metadata_path.display()))?;

let metadata =
serde_json::from_str(&content).context("Unable to deserialize Cargo metadata")?;

let lockfile = cargo_lock::Lockfile::load(&lockfile_path)
let lockfile = cargo_lock::Lockfile::load(lockfile_path)
.with_context(|| format!("Failed to load lockfile: {}", lockfile_path.display()))?;

Ok((metadata, lockfile))
Expand Down
Loading