Skip to content

Commit af4dae8

Browse files
committed
Followup
1. Address review comment 2. Fix/add unit tests: bazelisk test //... 3. Fix clippy: bazelisk build //... --config=clippy 4. Fix path patching tests: bazelisk run //:vendor_edit_test_out_of_tree a. Recognize when patched path is from a temporary Bazel dir, and rebase properly back to Bazel workspace (previous logic). b. Don't try to read BUILD.bazel when path patching in extensions.bzl, since the BUILD file is writted directly to patched path, which does not necessarily follow the convention (naming and location) of other generated build files. Change-Id: Ie3cddbafb7b524c0ea70494d76905475be1fc742
1 parent 2055f4c commit af4dae8

File tree

24 files changed

+1254
-21
lines changed

24 files changed

+1254
-21
lines changed

crate_universe/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ exclude = [
2121
"test_data/metadata/multi_kind_proc_macro_dep",
2222
"test_data/metadata/nested_build_dependencies",
2323
"test_data/metadata/no_deps",
24+
"test_data/metadata/path_patching",
2425
"test_data/metadata/resolver_2_deps",
2526
"test_data/metadata/target_cfg_features",
2627
"test_data/metadata/target_features",

crate_universe/extensions.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,9 +713,9 @@ def _generate_hub_and_spokes(
713713
version = version.replace("+", "-"),
714714
)
715715

716-
build_file_content = module_ctx.read(crates_dir.get_child("BUILD.%s-%s.bazel" % (name, version)))
717716
if "Http" in repo:
718717
# Replicates functionality in repo_http.j2.
718+
build_file_content = module_ctx.read(crates_dir.get_child("BUILD.%s-%s.bazel" % (name, version)))
719719
repo = repo["Http"]
720720
http_archive(
721721
name = crate_repo_name,
@@ -731,6 +731,7 @@ def _generate_hub_and_spokes(
731731
)
732732
elif "Git" in repo:
733733
# Replicates functionality in repo_git.j2
734+
build_file_content = module_ctx.read(crates_dir.get_child("BUILD.%s-%s.bazel" % (name, version)))
734735
repo = repo["Git"]
735736
kwargs = {}
736737
for k, v in repo["commitish"].items():

crate_universe/src/api/lockfile.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ mod test {
176176
},
177177
target: String::from("anyhow"),
178178
alias: None,
179+
source_annotation: None,
179180
},
180181
CrateDependency {
181182
id: CrateId {
@@ -184,6 +185,7 @@ mod test {
184185
},
185186
target: String::from("reqwest"),
186187
alias: None,
188+
source_annotation: None,
187189
},
188190
],
189191
);

