Skip to content

Commit fb616b4

Browse files
committed
Crate Universe: crate.spec() supports path
1 parent 0cb272d commit fb616b4

File tree

13 files changed

+166
-89
lines changed

13 files changed

+166
-89
lines changed

crate_universe/extensions.bzl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ _spec = tag_class(
12291229
doc = "A list of features to use for the crate.",
12301230
),
12311231
"git": attr.string(
1232-
doc = "The Git url to use for the crate. Cannot be used with `version`.",
1232+
doc = "The Git url to use for the crate. Cannot be used with `version` or `path`.",
12331233
),
12341234
"lib": attr.bool(
12351235
doc = "If using `artifact = 'bin'`, additionally setting `lib = True` declares a dependency on both the package's library and binary, as opposed to just the binary.",
@@ -1238,6 +1238,9 @@ _spec = tag_class(
12381238
doc = "The explicit name of the package.",
12391239
mandatory = True,
12401240
),
1241+
"path": attr.string(
1242+
doc = "The local path of the remote crate. Cannot be used with `version` or `git`.",
1243+
),
12411244
"repositories": attr.string_list(
12421245
doc = "A list of repository names specified from `crate.from_cargo(name=...)` that this spec is applied to. Defaults to all repositories.",
12431246
default = [],
@@ -1249,7 +1252,7 @@ _spec = tag_class(
12491252
doc = "The git tag of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds.",
12501253
),
12511254
"version": attr.string(
1252-
doc = "The exact version of the crate. Cannot be used with `git`.",
1255+
doc = "The exact version of the crate. Cannot be used with `git` or `path`.",
12531256
),
12541257
},
12551258
)

