Skip to content

Crate Universe: crate.spec() supports path #3464

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions crate_universe/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,7 @@ _spec = tag_class(
doc = "A list of features to use for the crate.",
),
"git": attr.string(
doc = "The Git url to use for the crate. Cannot be used with `version`.",
doc = "The Git url to use for the crate. Cannot be used with `version` or `path`.",
),
"lib": attr.bool(
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.",
Expand All @@ -1238,6 +1238,9 @@ _spec = tag_class(
doc = "The explicit name of the package.",
mandatory = True,
),
"path": attr.string(
doc = "The local path of the remote crate. Cannot be used with `version` or `git`.",
),
"repositories": attr.string_list(
doc = "A list of repository names specified from `crate.from_cargo(name=...)` that this spec is applied to. Defaults to all repositories.",
default = [],
Expand All @@ -1249,7 +1252,7 @@ _spec = tag_class(
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.",
),
"version": attr.string(
doc = "The exact version of the crate. Cannot be used with `git`.",
doc = "The exact version of the crate. Cannot be used with `git` or `path`.",
),
},
)
Expand Down
9 changes: 6 additions & 3 deletions crate_universe/private/crate.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ def _spec(
git = None,
branch = None,
tag = None,
rev = None):
rev = None,
path = None):
"""A constructor for a crate dependency.

See [specifying dependencies][sd] in the Cargo book for more details.
Expand All @@ -36,15 +37,16 @@ def _spec(

Args:
package (str, optional): The explicit name of the package (used when attempting to alias a crate).
version (str, optional): The exact version of the crate. Cannot be used with `git`.
version (str, optional): The exact version of the crate. Cannot be used with `git` or `path`.
artifact (str, optional): Set to "bin" to pull in a binary crate as an artifact dependency. Requires a nightly Cargo.
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.
default_features (bool, optional): Maps to the `default-features` flag.
features (list, optional): A list of features to use for the crate
git (str, optional): The Git url to use for the crate. Cannot be used with `version`.
git (str, optional): The Git url to use for the crate. Cannot be used with `version` or `path`.
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.
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.
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.
path (str, optional): The local path of the remote crate. Cannot be used with `version` or `git`.

Returns:
string: A json encoded string of all inputs
Expand All @@ -59,6 +61,7 @@ def _spec(
"git": git,
"lib": lib,
"package": package,
"path": path,
"rev": rev,
"tag": tag,
"version": version,
Expand Down
2 changes: 0 additions & 2 deletions crate_universe/src/api/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ mod test {
},
target: String::from("anyhow"),
alias: None,
local_path: None,
},
CrateDependency {
id: CrateId {
Expand All @@ -185,7 +184,6 @@ mod test {
},
target: String::from("reqwest"),
alias: None,
local_path: None,
},
],
);
Expand Down
14 changes: 0 additions & 14 deletions crate_universe/src/context/crate_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::collections::{BTreeMap, BTreeSet};

use camino::Utf8PathBuf;
use cargo_metadata::{Node, Package, PackageId};
use serde::{Deserialize, Serialize};

Expand All @@ -26,11 +25,6 @@ pub struct CrateDependency {
/// Some dependencies are assigned aliases. This is tracked here
#[serde(default, skip_serializing_if = "Option::is_none")]
pub alias: Option<String>,

/// Local path of this dependency if provided. This captures local paths from both the
/// `[dependencies]` table and the `[patches]` table so they can be used in rendering.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub(crate) local_path: Option<Utf8PathBuf>,
}

#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -392,10 +386,6 @@ impl CrateContext {
id: CrateId::new(pkg.name.clone(), pkg.version.clone()),
target,
alias: dep.alias,
local_path: match source_annotations.get(&dep.package_id) {
Some(SourceAnnotation::Path { path }) => Some(path.clone()),
_ => None,
},
}
};

Expand Down Expand Up @@ -493,10 +483,6 @@ impl CrateContext {
id: current_crate_id,
target: target.crate_name.clone(),
alias: None,
local_path: match source_annotations.get(&annotation.node.id) {
Some(SourceAnnotation::Path { path }) => Some(path.clone()),
_ => None,
},
},
None,
);
Expand Down
4 changes: 0 additions & 4 deletions crate_universe/src/context/platforms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
local_path: None,
},
None,
);
Expand Down Expand Up @@ -194,7 +193,6 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
local_path: None,
},
Some(configuration),
);
Expand Down Expand Up @@ -281,7 +279,6 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
local_path: None,
},
Some(configuration.clone()),
);
Expand Down Expand Up @@ -348,7 +345,6 @@ mod test {
id: CrateId::new("mock_crate_b".to_owned(), VERSION_ZERO_ONE_ZERO),
target: "mock_crate_b".to_owned(),
alias: None,
local_path: None,
},
Some(configuration.clone()),
);
Expand Down
8 changes: 2 additions & 6 deletions crate_universe/src/rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,8 @@ impl Renderer {
extra_deps: Select<BTreeSet<Label>>,
) -> Select<BTreeSet<Label>> {
Select::merge(
deps.map(|dep| match dep.local_path {
Some(path) => Label::from_str(&format!("//{}:{}", path, &dep.target)).unwrap(),
_ => self.crate_label(&dep.id.name, &dep.id.version.to_string(), &dep.target),
deps.map(|dep| {
self.crate_label(&dep.id.name, &dep.id.version.to_string(), &dep.target)
}),
extra_deps,
)
Expand Down Expand Up @@ -2026,7 +2025,6 @@ mod test {
// this is identical to what we have in the `name` attribute
// which creates conflict in `render_module_build_file`
alias: Some("mock_crate".into()),
local_path: None,
}])),
..Default::default()
},
Expand Down Expand Up @@ -2152,13 +2150,11 @@ mod test {
id: dependency_id.clone(),
target: "my_dependency".into(),
alias: None,
local_path: None,
},
CrateDependency {
id: dependency_id,
target: "my_dependency".into(),
alias: Some("my_dependency_other".into()),
local_path: None,
},
])),
..Default::default()
Expand Down
3 changes: 2 additions & 1 deletion crate_universe/src/splicing/splicer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ mod test {
use super::*;

use std::fs::File;
use std::path::PathBuf;
use std::str::FromStr;

use cargo_metadata::PackageId;
Expand Down Expand Up @@ -670,7 +671,7 @@ mod test {

/// Get cargo and rustc binaries the Bazel way
#[cfg(not(feature = "cargo"))]
fn get_cargo_and_rustc_paths() -> (std::path::PathBuf, std::path::PathBuf) {
fn get_cargo_and_rustc_paths() -> (PathBuf, PathBuf) {
let r = runfiles::Runfiles::create().unwrap();
let cargo_path = runfiles::rlocation!(r, concat!("rules_rust/", env!("CARGO"))).unwrap();
let rustc_path = runfiles::rlocation!(r, concat!("rules_rust/", env!("RUSTC"))).unwrap();
Expand Down
16 changes: 14 additions & 2 deletions examples/crate_universe_local_path/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,24 @@ use_repo(
register_toolchains("@rust_toolchains//:all")

crate = use_extension(
"@rules_rust//crate_universe:extension.bzl",
"@rules_rust//crate_universe:extensions.bzl",
"crate",
)
crate.from_cargo(
name = "crates_from_cargo_workspace",
cargo_lockfile = "//crates_from_workspace:Cargo.lock",
cargo_lockfile = "//crates_from_workspace:Cargo.workspace.lock",
manifests = ["//crates_from_workspace:Cargo.toml"],
)
use_repo(crate, "crates_from_cargo_workspace")

# crate.spec(
# package = "lazy_static",
# repositories = ["crates_from_packages"],
# # Replaced by `vendor_lazy_static.sh`
# version = "1.5.0", # lazy_static
# )
# crate.from_specs(
# name = "crates_from_packages",
# cargo_lockfile = "//crates_from_workspace:Cargo.packages.lock",
# )
# use_repo(crate, "crates_from_packages")
22 changes: 18 additions & 4 deletions examples/crate_universe_local_path/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,31 @@ load("@rules_rust//crate_universe:repositories.bzl", "crate_universe_dependencie

crate_universe_dependencies(bootstrap = True)

load("@rules_rust//crate_universe:defs.bzl", "crates_repository")
load("@rules_rust//crate_universe:defs.bzl", "crate", "crates_repository")

crates_repository(
name = "crates_from_cargo_workspace",
cargo_lockfile = "//crates_from_workspace:Cargo.lock",
cargo_lockfile = "//crates_from_workspace:Cargo.workspace.lock",
# `generator` is not necessary in official releases.
# See load satement for `cargo_bazel_bootstrap`.
generator = "@cargo_bazel_bootstrap//:cargo-bazel",
manifests = ["//crates_from_workspace:Cargo.toml"],
)

load("@crates_from_cargo_workspace//:defs.bzl", "crate_repositories")
load("@crates_from_cargo_workspace//:defs.bzl", crate_repositories_from_cargo_workspace = "crate_repositories")

crate_repositories()
crate_repositories_from_cargo_workspace()

# crates_repository(
# name = "crates_from_packages",
# cargo_lockfile = "//crates_from_workspace:Cargo.packages.lock",
# # `generator` is not necessary in official releases.
# # See load satement for `cargo_bazel_bootstrap`.
# generator = "@cargo_bazel_bootstrap//:cargo-bazel",
# # Replaced by `vendor_lazy_static.sh`
# packages = {"lazy_static": crate.spec(version = "1.5.0")},
# )

# load("@crates_from_packages//:defs.bzl", crate_repositories_from_packages = "crate_repositories")

# crate_repositories_from_packages()
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
load("@crates_from_cargo_workspace//:defs.bzl", "all_crate_deps")
load("@crates_from_cargo_workspace//:defs.bzl", all_crate_deps_from_cargo_workspace = "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_test")

rust_test(
name = "test",
name = "test_from_cargo_workspace",
srcs = ["test.rs"],
deps = all_crate_deps(),
deps = all_crate_deps_from_cargo_workspace(),
)

# rust_test(
# name = "test_from_packages",
# srcs = ["test.rs"],
# deps = ["@crates_from_packages//:lazy_static"],
# )
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4

[[package]]
name = "direct-cargo-bazel-deps"
version = "0.0.1"
dependencies = [
"lazy_static",
]

[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
2 changes: 1 addition & 1 deletion examples/crate_universe_local_path/vendor_edit_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ vendor_target="$1"

echo >&2 "Patching lazy_static dependency"

vendored_lazy_static_dir="$(bazel run "${vendor_target}" | awk '$0 ~ /Copied to/ {print $3}')"
vendored_lazy_static_dir="$(bazel run "${vendor_target}" | awk '$0 ~ /VENDOR_EDIT_TEST:/ {print $2}')"
echo >&2 "Vendored lazy_static to ${vendored_lazy_static_dir}"

echo >&2 "Running test which is expected to pass, now that patch is applied"
Expand Down
8 changes: 7 additions & 1 deletion examples/crate_universe_local_path/vendor_lazy_static.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ cp -r "lazy_static_1.5.0_copy/"* "${copy_to}/"
echo "pub const VENDORED_BY: &'static str = \"rules_rust\";" >> "${copy_to}/src/lib.rs"

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

echo "Copied to ${copy_to} and updated ${cargo_toml_to_update}"
echo "Copied to ${copy_to}, updated ${cargo_toml_to_update}, updated ${workspace_bazel_to_update}, and updated ${module_bazel_to_update}"

echo "VENDOR_EDIT_TEST: ${copy_to}"
Loading
Loading