crate_universe/src/context.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ mod test {
269269
fn mock_context_common() -> Context {
270270
let annotations = Annotations::new(
271271
crate::test::metadata::common(),
272+
&None,
272273
crate::test::lockfile::common(),
273274
Config::default(),
274275
Utf8Path::new("/tmp/bazelworkspace"),
@@ -281,6 +282,7 @@ mod test {
281282
fn mock_context_aliases() -> Context {
282283
let annotations = Annotations::new(
283284
crate::test::metadata::alias(),
285+
&None,
284286
crate::test::lockfile::alias(),
285287
Config::default(),
286288
Utf8Path::new("/tmp/bazelworkspace"),
@@ -293,6 +295,7 @@ mod test {
293295
fn mock_context_workspace_build_scripts_deps() -> Context {
294296
let annotations = Annotations::new(
295297
crate::test::metadata::workspace_build_scripts_deps(),
298+
&None,
296299
crate::test::lockfile::workspace_build_scripts_deps(),
297300
Config {
298301
generate_build_scripts: true,

crate_universe/src/context/crate_context.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ pub struct CrateDependency {
2626
#[serde(default, skip_serializing_if = "Option::is_none")]
2727
pub alias: Option<String>,
2828

29+
/// Where to acquire the source of this dependency.
2930
#[serde(default, skip_serializing_if = "Option::is_none")]
3031
pub(crate) source_annotation: Option<SourceAnnotation>,
3132
}
@@ -479,10 +480,7 @@ impl CrateContext {
479480
id: current_crate_id,
480481
target: target.crate_name.clone(),
481482
alias: None,
482-
source_annotation: match source_annotations.get(&annotation.node.id) {
483-
Some(source_annotation) => Some(source_annotation.clone()),
484-
None => None,
485-
},
483+
source_annotation: source_annotations.get(&annotation.node.id).cloned(),
486484
},
487485
None,
488486
);
@@ -882,6 +880,7 @@ mod test {
882880
fn common_annotations() -> Annotations {
883881
Annotations::new(
884882
crate::test::metadata::common(),
883+
&None,
885884
crate::test::lockfile::common(),
886885
crate::config::Config::default(),
887886
Utf8Path::new("/tmp/bazelworkspace"),
@@ -986,6 +985,7 @@ mod test {
986985
fn build_script_annotations() -> Annotations {
987986
Annotations::new(
988987
crate::test::metadata::build_scripts(),
988+
&None,
989989
crate::test::lockfile::build_scripts(),
990990
crate::config::Config::default(),
991991
Utf8Path::new("/tmp/bazelworkspace"),
@@ -996,6 +996,7 @@ mod test {
996996
fn crate_type_annotations() -> Annotations {
997997
Annotations::new(
998998
crate::test::metadata::crate_types(),
999+
&None,
9991000
crate::test::lockfile::crate_types(),
10001001
crate::config::Config::default(),
10011002
Utf8Path::new("/tmp/bazelworkspace"),
@@ -1300,6 +1301,7 @@ mod test {
13001301
fn absolute_paths_for_srcs_are_errors() {
13011302
let annotations = Annotations::new(
13021303
crate::test::metadata::abspath(),
1304+
&None,
13031305
crate::test::lockfile::abspath(),
13041306
crate::config::Config::default(),
13051307
Utf8Path::new("/tmp/bazelworkspace"),

crate_universe/src/context/platforms.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ mod test {
135135
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
136136
target: "mock_crate_b".to_owned(),
137137
alias: None,
138+
source_annotation: None,
138139
},
139140
None,
140141
);
@@ -193,6 +194,7 @@ mod test {
193194
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
194195
target: "mock_crate_b".to_owned(),
195196
alias: None,
197+
source_annotation: None,
196198
},
197199
Some(configuration),
198200
);
@@ -279,6 +281,7 @@ mod test {
279281
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
280282
target: "mock_crate_b".to_owned(),
281283
alias: None,
284+
source_annotation: None,
282285
},
283286
Some(configuration.clone()),
284287
);
@@ -345,6 +348,7 @@ mod test {
345348
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
346349
target: "mock_crate_b".to_owned(),
347350
alias: None,
351+
source_annotation: None,
348352
},
349353
Some(configuration.clone()),
350354
);

crate_universe/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ pub(crate) fn load_metadata(
361361
let metadata =
362362
serde_json::from_str(&content).context("Unable to deserialize Cargo metadata")?;
363363

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

367367
Ok((metadata, lockfile))

crate_universe/src/metadata/metadata_annotation.rs

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::collections::{BTreeMap, BTreeSet};
44
use std::path::PathBuf;
55

6-
use anyhow::{bail, Result};
6+
use anyhow::{bail, Context, Result};
77
use camino::{Utf8Path, Utf8PathBuf};
88
use cargo_metadata::{Node, Package, PackageId};
99
use hex::ToHex;
@@ -277,21 +277,28 @@ impl LockfileAnnotation {
277277
// its parent directory (relative to Bazel workspace root),
278278
// since `suffix` is relative to it in the actual Bazel
279279
// workspace.
280-
let relative_lockfile_path = Utf8Path::from_path(
281-
path.parent().expect("unexpected empty lockfile path"),
280+
let p = Utf8Path::from_path(
281+
path.parent()
282+
.context("unexpected empty lockfile path")?,
282283
)
283-
.expect("unxpected non-Unicode lockfile path")
284-
.strip_prefix(nonhermetic_root_bazel_workspace_dir)
285-
.expect(
286-
"unexpected lockfile path no under root Bazel workspace",
287-
);
288-
new_path.push(relative_lockfile_path);
289-
} else if let Some(prefix) =
290-
// Lockfile path includes workspace prefix, so avoid
291-
// duplicating it here.
292-
workspace_metadata.workspace_prefix.as_ref()
293-
{
294-
new_path.push(prefix);
284+
.context("unxpected non-Unicode lockfile path")?;
285+
if p.starts_with(nonhermetic_root_bazel_workspace_dir) {
286+
// If path in lockfile is under Bazel root, strip Bazel
287+
// root to get the actual path.
288+
let relative_lockfile_path = p.strip_prefix(nonhermetic_root_bazel_workspace_dir)
289+
.context("unexpected lockfile path not under root Bazel workspace")?;
290+
new_path.push(relative_lockfile_path);
291+
} else {
292+
// If path in lockfile is not under Bazel root, we are
293+
// likely in a temporary directory, so rebase to Bazel
294+
// root.
295+
new_path.push(nonhermetic_root_bazel_workspace_dir);
296+
if let Some(prefix) =
297+
workspace_metadata.workspace_prefix.as_ref()
298+
{
299+
new_path.push(prefix);
300+
}
301+
}
295302
}
296303
new_path.push(suffix);
297304
new_path
@@ -581,6 +588,7 @@ mod test {
581588
#[test]
582589
fn annotate_lockfile_with_aliases() {
583590
LockfileAnnotation::new(
591+
&None,
584592
test::lockfile::alias(),
585593
&test::metadata::alias(),
586594
Utf8Path::new("/tmp/bazelworkspace"),
@@ -596,6 +604,7 @@ mod test {
596604
#[test]
597605
fn annotate_lockfile_with_build_scripts() {
598606
LockfileAnnotation::new(
607+
&None,
599608
test::lockfile::build_scripts(),
600609
&test::metadata::build_scripts(),
601610
Utf8Path::new("/tmp/bazelworkspace"),
@@ -606,6 +615,7 @@ mod test {
606615
#[test]
607616
fn annotate_lockfile_with_no_deps() {
608617
LockfileAnnotation::new(
618+
&None,
609619
test::lockfile::no_deps(),
610620
&test::metadata::no_deps(),
611621
Utf8Path::new("/tmp/bazelworkspace"),
@@ -616,6 +626,7 @@ mod test {
616626
#[test]
617627
fn detects_strip_prefix_for_git_repo() {
618628
let crates = LockfileAnnotation::new(
629+
&None,
619630
test::lockfile::git_repos(),
620631
&test::metadata::git_repos(),
621632
Utf8Path::new("/tmp/bazelworkspace"),
@@ -643,6 +654,7 @@ mod test {
643654
#[test]
644655
fn resolves_commit_from_branches_and_tags() {
645656
let crates = LockfileAnnotation::new(
657+
&None,
646658
test::lockfile::git_repos(),
647659
&test::metadata::git_repos(),
648660
Utf8Path::new("/tmp/bazelworkspace"),
@@ -666,6 +678,27 @@ mod test {
666678
);
667679
}
668680

681+
#[test]
682+
fn detects_local_path_patching() {
683+
let crates = LockfileAnnotation::new(
684+
&None,
685+
test::lockfile::path_patching(),
686+
&test::metadata::path_patching(),
687+
Utf8Path::new("/tmp/bazelworkspace"),
688+
)
689+
.unwrap()
690+
.crates;
691+
692+
// We can't reliably construct the PackageId because it'll contain local absolute paths,
693+
// so compare the entire container, which should only have one element anyways.
694+
assert_eq!(
695+
crates.into_values().collect::<Vec<_>>(),
696+
[SourceAnnotation::Path {
697+
path: "child_a".into()
698+
}]
699+
);
700+
}
701+
669702
#[test]
670703
fn detect_unused_annotation() {
671704
// Create a config with some random annotation
@@ -677,6 +710,7 @@ mod test {
677710

678711
let result = Annotations::new(
679712
test::metadata::no_deps(),
713+
&None,
680714
test::lockfile::no_deps(),
681715
config,
682716
Utf8Path::new("/tmp/bazelworkspace"),
@@ -715,6 +749,7 @@ mod test {
715749
// crate author in package metadata.
716750
let combined_annotations = Annotations::new(
717751
test::metadata::has_package_metadata(),
752+
&None,
718753
test::lockfile::has_package_metadata(),
719754
config,
720755
Utf8Path::new("/tmp/bazelworkspace"),

crate_universe/src/rendering.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,7 @@ mod test {
13871387
};
13881388
let annotations = Annotations::new(
13891389
test::metadata::alias(),
1390+
&None,
13901391
test::lockfile::alias(),
13911392
config,
13921393
Utf8Path::new("/tmp/bazelworkspace"),
@@ -1599,6 +1600,7 @@ mod test {
15991600

16001601
let annotations = Annotations::new(
16011602
metadata,
1603+
&None,
16021604
lockfile,
16031605
config.clone(),
16041606
Utf8Path::new("/tmp/bazelworkspace"),
@@ -1987,6 +1989,7 @@ mod test {
19871989
// this is identical to what we have in the `name` attribute
19881990
// which creates conflict in `render_module_build_file`
19891991
alias: Some("mock_crate".into()),
1992+
source_annotation: None,
19901993
}])),
19911994
..Default::default()
19921995
},

crate_universe/src/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ pub(crate) mod metadata {
109109
.unwrap()
110110
}
111111

112+
pub(crate) fn path_patching() -> cargo_metadata::Metadata {
113+
serde_json::from_str(include_str!(concat!(
114+
env!("CARGO_MANIFEST_DIR"),
115+
"/test_data/metadata/path_patching/metadata.json"
116+
)))
117+
.unwrap()
118+
}
119+
112120
pub(crate) fn optional_deps_disabled() -> cargo_metadata::Metadata {
113121
serde_json::from_str(include_str!(concat!(
114122
env!("CARGO_MANIFEST_DIR"),
@@ -249,6 +257,14 @@ pub(crate) mod lockfile {
249257
.unwrap()
250258
}
251259

260+
pub(crate) fn path_patching() -> cargo_lock::Lockfile {
261+
cargo_lock::Lockfile::from_str(include_str!(concat!(
262+
env!("CARGO_MANIFEST_DIR"),
263+
"/test_data/metadata/path_patching/Cargo.lock"
264+
)))
265+
.unwrap()
266+
}
267+
252268
pub(crate) fn common() -> cargo_lock::Lockfile {
253269
cargo_lock::Lockfile::from_str(include_str!(concat!(
254270
env!("CARGO_MANIFEST_DIR"),

crate_universe/test_data/metadata/path_patching/Cargo.lock

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "path_patching"
3+
version = "0.1.0"
4+
edition = "2018"
5+
6+
# Required to satisfy cargo but no `lib.rs` is expected to
7+
# exist within test data.
8+
[lib]
9+
path = "lib.rs"
10+
11+
[dependencies]
12+
child_a = "1.4.2"
13+
14+
[patch.crates-io]
15+
child_a = { path = "child_a" }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "child_a"
3+
version = "1.4.2"
4+
edition = "2018"
5+
6+
# Required to satisfy cargo but no `lib.rs` is expected to
7+
# exist within test data.
8+
[lib]
9+
path = "lib.rs"
10+
11+
[dependencies]

0 commit comments

Comments
 (0)