Skip to content

Commit d86faee

Browse files
authored
Adds support for bindeps in crate universe (#2226)
Fixes #1739, #2031, #2168.
1 parent d5d7fb8 commit d86faee

File tree

16 files changed

+2201
-51
lines changed

16 files changed

+2201
-51
lines changed

crate_universe/Cargo.lock

Lines changed: 32 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate_universe/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ default = ["cargo"]
2727
[dependencies]
2828
anyhow = "1.0.71"
2929
cargo_metadata = "0.15.4"
30-
cargo_toml = "0.15.2"
30+
cargo_toml = "0.17.0"
3131
cargo-lock = "9.0.0"
3232
cargo-platform = "0.1.2"
3333
cfg-expr = "0.15.0"
@@ -47,7 +47,7 @@ sha2 = "0.10.7"
4747
tempfile = "3.6.0"
4848
tera = "1.19.0"
4949
textwrap = "0.16.0"
50-
toml = "0.7.6"
50+
toml = "0.8.0"
5151
tracing = "0.1.40"
5252
tracing-subscriber = "0.3.17"
5353

crate_universe/src/metadata.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,17 @@ impl MetadataGenerator for Generator {
7575
cargo_lock::Lockfile::load(lock_path)?
7676
};
7777

78+
let mut other_options = vec!["--locked".to_owned()];
79+
if self.cargo_bin.is_nightly()? {
80+
other_options.push("-Zbindeps".to_owned());
81+
}
82+
7883
let metadata = self
7984
.cargo_bin
8085
.metadata_command()?
8186
.current_dir(manifest_dir)
8287
.manifest_path(manifest_path.as_ref())
83-
.other_options(["--locked".to_owned()])
88+
.other_options(other_options)
8489
.exec()?;
8590

8691
Ok((metadata, lockfile))
@@ -108,6 +113,9 @@ impl Cargo {
108113
pub fn command(&self) -> Result<Command> {
109114
let mut command = Command::new(&self.path);
110115
command.envs(self.env()?);
116+
if self.is_nightly()? {
117+
command.arg("-Zbindeps");
118+
}
111119
Ok(command)
112120
}
113121

@@ -132,6 +140,16 @@ impl Cargo {
132140
Ok(full_version.clone().unwrap())
133141
}
134142

143+
pub fn is_nightly(&self) -> Result<bool> {
144+
let full_version = self.full_version()?;
145+
let version_str = full_version.split(' ').nth(1);
146+
if let Some(version_str) = version_str {
147+
let version = Version::parse(version_str).context("Failed to parse cargo version")?;
148+
return Ok(version.pre.as_str() == "nightly");
149+
}
150+
bail!("Couldn't parse cargo version");
151+
}
152+
135153
pub fn use_sparse_registries_for_crates_io(&self) -> Result<bool> {
136154
let full_version = self.full_version()?;
137155
let version_str = full_version.split(' ').nth(1);

examples/crate_universe/WORKSPACE.bazel

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,28 @@ load(
4646

4747
cargo_aliases_crate_repositories()
4848

49+
###############################################################################
50+
# C A R G O B I N D E P S
51+
###############################################################################
52+
53+
crates_repository(
54+
name = "crate_index_cargo_bindeps",
55+
cargo_lockfile = "//cargo_bindeps:Cargo.lock",
56+
generate_binaries = True,
57+
# `generator` is not necessary in official releases.
58+
# See load satement for `cargo_bazel_bootstrap`.
59+
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
60+
manifests = ["//cargo_bindeps:Cargo.toml"],
61+
rust_version = "nightly",
62+
)
63+
64+
load(
65+
"@crate_index_cargo_bindeps//:defs.bzl",
66+
cargo_bindeps_crate_repositories = "crate_repositories",
67+
)
68+
69+
cargo_bindeps_crate_repositories()
70+
4971
###############################################################################
5072
# C A R G O L O C A L
5173
###############################################################################

examples/crate_universe/cargo_aliases/cargo-bazel-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
2+
3+
alias(
4+
name = "hyperfine",
5+
actual = "@crate_index_cargo_bindeps//:hyperfine__hyperfine",
6+
tags = ["manual"],
7+
)
8+
9+
rust_library(
10+
name = "bindeps",
11+
srcs = glob(["**/*.rs"]),
12+
data = [":hyperfine"],
13+
edition = "2018",
14+
rustc_env = {
15+
"HYPERFINE": "$(rootpath :hyperfine)",
16+
},
17+
)
18+
19+
rust_test(
20+
name = "unit_test",
21+
crate = ":bindeps",
22+
edition = "2018",
23+
)

0 commit comments

Comments
 (0)