From 301e5a42441cf4b15b7d96d7af0b9a51a32b7ab8 Mon Sep 17 00:00:00 2001 From: Gio Gutierrez Date: Fri, 6 Dec 2024 13:11:00 -0500 Subject: [PATCH 1/5] feat: Add workspace_cargo_toml attribute --- crate_universe/private/crates_repository.bzl | 4 ++ crate_universe/private/crates_vendor.bzl | 12 ++++ crate_universe/private/splicing_utils.bzl | 9 ++- crate_universe/src/cli/splice.rs | 9 ++- crate_universe/src/cli/vendor.rs | 9 ++- crate_universe/src/splicing/splicer.rs | 48 ++++++++++------ .../tests/cargo_integration_test.rs | 56 ++++++++++++++++++- 7 files changed, 126 insertions(+), 21 deletions(-) diff --git a/crate_universe/private/crates_repository.bzl b/crate_universe/private/crates_repository.bzl index 70ce40bfb4..fb33735890 100644 --- a/crate_universe/private/crates_repository.bzl +++ b/crate_universe/private/crates_repository.bzl @@ -61,6 +61,7 @@ def _crates_repository_impl(repository_ctx): repository_ctx = repository_ctx, generator = generator, cargo_lockfile = lockfiles.cargo, + workspace_cargo_toml = repository_ctx.path(repository_ctx.attr.workspace_cargo_toml) if repository_ctx.attr.workspace_cargo_toml else None, splicing_manifest = splicing_manifest, config_path = config_path, cargo = cargo_path, @@ -231,6 +232,9 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i ), mandatory = True, ), + "workspace_cargo_toml": attr.label( + doc = "The path to the workspace `Cargo.toml` file.", + ), "compressed_windows_toolchain_names": attr.bool( doc = "Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.", default = True, diff --git a/crate_universe/private/crates_vendor.bzl b/crate_universe/private/crates_vendor.bzl index a195f8c20f..ef5c34deb3 100644 --- a/crate_universe/private/crates_vendor.bzl +++ b/crate_universe/private/crates_vendor.bzl @@ -326,6 +326,14 @@ def _crates_vendor_impl(ctx): ]) cargo_bazel_runfiles.extend([ctx.file.cargo_lockfile]) + # Add an optional `Cargo.toml` file. + if ctx.attr.workspace_cargo_toml: + args.extend([ + "--workspace-cargo-toml", + _runfiles_path(ctx.file.workspace_cargo_toml, is_windows), + ]) + cargo_bazel_runfiles.extend([ctx.file.workspace_cargo_toml]) + # Optionally include buildifier if ctx.attr.buildifier: args.extend(["--buildifier", _runfiles_path(ctx.executable.buildifier, is_windows)]) @@ -402,6 +410,10 @@ CRATES_VENDOR_ATTRS = { doc = "The path to an existing `Cargo.lock` file", allow_single_file = True, ), + "workspace_cargo_toml": attr.label( + doc = "The path to the workspace's `Cargo.toml` file", + allow_single_file = True, + ), "generate_binaries": attr.bool( doc = ( "Whether to generate `rust_binary` targets for all the binary crates in every package. " + diff --git a/crate_universe/private/splicing_utils.bzl b/crate_universe/private/splicing_utils.bzl index c9ccdb77f1..b898a83932 100644 --- a/crate_universe/private/splicing_utils.bzl +++ b/crate_universe/private/splicing_utils.bzl @@ -114,13 +114,14 @@ def create_splicing_manifest(repository_ctx): return splicing_manifest -def splice_workspace_manifest(repository_ctx, generator, cargo_lockfile, splicing_manifest, config_path, cargo, rustc): +def splice_workspace_manifest(repository_ctx, generator, workspace_cargo_toml, cargo_lockfile, splicing_manifest, config_path, cargo, rustc): """Splice together a Cargo workspace from various other manifests and package definitions Args: repository_ctx (repository_ctx): The rule's context object. generator (path): The `cargo-bazel` binary. cargo_lockfile (path): The path to a "Cargo.lock" file. + workspace_cargo_toml (path): The path to the workspace's "Cargo.toml" file. splicing_manifest (path): The path to a splicing manifest. config_path: The path to the config file (containing `cargo_bazel::config::Config`.) cargo (path): The path to a Cargo binary. @@ -153,6 +154,12 @@ def splice_workspace_manifest(repository_ctx, generator, cargo_lockfile, splicin repository_ctx.workspace_root, ] + if workspace_cargo_toml: + arguments.extend([ + "--workspace-cargo-toml", + workspace_cargo_toml, + ]) + # Optionally set the splicing workspace directory to somewhere within the repository directory # to improve the debugging experience. if CARGO_BAZEL_DEBUG in repository_ctx.os.environ: diff --git a/crate_universe/src/cli/splice.rs b/crate_universe/src/cli/splice.rs index c47fcd3a8b..d147b8beb3 100644 --- a/crate_universe/src/cli/splice.rs +++ b/crate_universe/src/cli/splice.rs @@ -60,6 +60,10 @@ pub struct SpliceOptions { #[clap(long)] pub nonhermetic_root_bazel_workspace_dir: PathBuf, + + // The path to the workspace cargo toml file + #[clap(long)] + pub workspace_cargo_toml: Option, } /// Combine a set of disjoint manifests into a single workspace. @@ -86,7 +90,10 @@ pub fn splice(opt: SpliceOptions) -> Result<()> { // Splice together the manifest let manifest_path = splicer - .splice_workspace(&opt.nonhermetic_root_bazel_workspace_dir) + .splice_workspace( + &opt.nonhermetic_root_bazel_workspace_dir, + opt.workspace_cargo_toml.as_deref(), + ) .context("Failed to splice workspace")?; // Generate a lockfile diff --git a/crate_universe/src/cli/vendor.rs b/crate_universe/src/cli/vendor.rs index ee2fa83b60..d50e43e20c 100644 --- a/crate_universe/src/cli/vendor.rs +++ b/crate_universe/src/cli/vendor.rs @@ -82,6 +82,10 @@ pub struct VendorOptions { /// You basically never want to use this value. #[clap(long)] pub nonhermetic_root_bazel_workspace_dir: Utf8PathBuf, + + /// The path to the workspace cargo toml file + #[clap(long)] + pub workspace_cargo_toml: Option, } /// Run buildifier on a given file. @@ -141,7 +145,10 @@ pub fn vendor(opt: VendorOptions) -> Result<()> { // Splice together the manifest let manifest_path = splicer - .splice_workspace(opt.nonhermetic_root_bazel_workspace_dir.as_std_path()) + .splice_workspace( + opt.nonhermetic_root_bazel_workspace_dir.as_std_path(), + opt.workspace_cargo_toml.as_deref(), + ) .context("Failed to splice workspace")?; // Gather a cargo lockfile diff --git a/crate_universe/src/splicing/splicer.rs b/crate_universe/src/splicing/splicer.rs index bfc3d7d84e..4030aaa27d 100644 --- a/crate_universe/src/splicing/splicer.rs +++ b/crate_universe/src/splicing/splicer.rs @@ -47,7 +47,19 @@ impl<'a> SplicerKind<'a> { manifests: &'a BTreeMap, splicing_manifest: &'a SplicingManifest, nonhermetic_root_bazel_workspace_dir: &Path, + workspace_cargo_toml: Option<&Utf8Path>, ) -> Result { + if let Some((path, manifies)) = workspace_cargo_toml + .and_then(|path| manifests.get_key_value(path)) + .and_then(|(path, manifest)| manifest.workspace.as_ref().map(|_| (path, manifest))) + { + return Ok(Self::Workspace { + path, + manifest: manifies, + splicing_manifest, + }); + } + let workspaces = discover_workspaces( manifests.keys().cloned().collect(), manifests, @@ -503,11 +515,13 @@ impl Splicer { pub(crate) fn splice_workspace( &self, nonhermetic_root_bazel_workspace_dir: &Path, + workspace_cargo_toml: Option<&Utf8Path>, ) -> Result { SplicerKind::new( &self.manifests, &self.splicing_manifest, nonhermetic_root_bazel_workspace_dir, + workspace_cargo_toml, )? .splice(&self.workspace_dir) } @@ -980,7 +994,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Locate cargo @@ -1023,7 +1037,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Locate cargo @@ -1074,7 +1088,7 @@ mod test { splicing_manifest, ) .unwrap() - .splice_workspace(Path::new("/doesnotexist")); + .splice_workspace(Path::new("/doesnotexist"), None); assert!(workspace_manifest.is_err()); @@ -1104,7 +1118,7 @@ mod test { splicing_manifest, ) .unwrap() - .splice_workspace(Path::new("/doesnotexist")); + .splice_workspace(Path::new("/doesnotexist"), None); assert!(workspace_manifest.is_err()); @@ -1172,7 +1186,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")); + .splice_workspace(Path::new("/doesnotexist"), None); assert!(workspace_manifest.is_err()); @@ -1209,7 +1223,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); let metadata = generate_metadata(workspace_manifest.as_path_buf()); @@ -1234,7 +1248,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Locate cargo @@ -1271,7 +1285,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Check the default resolver version @@ -1321,7 +1335,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Check the specified resolver version @@ -1381,7 +1395,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Check the default resolver version @@ -1423,7 +1437,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Ensure the patches match the expected value @@ -1486,7 +1500,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Ensure the patches match the expected value @@ -1537,7 +1551,7 @@ mod test { let workspace_manifest = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); // Ensure the patches match the expected value @@ -1579,7 +1593,7 @@ mod test { let workspace_root = tempfile::tempdir().unwrap(); let result = Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")); + .splice_workspace(Path::new("/doesnotexist"), None); // Confirm conflicting patches have been detected assert!(result.is_err()); @@ -1601,7 +1615,7 @@ mod test { let workspace_root = tempfile::tempdir().unwrap(); Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); let cargo_config = workspace_root.as_ref().join(".cargo").join("config.toml"); @@ -1634,7 +1648,7 @@ mod test { let workspace_root = tempfile::tempdir().unwrap(); Splicer::new(tempdir_utf8pathbuf(&workspace_root), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")) + .splice_workspace(Path::new("/doesnotexist"), None) .unwrap(); let cargo_config = workspace_root.as_ref().join(".cargo").join("config.toml"); @@ -1661,7 +1675,7 @@ mod test { let workspace_root = tempdir_utf8pathbuf(&temp_dir).join("workspace_root"); let splicing_result = Splicer::new(workspace_root.clone(), splicing_manifest) .unwrap() - .splice_workspace(Path::new("/doesnotexist")); + .splice_workspace(Path::new("/doesnotexist"), None); // Ensure cargo config files in parent directories lead to errors assert!(splicing_result.is_err()); diff --git a/crate_universe/tests/cargo_integration_test.rs b/crate_universe/tests/cargo_integration_test.rs index 49b739b8d2..2aaf503ebf 100644 --- a/crate_universe/tests/cargo_integration_test.rs +++ b/crate_universe/tests/cargo_integration_test.rs @@ -10,6 +10,7 @@ use std::fs; use std::path::PathBuf; use anyhow::{ensure, Context, Result}; +use camino::Utf8PathBuf; use cargo_bazel::cli::{splice, SpliceOptions}; use serde_json::{json, Value}; @@ -65,7 +66,12 @@ fn setup_cargo_env(rfiles: &runfiles::Runfiles) -> Result<(PathBuf, PathBuf)> { Ok((cargo, rustc)) } -fn run(repository_name: &str, manifests: HashMap, lockfile: &str) -> Value { +fn run( + repository_name: &str, + manifests: HashMap, + lockfile: &str, + workspace_cargo_toml: Option<&str>, +) -> Value { let scratch = tempfile::tempdir().unwrap(); let runfiles = runfiles::Runfiles::create().unwrap(); @@ -116,6 +122,9 @@ fn run(repository_name: &str, manifests: HashMap, lockfile: &str cargo, rustc, nonhermetic_root_bazel_workspace_dir: PathBuf::from("/doesnotexist"), + workspace_cargo_toml: workspace_cargo_toml.map(|s| { + Utf8PathBuf::from_path_buf(runfiles::rlocation!(runfiles, s).unwrap()).unwrap() + }), }) .unwrap(); @@ -149,6 +158,7 @@ fn feature_generator() { "//:test_input".to_string(), )]), "rules_rust/crate_universe/test_data/metadata/target_features/Cargo.lock", + None, ); assert_eq!( @@ -267,6 +277,7 @@ fn feature_generator_cfg_features() { "//:test_input".to_string(), )]), "rules_rust/crate_universe/test_data/metadata/target_cfg_features/Cargo.lock", + None, ); assert_eq!( @@ -348,6 +359,46 @@ fn feature_generator_workspace() { ), ]), "rules_rust/crate_universe/test_data/metadata/workspace/Cargo.lock", + None, + ); + + assert!(!metadata["metadata"]["cargo-bazel"]["tree_metadata"]["wgpu 0.14.0"].is_null()); +} + +#[test] +fn feature_generator_workspace_toml_file() { + if should_skip_test() { + eprintln!("Skipping!"); + return; + } + + let r = runfiles::Runfiles::create().unwrap(); + let metadata = run( + "workspace_test", + HashMap::from([ + ( + runfiles::rlocation!( + r, + "rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml" + ) + .unwrap() + .to_string_lossy() + .to_string(), + "//:test_input".to_string(), + ), + ( + runfiles::rlocation!( + r, + "rules_rust/crate_universe/test_data/metadata/workspace/child/Cargo.toml" + ) + .unwrap() + .to_string_lossy() + .to_string(), + "//crate_universe:test_data/metadata/workspace/child/Cargo.toml".to_string(), + ), + ]), + "rules_rust/crate_universe/test_data/metadata/workspace/Cargo.lock", + Some("rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml"), ); assert!(!metadata["metadata"]["cargo-bazel"]["tree_metadata"]["wgpu 0.14.0"].is_null()); @@ -374,6 +425,7 @@ fn feature_generator_crate_combined_features() { "//:test_input".to_string(), )]), "rules_rust/crate_universe/test_data/metadata/crate_combined_features/Cargo.lock", + Some("rules_rust/crate_universe/test_data/metadata/crate_combined_features/Cargo.lock"), ); // serde appears twice in the list of dependencies, with and without derive features @@ -415,6 +467,7 @@ fn resolver_2_deps() { "//:test_input".to_string(), )]), "rules_rust/crate_universe/test_data/metadata/resolver_2_deps/Cargo.lock", + None, ); assert_eq!( @@ -539,6 +592,7 @@ fn host_specific_build_deps() { "//:test_input".to_string(), )]), "rules_rust/crate_universe/test_data/metadata/host_specific_build_deps/Cargo.lock", + None, ); assert_eq!( From 682021b039f10328fad8ba16e3cf2c98daa2ae25 Mon Sep 17 00:00:00 2001 From: Gio Gutierrez Date: Fri, 6 Dec 2024 13:30:28 -0500 Subject: [PATCH 2/5] Regenerate documentation --- docs/src/crate_universe.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/docs/src/crate_universe.md b/docs/src/crate_universe.md index 11b7831c26..089889df5d 100644 --- a/docs/src/crate_universe.md +++ b/docs/src/crate_universe.md @@ -302,7 +302,7 @@ There is an example of this in the "complicated dependencies" section of https:/ crates_vendor(name, annotations, bazel, buildifier, cargo_bazel, cargo_config, cargo_lockfile, generate_binaries, generate_build_scripts, generate_target_compatible_with, manifests, mode, packages, render_config, repository_name, splicing_config, - supported_platform_triples, vendor_path) + supported_platform_triples, vendor_path, workspace_cargo_toml) A rule for defining Rust dependencies (crates) and writing targets for them to the current workspace. @@ -398,6 +398,7 @@ call against the generated workspace. The following table describes how to contr | splicing_config | The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\%rsplicing_config` to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | `""` | | supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | `["aarch64-apple-darwin", "aarch64-apple-ios", "aarch64-apple-ios-sim", "aarch64-linux-android", "aarch64-pc-windows-msvc", "aarch64-unknown-fuchsia", "aarch64-unknown-linux-gnu", "aarch64-unknown-nixos-gnu", "aarch64-unknown-nto-qnx710", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-apple-darwin", "i686-linux-android", "i686-pc-windows-msvc", "i686-unknown-freebsd", "i686-unknown-linux-gnu", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "thumbv8m.main-none-eabi", "wasm32-unknown-unknown", "wasm32-wasip1", "x86_64-apple-darwin", "x86_64-apple-ios", "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", "x86_64-unknown-fuchsia", "x86_64-unknown-linux-gnu", "x86_64-unknown-nixos-gnu", "x86_64-unknown-none"]` | | vendor_path | The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root | String | optional | `"crates"` | +| workspace_cargo_toml | The path to the workspace's `Cargo.toml` file | Label | optional | `None` | @@ -755,7 +756,7 @@ crates_repository(name, generate_target_compatible_with, generator, generator_sha256s, generator_urls, isolated, lockfile, manifests, packages, quiet, render_config, repin_instructions, repo_mapping, rust_toolchain_cargo_template, rust_toolchain_rustc_template, - rust_version, splicing_config, supported_platform_triples) + rust_version, splicing_config, supported_platform_triples, workspace_cargo_toml) A rule for defining and downloading Rust dependencies (crates). This rule @@ -876,6 +877,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i | rust_version | The version of Rust the currently registered toolchain is using. Eg. `1.56.0`, or `nightly/2021-09-08` | String | optional | `"1.83.0"` | | splicing_config | The configuration flags to use for splicing Cargo maniests. Use `//crate_universe:defs.bzl\%rsplicing_config` to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | `""` | | supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | `["aarch64-apple-darwin", "aarch64-apple-ios", "aarch64-apple-ios-sim", "aarch64-linux-android", "aarch64-pc-windows-msvc", "aarch64-unknown-fuchsia", "aarch64-unknown-linux-gnu", "aarch64-unknown-nixos-gnu", "aarch64-unknown-nto-qnx710", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-apple-darwin", "i686-linux-android", "i686-pc-windows-msvc", "i686-unknown-freebsd", "i686-unknown-linux-gnu", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "thumbv8m.main-none-eabi", "wasm32-unknown-unknown", "wasm32-wasip1", "x86_64-apple-darwin", "x86_64-apple-ios", "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", "x86_64-unknown-fuchsia", "x86_64-unknown-linux-gnu", "x86_64-unknown-nixos-gnu", "x86_64-unknown-none"]` | +| workspace_cargo_toml | The path to the workspace `Cargo.toml` file. | Label | optional | `None` | **ENVIRONMENT VARIABLES** From b705cd5d271804f8608cc6b490e3f0bef3f7c4fe Mon Sep 17 00:00:00 2001 From: Gio Gutierrez Date: Fri, 6 Dec 2024 13:38:18 -0500 Subject: [PATCH 3/5] Regenerate documentation --- crate_universe/private/crates_repository.bzl | 6 +++--- crate_universe/private/crates_vendor.bzl | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/crate_universe/private/crates_repository.bzl b/crate_universe/private/crates_repository.bzl index fb33735890..770dff8bdd 100644 --- a/crate_universe/private/crates_repository.bzl +++ b/crate_universe/private/crates_repository.bzl @@ -232,9 +232,6 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i ), mandatory = True, ), - "workspace_cargo_toml": attr.label( - doc = "The path to the workspace `Cargo.toml` file.", - ), "compressed_windows_toolchain_names": attr.bool( doc = "Wether or not the toolchain names of windows toolchains are expected to be in a `compressed` format.", default = True, @@ -341,6 +338,9 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i doc = "A set of all platform triples to consider when generating dependencies.", default = SUPPORTED_PLATFORM_TRIPLES, ), + "workspace_cargo_toml": attr.label( + doc = "The path to the workspace `Cargo.toml` file.", + ), }, environ = CRATES_REPOSITORY_ENVIRON, ) diff --git a/crate_universe/private/crates_vendor.bzl b/crate_universe/private/crates_vendor.bzl index ef5c34deb3..dbde036a89 100644 --- a/crate_universe/private/crates_vendor.bzl +++ b/crate_universe/private/crates_vendor.bzl @@ -410,10 +410,6 @@ CRATES_VENDOR_ATTRS = { doc = "The path to an existing `Cargo.lock` file", allow_single_file = True, ), - "workspace_cargo_toml": attr.label( - doc = "The path to the workspace's `Cargo.toml` file", - allow_single_file = True, - ), "generate_binaries": attr.bool( doc = ( "Whether to generate `rust_binary` targets for all the binary crates in every package. " + @@ -474,6 +470,10 @@ CRATES_VENDOR_ATTRS = { doc = "The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root", default = "crates", ), + "workspace_cargo_toml": attr.label( + doc = "The path to the workspace's `Cargo.toml` file", + allow_single_file = True, + ), } crates_vendor = rule( From b64ac12f68260156f46638a8eca2f46b89b796e1 Mon Sep 17 00:00:00 2001 From: Gio Gutierrez Date: Fri, 6 Dec 2024 13:42:54 -0500 Subject: [PATCH 4/5] feat: Use different cargo file --- .../metadata/workspace/Cargo.bazel.toml | 16 ++++++++++++++++ crate_universe/tests/cargo_integration_test.rs | 4 ++-- 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 crate_universe/test_data/metadata/workspace/Cargo.bazel.toml diff --git a/crate_universe/test_data/metadata/workspace/Cargo.bazel.toml b/crate_universe/test_data/metadata/workspace/Cargo.bazel.toml new file mode 100644 index 0000000000..a5202877c4 --- /dev/null +++ b/crate_universe/test_data/metadata/workspace/Cargo.bazel.toml @@ -0,0 +1,16 @@ +[workspace] +members = [ + "child", +] +resolver = "2" + +[package] +name = "parent" +version = "0.1.0" +edition = "2018" + +# Required to satisfy cargo but no `lib.rs` is expected to +# exist within test data. +[lib] +path = "lib.rs" + diff --git a/crate_universe/tests/cargo_integration_test.rs b/crate_universe/tests/cargo_integration_test.rs index 2aaf503ebf..86ea3e42ff 100644 --- a/crate_universe/tests/cargo_integration_test.rs +++ b/crate_universe/tests/cargo_integration_test.rs @@ -379,7 +379,7 @@ fn feature_generator_workspace_toml_file() { ( runfiles::rlocation!( r, - "rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml" + "rules_rust/crate_universe/test_data/metadata/workspace/Cargo.bazel.toml" ) .unwrap() .to_string_lossy() @@ -398,7 +398,7 @@ fn feature_generator_workspace_toml_file() { ), ]), "rules_rust/crate_universe/test_data/metadata/workspace/Cargo.lock", - Some("rules_rust/crate_universe/test_data/metadata/workspace/Cargo.toml"), + Some("rules_rust/crate_universe/test_data/metadata/workspace/Cargo.bazel.toml"), ); assert!(!metadata["metadata"]["cargo-bazel"]["tree_metadata"]["wgpu 0.14.0"].is_null()); From 649565fe733a8b305d60a45973bb5cf0b71d2b7c Mon Sep 17 00:00:00 2001 From: Gio Gutierrez Date: Sat, 7 Dec 2024 11:41:08 -0500 Subject: [PATCH 5/5] feat: Ignore entry errors --- crate_universe/src/metadata/workspace_discoverer.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/crate_universe/src/metadata/workspace_discoverer.rs b/crate_universe/src/metadata/workspace_discoverer.rs index 51eb0fd233..ec89704ec6 100644 --- a/crate_universe/src/metadata/workspace_discoverer.rs +++ b/crate_universe/src/metadata/workspace_discoverer.rs @@ -92,8 +92,10 @@ fn discover_workspaces_with_cache( true }) { - let entry = - entry.context("Failed to walk filesystem finding workspace Cargo.toml files")?; + let Ok(entry) = entry else { + // Skip errors. + continue; + }; if entry.file_name() != "Cargo.toml" { continue;