diff options
34 files changed, 119 insertions, 75 deletions
diff --git a/MODULE.bazel b/MODULE.bazel index f50cb4d1..89307da8 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -2,7 +2,7 @@ module( name = "rules_rust", - version = "0.44.0", + version = "0.45.1", ) bazel_dep( diff --git a/crate_universe/Cargo.lock b/crate_universe/Cargo.lock index 5297acf7..d980f4ef 100644 --- a/crate_universe/Cargo.lock +++ b/crate_universe/Cargo.lock @@ -169,7 +169,7 @@ dependencies = [ [[package]] name = "cargo-bazel" -version = "0.13.0" +version = "0.14.0" dependencies = [ "anyhow", "camino", diff --git a/crate_universe/Cargo.toml b/crate_universe/Cargo.toml index 66005370..ee9e736e 100644 --- a/crate_universe/Cargo.toml +++ b/crate_universe/Cargo.toml @@ -4,7 +4,7 @@ exclude = ["test_data"] [package] name = "cargo-bazel" -version = "0.13.0" +version = "0.14.0" authors = ["Andre Brisco - andre.brisco@protonmail.com"] categories = ["development-tools"] description = "A collection of tools which use Cargo to generate build targets for Bazel" diff --git a/crate_universe/private/common_utils.bzl b/crate_universe/private/common_utils.bzl index e8fc97be..923c0e0e 100644 --- a/crate_universe/private/common_utils.bzl +++ b/crate_universe/private/common_utils.bzl @@ -28,13 +28,14 @@ STDERR ------------------------------------------------------------------------ {stderr} """ -def execute(repository_ctx, args, env = {}): +def execute(repository_ctx, args, env = {}, allow_fail = False): """A heler macro for executing some arguments and displaying nicely formatted errors Args: repository_ctx (repository_ctx): The rule's context object. args (list): A list of strings which act as `argv` for execution. env (dict, optional): Environment variables to set in the execution environment. + allow_fail (bool, optional): Allow the process to fail. Returns: struct: The results of `repository_ctx.execute` @@ -50,7 +51,7 @@ def execute(repository_ctx, args, env = {}): quiet = quiet, ) - if result.return_code: + if result.return_code and not allow_fail: fail(_EXECUTE_ERROR_MESSAGE.format( args = args, exit_code = result.return_code, diff --git a/crate_universe/private/generate_utils.bzl b/crate_universe/private/generate_utils.bzl index 67784a62..a6c88dd8 100644 --- a/crate_universe/private/generate_utils.bzl +++ b/crate_universe/private/generate_utils.bzl @@ -396,12 +396,13 @@ def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_m repository_ctx = repository_ctx, args = args, env = env, + allow_fail = True, ) # If it was determined repinning should occur but there was no # flag indicating repinning was requested, an error is raised # since repinning should be an explicit action - if result.stdout.strip().lower() == "repin": + if result.return_code: fail(("\n".join([ result.stderr, ( diff --git a/crate_universe/src/cli/query.rs b/crate_universe/src/cli/query.rs index eefd144a..1d9553c4 100644 --- a/crate_universe/src/cli/query.rs +++ b/crate_universe/src/cli/query.rs @@ -3,7 +3,7 @@ use std::fs; use std::path::PathBuf; -use anyhow::Result; +use anyhow::{bail, Result}; use clap::Parser; use crate::config::Config; @@ -42,19 +42,19 @@ pub fn query(opt: QueryOptions) -> Result<()> { // Read the lockfile let content = match fs::read_to_string(&opt.lockfile) { Ok(c) => c, - Err(_) => return announce_repin("Unable to read lockfile"), + Err(_) => bail!("Unable to read lockfile"), }; // Deserialize it so we can easily compare it with let lockfile: Context = match serde_json::from_str(&content) { Ok(ctx) => ctx, - Err(_) => return announce_repin("Could not load lockfile"), + Err(_) => bail!("Could not load lockfile"), }; // Check to see if a digest has been set let digest = match &lockfile.checksum { Some(d) => d.clone(), - None => return announce_repin("No digest provided in lockfile"), + None => bail!("No digest provided in lockfile"), }; // Load the config file @@ -70,16 +70,11 @@ pub fn query(opt: QueryOptions) -> Result<()> { &Cargo::new(opt.cargo), &opt.rustc, )?; + if digest != expected { - return announce_repin(&format!("Digests do not match: {digest:?} != {expected:?}")); + bail!("Digests do not match: Current {digest:?} != Expected {expected:?}"); } // There is no need to repin Ok(()) } - -fn announce_repin(reason: &str) -> Result<()> { - eprintln!("{reason}"); - println!("repin"); - Ok(()) -} diff --git a/crate_universe/src/lockfile.rs b/crate_universe/src/lockfile.rs index 54b3fa93..d74e7b0a 100644 --- a/crate_universe/src/lockfile.rs +++ b/crate_universe/src/lockfile.rs @@ -93,6 +93,16 @@ impl Digest { }) } + /// A helper for generating a hash and logging it's contents. + fn compute_single_hash(data: &str, id: &str) -> String { + let mut hasher = Sha256::new(); + hasher.update(data.as_bytes()); + hasher.update(b"\0"); + let hash = hasher.finalize().encode_hex::<String>(); + tracing::debug!("{} hash: {}", id, hash); + hash + } + fn compute( context: &Context, config: &Config, @@ -108,25 +118,44 @@ impl Digest { let mut hasher = Sha256::new(); - hasher.update(cargo_bazel_version.as_bytes()); + hasher.update(Digest::compute_single_hash( + cargo_bazel_version, + "cargo-bazel version", + )); hasher.update(b"\0"); - hasher.update(serde_json::to_string(context).unwrap().as_bytes()); + // The lockfile context (typically `cargo-bazel-lock.json`). + hasher.update(Digest::compute_single_hash( + &serde_json::to_string(context).unwrap(), + "lockfile context", + )); hasher.update(b"\0"); - hasher.update(serde_json::to_string(config).unwrap().as_bytes()); + // This content is generated by various attributes in Bazel rules and written to a file behind the scenes. + hasher.update(Digest::compute_single_hash( + &serde_json::to_string(config).unwrap(), + "workspace config", + )); hasher.update(b"\0"); - hasher.update(serde_json::to_string(splicing_metadata).unwrap().as_bytes()); + // Data collected about Cargo manifests and configs that feed into dependency generation. This file + // is also generated by Bazel behind the scenes based on user inputs. + hasher.update(Digest::compute_single_hash( + &serde_json::to_string(splicing_metadata).unwrap(), + "splicing manifest", + )); hasher.update(b"\0"); - hasher.update(cargo_version.as_bytes()); + hasher.update(Digest::compute_single_hash(cargo_version, "Cargo version")); hasher.update(b"\0"); - hasher.update(rustc_version.as_bytes()); + hasher.update(Digest::compute_single_hash(rustc_version, "Rustc version")); hasher.update(b"\0"); - Self(hasher.finalize().encode_hex::<String>()) + let hash = hasher.finalize().encode_hex::<String>(); + tracing::debug!("Digest hash: {}", hash); + + Self(hash) } pub(crate) fn bin_version(binary: &Path) -> Result<String> { @@ -212,7 +241,7 @@ mod test { ); assert_eq!( - Digest("83ad667352ca5a7cb3cc60f171a65f3bf264c7c97c6d91113d4798ca1dfb8d48".to_owned()), + Digest("7f8d38b770a838797e24635a9030d4194210ff331f1a5b59c753f23fd197b5d8".to_owned()), digest, ); } @@ -257,7 +286,7 @@ mod test { ); assert_eq!( - Digest("40a5ede6a47639166062fffab74e5dbe229b1d2508bcf70d8dfeba04b4f4ac9a".to_owned()), + Digest("4148d6b336e574a67417f77fb4727e9c1b014a0b5ab90771f57285f96bca0fee".to_owned()), digest, ); } @@ -288,7 +317,7 @@ mod test { ); assert_eq!( - Digest("0d217eec46c3d5a00b142db59994eaa226b630418f70e6b54e8ecc66f7d549da".to_owned()), + Digest("e81dba9d36276baa8d491373fe09ef38e71e68c12f70e45b7c260ba2c48a87f5".to_owned()), digest, ); } @@ -337,7 +366,7 @@ mod test { ); assert_eq!( - Digest("9e3d58a48b375bec2cf8c783d92f5d8db67306046e652ee376f30c362c882f56".to_owned()), + Digest("f1b8ca07d35905bbd8bba79137ca7a02414b4ef01f28c459b78d1807ac3a8191".to_owned()), digest, ); } diff --git a/crate_universe/version.bzl b/crate_universe/version.bzl index ab9dca48..b03ead01 100644 --- a/crate_universe/version.bzl +++ b/crate_universe/version.bzl @@ -1,3 +1,3 @@ """ Version info for the `cargo-bazel` repository """ -VERSION = "0.13.0" +VERSION = "0.14.0" diff --git a/docs/flatten.md b/docs/flatten.md index 3d3210a4..5a27be78 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1963,7 +1963,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | <a id="rust_register_toolchains-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when global allocator is used with cc_common.link. | `None` | | <a id="rust_register_toolchains-iso_date"></a>iso_date | **Deprecated**: Use <code>versions</code> instead. | `None` | | <a id="rust_register_toolchains-register_toolchains"></a>register_toolchains | If true, repositories will be generated to produce and register <code>rust_toolchain</code> targets. | `True` | -| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. | `"nightly/2024-05-02"` | +| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. If none is supplied and only a single version in <code>versions</code> is given, then this defaults to that version, otherwise will default to the default nightly version. | `None` | | <a id="rust_register_toolchains-rust_analyzer_version"></a>rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` | | <a id="rust_register_toolchains-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` | | <a id="rust_register_toolchains-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasi"]` | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index 252d4b24..bc59cdb1 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -275,7 +275,7 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | <a id="rust_register_toolchains-global_allocator_library"></a>global_allocator_library | Target that provides allocator functions when global allocator is used with cc_common.link. | `None` | | <a id="rust_register_toolchains-iso_date"></a>iso_date | **Deprecated**: Use <code>versions</code> instead. | `None` | | <a id="rust_register_toolchains-register_toolchains"></a>register_toolchains | If true, repositories will be generated to produce and register <code>rust_toolchain</code> targets. | `True` | -| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. | `"nightly/2024-05-02"` | +| <a id="rust_register_toolchains-rustfmt_version"></a>rustfmt_version | The version of rustfmt. If none is supplied and only a single version in <code>versions</code> is given, then this defaults to that version, otherwise will default to the default nightly version. | `None` | | <a id="rust_register_toolchains-rust_analyzer_version"></a>rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` | | <a id="rust_register_toolchains-sha256s"></a>sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` | | <a id="rust_register_toolchains-extra_target_triples"></a>extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasi"]` | diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_none.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_none.json index f31d6ffe..e8f86525 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_none.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_none.json @@ -1,5 +1,5 @@ { - "checksum": "8eebb15991d786d92c33fada8f4d1ffa4066e458853e3e9057788d5dfc923296", + "checksum": "81f62de6f649e0f4b4ba6bef3b10e732e395b45134f33b001ae0d5f8109e7494", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_opt.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_opt.json index a4914624..451128ea 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_opt.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_alias_annotation_opt.json @@ -1,5 +1,5 @@ { - "checksum": "ef0996d8d24c870ad4d6781696663b4ce32158b5a11f5c5e6f2040f647408cbf", + "checksum": "ed78b5a6ffbed80c354bfe097ab85aaef09b242fd0759fed09db2ffdbd6d3768", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_custom_annotation_none.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_custom_annotation_none.json index 5296576d..9d837536 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_custom_annotation_none.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_custom_annotation_none.json @@ -1,5 +1,5 @@ { - "checksum": "dd50f7aa27d4f281b8f283beb8b44a332ad8c11731f68250159e38622d89e6c3", + "checksum": "e358b727b6eaba65cda94827b0980ee467846ac1b0201c049bc92b7d5d19cdba", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_dbg_annotation_fastbuild.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_dbg_annotation_fastbuild.json index b994d723..eac0a49e 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_dbg_annotation_fastbuild.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_dbg_annotation_fastbuild.json @@ -1,5 +1,5 @@ { - "checksum": "69b55cc95efd1af3026173f4d04a6de7862fcf666203f853cd2042615c7d6192", + "checksum": "c8be56b12c4f1e37f413fd2e975f9d35f34d8465f2b1af1f4a661bd777c0aed5", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_alias.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_alias.json index 4efa5353..2a373019 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_alias.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_alias.json @@ -1,5 +1,5 @@ { - "checksum": "cedf00d05772baf88268e3a53147b38b4a0060135b4893a5bab134f08176265a", + "checksum": "dbfb871acaddb8d60e157a70b9b16dbf8ad1e78dd12536461ff4c94340fbdbc7", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_dbg.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_dbg.json index 2c1d7e55..b443794d 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_dbg.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_dbg.json @@ -1,5 +1,5 @@ { - "checksum": "34723df8f98be06e6c8443df29c797c67c3bf65c204842b8ebd5a813963842a2", + "checksum": "c111189559c951979977616f3e3d76f0bacc782b9d8f5dce1a0b7290eb2756ee", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_none.json b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_none.json index 7cb79a08..7dd4285d 100644 --- a/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_none.json +++ b/examples/crate_universe/alias_rule/cargo-bazel-lock_global_opt_annotation_none.json @@ -1,5 +1,5 @@ { - "checksum": "689e7c097350dd53a519f2b2510bd0a5e1a87381beb4c245b82e40f1b4cd4600", + "checksum": "b2145b6622f242823810990c65a4624f4249f08fd8d9816c036fd85b12be9449", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json b/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json index 1eba0ba7..e3f3a33b 100644 --- a/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "aff5d296232e27a2be31e0c924d2d6b270f90a1ed3ac991a15a1f635d7fff553", + "checksum": "ba1794b9d1cb7443d3cce9d584cb55c78bc251656241e26afd33d06373b6b7a5", "crates": { "aho-corasick 0.7.20": { "name": "aho-corasick", diff --git a/examples/crate_universe/cargo_conditional_deps/cargo-bazel-lock.json b/examples/crate_universe/cargo_conditional_deps/cargo-bazel-lock.json index 2a5decbc..05f24e92 100644 --- a/examples/crate_universe/cargo_conditional_deps/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_conditional_deps/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "945a24f2caf67b72776c1e1184f5225ed3730c8e1b0ea26dc14405e9e818d7e8", + "checksum": "e167d4698c9d2882d7d61acb7560b0f39896355a87f945fe2906ed44585d8b5b", "crates": { "autocfg 1.1.0": { "name": "autocfg", diff --git a/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json b/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json index 73f76cc8..1035cd88 100644 --- a/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "31d1aed9525adf02f67e40f8a5ef415d0d0d3792dbf8d0eda364db304c721ed1", + "checksum": "216a686dd8be7aa09756d62e5f52a3d126db37172c1615b193d0626cdf3a4cbe", "crates": { "ansi_term 0.12.1": { "name": "ansi_term", diff --git a/examples/crate_universe/complicated_dependencies/cargo-bazel-lock.json b/examples/crate_universe/complicated_dependencies/cargo-bazel-lock.json index 7ee4a5b3..1710bc4d 100644 --- a/examples/crate_universe/complicated_dependencies/cargo-bazel-lock.json +++ b/examples/crate_universe/complicated_dependencies/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "07cc02dd40387f184603870b77daf194a9a3a67ad92c7114acd02b2c1059c364", + "checksum": "92aff3e04bbf34fd9df494ac3ac22c16ec5b9a73da86ee05ad5b654c639e54e1", "crates": { "aho-corasick 1.1.3": { "name": "aho-corasick", diff --git a/examples/crate_universe/multi_package/cargo-bazel-lock.json b/examples/crate_universe/multi_package/cargo-bazel-lock.json index 94156ec1..33fb7035 100644 --- a/examples/crate_universe/multi_package/cargo-bazel-lock.json +++ b/examples/crate_universe/multi_package/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "c93d2a4f30146ca29998de2f9d8c5671cf5dc35b5f561cef3acb2f441c77d217", + "checksum": "276333e236206b7130417df1123515a696ce456e9e398b94b7c5454da20ef91e", "crates": { "aho-corasick 0.7.20": { "name": "aho-corasick", diff --git a/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json b/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json index 7c6089d4..47741888 100644 --- a/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json +++ b/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "2b1cbab2b98d3179fa2dcd3922d3f728c9f08b8373d2a9a11bd5e9cfe5242970", + "checksum": "10f8dc5f4aea607a12d2dfca7e9d2dc1e76b56949c2c361121d7508a5b74a1f9", "crates": { "async-trait 0.1.64": { "name": "async-trait", diff --git a/examples/crate_universe/using_cxx/cargo-bazel-lock.json b/examples/crate_universe/using_cxx/cargo-bazel-lock.json index d6faf0e6..754a7779 100644 --- a/examples/crate_universe/using_cxx/cargo-bazel-lock.json +++ b/examples/crate_universe/using_cxx/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "e1307baf970c0e62403e2e85bd179a89b2c018e2a408815fe04819c01d232704", + "checksum": "52742defe5b3e707b48e3291f142a276e6600ed2c4a9cde56dbc191d77e95bde", "crates": { "cc 1.0.82": { "name": "cc", diff --git a/examples/crate_universe/using_cxx/cxxbridge-cmd.Cargo.Bazel.lock b/examples/crate_universe/using_cxx/cxxbridge-cmd.Cargo.Bazel.lock index b47a5999..c13d51fa 100644 --- a/examples/crate_universe/using_cxx/cxxbridge-cmd.Cargo.Bazel.lock +++ b/examples/crate_universe/using_cxx/cxxbridge-cmd.Cargo.Bazel.lock @@ -1,5 +1,5 @@ { - "checksum": "2a6c9b478e404dbd29526939eab51fcf24aa1620547b1727d810550ce762115e", + "checksum": "b42c9db041308f147d6e0c85d03b0b1d336a2d3cb035488a0c46ea9a6b742439", "crates": { "anstyle 1.0.1": { "name": "anstyle", diff --git a/examples/nix_cross_compiling/bazel/cargo/cargo-bazel-lock.json b/examples/nix_cross_compiling/bazel/cargo/cargo-bazel-lock.json index 75845707..fad6d998 100644 --- a/examples/nix_cross_compiling/bazel/cargo/cargo-bazel-lock.json +++ b/examples/nix_cross_compiling/bazel/cargo/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "9c6d454ed6912ab0a772ba6ca8e4c11000584ff5efecb268b741408a27f485e0", + "checksum": "03ca4ea10ba98f48df6ca7d78c292668385403c13e48c2b0ca953b95b177c554", "crates": { "addr2line 0.21.0": { "name": "addr2line", diff --git a/proto/prost/private/prost.bzl b/proto/prost/private/prost.bzl index b7d55c36..a8735dee 100644 --- a/proto/prost/private/prost.bzl +++ b/proto/prost/private/prost.bzl @@ -275,6 +275,7 @@ def _rust_prost_aspect_impl(target, ctx): package_info = package_info_file, ), rust_analyzer_info, + OutputGroupInfo(rust_generated_srcs = [lib_rs]), ] rust_prost_aspect = aspect( diff --git a/proto/protobuf/proto.bzl b/proto/protobuf/proto.bzl index a9509ee0..e3ef70e5 100644 --- a/proto/protobuf/proto.bzl +++ b/proto/protobuf/proto.bzl @@ -209,7 +209,7 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr ), ) - return rustc_compile_action( + providers = rustc_compile_action( ctx = ctx, attr = ctx.attr, toolchain = toolchain, @@ -233,6 +233,8 @@ def _rust_proto_compile(protos, descriptor_sets, imports, crate_name, ctx, is_gr ), output_hash = output_hash, ) + providers.append(OutputGroupInfo(rust_generated_srcs = [lib_rs])) + return providers def _rust_protogrpc_library_impl(ctx, is_grpc): """Implementation of the rust_(proto|grpc)_library. diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 474d1a61..98806f1a 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1391,13 +1391,18 @@ def rustc_compile_action( # The path to the package dir, including a trailing "/". package_dir = ctx.bin_dir.path + "/" - if ctx.label.workspace_root: + + # For external repositories, workspace root is not part of the output + # path when sibling repository layout is used (the repository name is + # part of the bin_dir). This scenario happens when the workspace root + # starts with "../" + if ctx.label.workspace_root and not ctx.label.workspace_root.startswith("../"): package_dir = package_dir + ctx.label.workspace_root + "/" if ctx.label.package: package_dir = package_dir + ctx.label.package + "/" if not crate_info.output.path.startswith(package_dir): - fail("The package dir path {} should be a prefix of the crate_info.output.path {}", package_dir, crate_info.output.path) + fail("The package dir path", package_dir, "should be a prefix of the crate_info.output.path", crate_info.output.path) output_relative_to_package = crate_info.output.path[len(package_dir):] diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 59c8a8c8..9552e816 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -112,7 +112,7 @@ def rust_register_toolchains( global_allocator_library = None, iso_date = None, register_toolchains = True, - rustfmt_version = DEFAULT_NIGHTLY_VERSION, + rustfmt_version = None, rust_analyzer_version = None, sha256s = None, extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES, @@ -146,7 +146,7 @@ def rust_register_toolchains( global_allocator_library (str, optional): Target that provides allocator functions when global allocator is used with cc_common.link. iso_date (str, optional): **Deprecated**: Use `versions` instead. register_toolchains (bool): If true, repositories will be generated to produce and register `rust_toolchain` targets. - rustfmt_version (str, optional): The version of rustfmt. + rustfmt_version (str, optional): The version of rustfmt. If none is supplied and only a single version in `versions` is given, then this defaults to that version, otherwise will default to the default nightly version. rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer. sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support. @@ -176,6 +176,12 @@ def rust_register_toolchains( else: versions = _RUST_TOOLCHAIN_VERSIONS + if not rustfmt_version: + if len(versions) == 1: + rustfmt_version = versions[0] + else: + rustfmt_version = DEFAULT_NIGHTLY_VERSION + if dev_components: has_nightly = False for ver in versions: diff --git a/test/no_std/cargo-bazel-lock.json b/test/no_std/cargo-bazel-lock.json index 486847d9..d6b142fc 100644 --- a/test/no_std/cargo-bazel-lock.json +++ b/test/no_std/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "4cdd2a75d814f49c49d7c02588accb2bc780d3cb1c0f718a83d7edffe4bbebe0", + "checksum": "81997a51e91981729d56b9118839163206d491451a10bf9611c6037d1ae2f665", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", diff --git a/tools/runfiles/runfiles.rs b/tools/runfiles/runfiles.rs index a485e2b4..0df98a22 100644 --- a/tools/runfiles/runfiles.rs +++ b/tools/runfiles/runfiles.rs @@ -44,7 +44,7 @@ const TEST_SRCDIR_ENV_VAR: &str = "TEST_SRCDIR"; #[macro_export] macro_rules! rlocation { - ($r:ident, $path:expr) => { + ($r:expr, $path:expr) => { $r.rlocation_from($path, env!("REPOSITORY_NAME")) }; } @@ -66,7 +66,7 @@ pub struct Runfiles { impl Runfiles { /// Creates a manifest based Runfiles object when - /// RUNFILES_MANIFEST_ONLY environment variable is present, + /// RUNFILES_MANIFEST_FILE environment variable is present, /// or a directory based Runfiles object otherwise. pub fn create() -> io::Result<Self> { let mode = if let Some(manifest_file) = std::env::var_os(MANIFEST_FILE_ENV_VAR) { @@ -125,21 +125,22 @@ impl Runfiles { return path.to_path_buf(); } - let parts: Vec<&str> = path - .to_str() - .expect("Should be valid UTF8") - .splitn(2, '/') - .collect(); - if parts.len() == 2 { - let key: (String, String) = (source_repo.into(), parts[0].into()); - if let Some(target_repo_directory) = self.repo_mapping.get(&key) { - return raw_rlocation( - &self.mode, - target_repo_directory.to_owned() + "/" + parts[1], - ); - }; + let path_str = path.to_str().expect("Should be valid UTF8"); + let (repo_alias, repo_path): (&str, Option<&str>) = match path_str.split_once('/') { + Some((name, alias)) => (name, Some(alias)), + None => (path_str, None), + }; + let key: (String, String) = (source_repo.into(), repo_alias.into()); + if let Some(target_repo_directory) = self.repo_mapping.get(&key) { + match repo_path { + Some(repo_path) => { + raw_rlocation(&self.mode, format!("{target_repo_directory}/{repo_path}")) + } + None => raw_rlocation(&self.mode, target_repo_directory), + } + } else { + raw_rlocation(&self.mode, path) } - raw_rlocation(&self.mode, path) } } @@ -259,8 +260,11 @@ mod test { env::remove_var(MANIFEST_FILE_ENV_VAR); let r = Runfiles::create().unwrap(); - let mut f = - File::open(r.rlocation("rules_rust/tools/runfiles/data/sample.txt")).unwrap(); + let d = rlocation!(r, "rules_rust"); + let f = rlocation!(r, "rules_rust/tools/runfiles/data/sample.txt"); + assert_eq!(d.join("tools/runfiles/data/sample.txt"), f); + + let mut f = File::open(f).unwrap(); let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); @@ -276,7 +280,7 @@ mod test { let r = Runfiles::create().unwrap(); let mut f = - File::open(r.rlocation("rules_rust/tools/runfiles/data/sample.txt")).unwrap(); + File::open(rlocation!(r, "rules_rust/tools/runfiles/data/sample.txt")).unwrap(); let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); @@ -295,7 +299,7 @@ mod test { let r = Runfiles::create().unwrap(); let mut f = - File::open(r.rlocation("rules_rust/tools/runfiles/data/sample.txt")).unwrap(); + File::open(rlocation!(r, "rules_rust/tools/runfiles/data/sample.txt")).unwrap(); let mut buffer = String::new(); f.read_to_string(&mut buffer).unwrap(); diff --git a/tools/rust_analyzer/lib.rs b/tools/rust_analyzer/lib.rs index 0ab395ad..6bca60b6 100644 --- a/tools/rust_analyzer/lib.rs +++ b/tools/rust_analyzer/lib.rs @@ -29,7 +29,7 @@ pub fn generate_crate_info( "--aspects={}//rust:defs.bzl%rust_analyzer_aspect", rules_rust.as_ref() )) - .arg("--output_groups=rust_analyzer_crate_spec") + .arg("--output_groups=rust_analyzer_crate_spec,rust_generated_srcs") .args(targets) .output()?; diff --git a/version.bzl b/version.bzl index e0fc50f2..a04955ed 100644 --- a/version.bzl +++ b/version.bzl @@ -1,3 +1,3 @@ """The version of rules_rust.""" -VERSION = "0.44.0" +VERSION = "0.45.1" |