Skip to content

Commit

Permalink
Merge pull request #8 from illicitonion/rules_rust-lockfile
Browse files Browse the repository at this point in the history
Use public lockfile interface from rules_rust
  • Loading branch information
Calsign authored Mar 4, 2024
2 parents 36ecd00 + 571091a commit b2abc40
Show file tree
Hide file tree
Showing 12 changed files with 18 additions and 78 deletions.
1 change: 0 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ gazelle_dependencies()

http_archive(
name = "rules_rust",
patches = ["//patches:rules_rust.patch"],
sha256 = versions.RULES_RUST_SHA256,
urls = [
"https://github.com/bazelbuild/rules_rust/releases/download/{0}/rules_rust-v{0}.tar.gz".format(versions.RULES_RUST_VERSION),
Expand Down
5 changes: 2 additions & 3 deletions bootstrap_from_cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
GAZELLE_RUST_COMMIT = "04e5450054ba5c89013022ad14c50b68c05214fd"
GAZELLE_RUST_SHA256 = "41b9261187aeb6a6e0d097ebbcd5e10cf89c439d950b9398d5bdc10abf614ab5"

RULES_RUST_VERSION = "0.28.0"
RULES_RUST_SHA256 = "c46bdafc582d9bd48a6f97000d05af4829f62d5fee10a2a3edddf2f3d9a232c1"
RULES_RUST_VERSION = "0.40.0"
RULES_RUST_SHA256 = "c30dfdf1e86fd50650a76ea645b3a45f2f00667b06187a685e9554e167ca97ee"

RUST_VERSION = "1.73.0"

Expand Down Expand Up @@ -71,7 +71,6 @@ def write_workspace(args: argparse.Namespace) -> None:
http_archive(
name = "rules_rust",
# This patch is currently necessary for gazelle_rust to parse crate_universe lockfiles.
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = "{sha256}",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/{version}/rules_rust-v{version}.tar.gz"],
)
Expand Down
4 changes: 2 additions & 2 deletions deps_versions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ versions = struct(
GO_VERSION = "1.21.0",

# rules_rust
RULES_RUST_VERSION = "0.38.0",
RULES_RUST_SHA256 = "6501960c3e4da32495d1e1007ded0769a534cb195c30dea36aa54f9d8a3f0361",
RULES_RUST_VERSION = "0.40.0",
RULES_RUST_SHA256 = "c30dfdf1e86fd50650a76ea645b3a45f2f00667b06187a685e9554e167ca97ee",

# rust
RUST_VERSION = "1.75.0",
Expand Down
6 changes: 2 additions & 4 deletions example/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ local_repository(

http_archive(
name = "rules_rust",
# NOTE: This patch is currently necessary for gazelle_rust to parse crate_universe lockfiles.
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = "6501960c3e4da32495d1e1007ded0769a534cb195c30dea36aa54f9d8a3f0361",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.38.0/rules_rust-v0.38.0.tar.gz"],
sha256 = "c30dfdf1e86fd50650a76ea645b3a45f2f00667b06187a685e9554e167ca97ee",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.40.0/rules_rust-v0.40.0.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@gazelle_rust//:deps_versions.bzl", "versions")

http_archive(
name = "rules_rust",
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = versions.RULES_RUST_SHA256,
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/{0}/rules_rust-v{0}.tar.gz".format(versions.RULES_RUST_VERSION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@gazelle_rust//:deps_versions.bzl", "versions")

http_archive(
name = "rules_rust",
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = versions.RULES_RUST_SHA256,
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/{0}/rules_rust-v{0}.tar.gz".format(versions.RULES_RUST_VERSION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@gazelle_rust//:deps_versions.bzl", "versions")

http_archive(
name = "rules_rust",
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = versions.RULES_RUST_SHA256,
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/{0}/rules_rust-v{0}.tar.gz".format(versions.RULES_RUST_VERSION),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@gazelle_rust//:deps_versions.bzl", "versions")

http_archive(
name = "rules_rust",
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = versions.RULES_RUST_SHA256,
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/{0}/rules_rust-v{0}.tar.gz".format(versions.RULES_RUST_VERSION),
Expand Down
1 change: 0 additions & 1 deletion generation_tests/crate_universe/vendored/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ load("@gazelle_rust//:deps_versions.bzl", "versions")

http_archive(
name = "rules_rust",
patches = ["@gazelle_rust//patches:rules_rust.patch"],
sha256 = versions.RULES_RUST_SHA256,
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_rust/releases/download/{0}/rules_rust-v{0}.tar.gz".format(versions.RULES_RUST_VERSION),
Expand Down
20 changes: 0 additions & 20 deletions patches/rules_rust.patch

This file was deleted.

20 changes: 0 additions & 20 deletions patches/rules_rust_p1.patch

This file was deleted.

35 changes: 12 additions & 23 deletions rust_parser/lockfile_crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,13 @@ use std::collections::HashMap;
use std::error::Error;
use std::path::PathBuf;

use cargo_bazel::api::lockfile::CargoBazelLockfile;
use messages_rust_proto::Package;

pub fn get_bazel_lockfile_crates(lockfile_path: PathBuf) -> Result<Vec<Package>, Box<dyn Error>> {
let lockfile_str = match std::fs::read_to_string(&lockfile_path) {
Err(err) => {
eprintln!(
"Could not open lockfile {}: {}",
&lockfile_path.to_str().unwrap_or("<utf-8 decode error>"),
err,
);
std::process::exit(1);
}
read_str => read_str?,
};
// Surprisingly, using serde_json::from_str is much faster than using serde_json::from_reader.
// See: https://github.com/serde-rs/json/issues/160
let context: cargo_bazel::context::Context = match serde_json::from_str(&lockfile_str) {
let context = match cargo_bazel::api::lockfile::parse(&lockfile_path) {
Err(err) => {
eprintln!(
"Could not parse lockfile {}: {}",
Expand All @@ -35,37 +25,36 @@ pub fn get_bazel_lockfile_crates(lockfile_path: PathBuf) -> Result<Vec<Package>,
let mut crates = Vec::new();

let mut add_crate = |id: &_, is_proc_macro| {
let crate_ = context.crates.get(id).expect("missing crate");
let crate_ = context.crate_info(id).expect("missing crate");

if let Some(library_target_name) = &crate_.library_target_name {
if let Some(library_target_name) = &crate_.library_target_name() {
let mut package = Package::default();
package.set_name(crate_.name.clone());
package.set_name(crate_.name().to_string());
package.set_crate_name(library_target_name.to_string());
package.set_proc_macro(is_proc_macro);

crates.push(package);
}
};

for workspace_member in context.workspace_members.keys() {
for workspace_member in context.workspace_members() {
let workspace_crate = context
.crates
.get(workspace_member)
.crate_info(&workspace_member)
.expect("missing workspace member");

for dep in workspace_crate.common_attrs.deps.values() {
for dep in workspace_crate.normal_deps().values() {
add_crate(&dep.id, false);
}

for dep in workspace_crate.common_attrs.deps_dev.values() {
for dep in workspace_crate.dev_deps().values() {
add_crate(&dep.id, false);
}

for proc_macro_dep in workspace_crate.common_attrs.proc_macro_deps.values() {
for proc_macro_dep in workspace_crate.proc_macro_deps().values() {
add_crate(&proc_macro_dep.id, true);
}

for proc_macro_dep in workspace_crate.common_attrs.proc_macro_deps_dev.values() {
for proc_macro_dep in workspace_crate.proc_macro_dev_deps().values() {
add_crate(&proc_macro_dep.id, true);
}
}
Expand Down Expand Up @@ -117,7 +106,7 @@ pub fn get_cargo_lockfile_crates(lockfile_path: PathBuf) -> Result<Vec<Package>,
for dep in deps {
let mut package = Package::default();
package.name = dep.name.as_str().to_string();
package.crate_name = cargo_bazel::utils::sanitize_module_name(&package.name);
package.crate_name = package.name.replace('-', "_");
package.proc_macro = is_proc_macro[dep.name.as_str()];

crates.push(package);
Expand Down

0 comments on commit b2abc40

Please sign in to comment.