crate_universe/private/crate.bzl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ def _spec(
2727
git = None,
2828
branch = None,
2929
tag = None,
30-
rev = None):
30+
rev = None,
31+
path = None):
3132
"""A constructor for a crate dependency.
3233
3334
See [specifying dependencies][sd] in the Cargo book for more details.
@@ -36,15 +37,16 @@ def _spec(
3637
3738
Args:
3839
package (str, optional): The explicit name of the package (used when attempting to alias a crate).
39-
version (str, optional): The exact version of the crate. Cannot be used with `git`.
40+
version (str, optional): The exact version of the crate. Cannot be used with `git` or `path`.
4041
artifact (str, optional): Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo.
4142
lib (bool, optional): If using `artifact = "bin"`, additionally setting `lib = True` declares a dependency on both the package's library and binary, as opposed to just the binary.
4243
default_features (bool, optional): Maps to the `default-features` flag.
4344
features (list, optional): A list of features to use for the crate
44-
git (str, optional): The Git url to use for the crate. Cannot be used with `version`.
45+
git (str, optional): The Git url to use for the crate. Cannot be used with `version` or `path`.
4546
branch (str, optional): The git branch of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds.
4647
tag (str, optional): The git tag of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified. Specifying `rev` is recommended for fully-reproducible builds.
4748
rev (str, optional): The git revision of the remote crate. Tied with the `git` param. Only one of branch, tag or rev may be specified.
49+
path (str, optional): The local path of the remote crate. Cannot be used with `version` or `git`.
4850
4951
Returns:
5052
string: A json encoded string of all inputs
@@ -59,6 +61,7 @@ def _spec(
5961
"git": git,
6062
"lib": lib,
6163
"package": package,
64+
"path": path,
6265
"rev": rev,
6366
"tag": tag,
6467
"version": version,

crate_universe/src/rendering.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -860,9 +860,8 @@ impl Renderer {
860860
extra_deps: Select<BTreeSet<Label>>,
861861
) -> Select<BTreeSet<Label>> {
862862
Select::merge(
863-
deps.map(|dep| match dep.local_path {
864-
Some(path) => Label::from_str(&format!("//{}:{}", path, &dep.target)).unwrap(),
865-
_ => self.crate_label(&dep.id.name, &dep.id.version.to_string(), &dep.target),
863+
deps.map(|dep| {
864+
self.crate_label(&dep.id.name, &dep.id.version.to_string(), &dep.target)
866865
}),
867866
extra_deps,
868867
)

examples/crate_universe_local_path/MODULE.bazel

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@ use_repo(
1616
register_toolchains("@rust_toolchains//:all")
1717

1818
crate = use_extension(
19-
"@rules_rust//crate_universe:extension.bzl",
19+
"@rules_rust//crate_universe:extensions.bzl",
2020
"crate",
2121
)
2222
crate.from_cargo(
2323
name = "crates_from_cargo_workspace",
24-
cargo_lockfile = "//crates_from_workspace:Cargo.lock",
24+
cargo_lockfile = "//crates_from_workspace:Cargo.workspace.lock",
2525
manifests = ["//crates_from_workspace:Cargo.toml"],
2626
)
2727
use_repo(crate, "crates_from_cargo_workspace")
28+
29+
crate.spec(
30+
package = "lazy_static",
31+
repositories = ["crates_from_packages"],
32+
# Replaced by `vendor_lazy_static.sh`
33+
version = "1.5.0", # lazy_static
34+
)
35+
crate.from_specs(
36+
name = "crates_from_packages",
37+
cargo_lockfile = "//crates_from_workspace:Cargo.packages.lock",
38+
)
39+
use_repo(crate, "crates_from_packages")

examples/crate_universe_local_path/WORKSPACE.bazel

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,31 @@ load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencie
1515

1616
crate_universe_dependencies(bootstrap = True)
1717

18-
load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
18+
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")
1919

2020
crates_repository(
2121
name = "crates_from_cargo_workspace",
22-
cargo_lockfile = "//crates_from_workspace:Cargo.lock",
22+
cargo_lockfile = "//crates_from_workspace:Cargo.workspace.lock",
2323
# `generator` is not necessary in official releases.
2424
# See load satement for `cargo_bazel_bootstrap`.
2525
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
2626
manifests = ["//crates_from_workspace:Cargo.toml"],
2727
)
2828

29-
load("@crates_from_cargo_workspace//:defs.bzl", "crate_repositories")
29+
load("@crates_from_cargo_workspace//:defs.bzl", crate_repositories_from_cargo_workspace = "crate_repositories")
3030

31-
crate_repositories()
31+
crate_repositories_from_cargo_workspace()
32+
33+
crates_repository(
34+
name = "crates_from_packages",
35+
cargo_lockfile = "//crates_from_workspace:Cargo.packages.lock",
36+
# `generator` is not necessary in official releases.
37+
# See load satement for `cargo_bazel_bootstrap`.
38+
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
39+
# Replaced by `vendor_lazy_static.sh`
40+
packages = {"lazy_static": crate.spec(version = "1.5.0")},
41+
)
42+
43+
load("@crates_from_packages//:defs.bzl", crate_repositories_from_packages = "crate_repositories")
44+
45+
crate_repositories_from_packages()
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
load("@crates_from_cargo_workspace//:defs.bzl", "all_crate_deps")
1+
load("@crates_from_cargo_workspace//:defs.bzl", all_crate_deps_from_cargo_workspace = "all_crate_deps")
22
load("@rules_rust//rust:defs.bzl", "rust_test")
33

44
rust_test(
5-
name = "test",
5+
name = "test_from_cargo_workspace",
66
srcs = ["test.rs"],
7-
deps = all_crate_deps(),
7+
deps = all_crate_deps_from_cargo_workspace(),
8+
)
9+
10+
rust_test(
11+
name = "test_from_packages",
12+
srcs = ["test.rs"],
13+
deps = ["@crates_from_packages//:lazy_static"],
814
)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 4
4+
5+
[[package]]
6+
name = "direct-cargo-bazel-deps"
7+
version = "0.0.1"
8+
dependencies = [
9+
"lazy_static",
10+
]
11+
12+
[[package]]
13+
name = "lazy_static"
14+
version = "1.5.0"
15+
source = "registry+https://github.com/rust-lang/crates.io-index"
16+
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"

examples/crate_universe_local_path/vendor_lazy_static.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ cp -r "lazy_static_1.5.0_copy/"* "${copy_to}/"
2929
echo "pub const VENDORED_BY: &'static str = \"rules_rust\";" >> "${copy_to}/src/lib.rs"
3030

3131
cargo_toml_to_update="crates_from_workspace/Cargo.toml"
32+
workspace_bazel_to_update="WORKSPACE.bazel"
33+
module_bazel_to_update="MODULE.bazel"
3234
echo "lazy_static = { path = \"${path_dep_path}\" }" >> "${cargo_toml_to_update}"
35+
sed -i -e "s|\"lazy_static\": crate.spec(version = \"1.5.0\")|\"lazy_static\": crate.spec(path = \"${path_dep_path}\")|" "${workspace_bazel_to_update}"
36+
sed -i -e "s|version = \"1.5.0\", # lazy_static|path = \"${path_dep_path}\", # lazy_static|" "${module_bazel_to_update}"
3337

34-
echo "Copied to ${copy_to} and updated ${cargo_toml_to_update}"
38+
echo "Copied to ${copy_to}, updated ${cargo_toml_to_update}, updated ${workspace_bazel_to_update}, and updated ${module_bazel_to_update}"

examples/nix_cross_compiling/nix/flake.lock

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

0 commit comments

Comments
 (0)