This PR makes two changes:
1. It removes the duplicately generated code in the prost `lib.rs`.
2. It allows opting into transitively including all of the proto deps.
## Duplicately generated code.
Previously there was a bug where we generated the same code at every
proto package level. This created separate Rust types for each module
which made it easy to accidentally import an incompatible type.
Before:
```rust
// @generated
pub mod b_ar {
pub mod b_az {
pub mod qaz {
pub mod qu_x {
// @generated
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::Foo,
>,
#[prost(message, optional, tag = "3")]
pub nested_foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::foo::NestedFoo,
>,
}
/// Nested message and enum types in `Bar`.
pub mod bar {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Baz {
#[prost(string, tag = "4")]
pub name: ::prost::alloc::string::String,
}
}
// @@protoc_insertion_point(module)
}
}
}
}
pub mod b_az {
pub mod qaz {
pub mod qu_x {
// @generated
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub foo:
::core::option::Option<::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::Foo>,
#[prost(message, optional, tag = "3")]
pub nested_foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::foo::NestedFoo,
>,
}
/// Nested message and enum types in `Bar`.
pub mod bar {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Baz {
#[prost(string, tag = "4")]
pub name: ::prost::alloc::string::String,
}
}
// @@protoc_insertion_point(module)
}
}
}
pub mod qaz {
pub mod qu_x {
// @generated
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub foo: ::core::option::Option<::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::Foo>,
#[prost(message, optional, tag = "3")]
pub nested_foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::foo::NestedFoo,
>,
}
/// Nested message and enum types in `Bar`.
pub mod bar {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Baz {
#[prost(string, tag = "4")]
pub name: ::prost::alloc::string::String,
}
}
// @@protoc_insertion_point(module)
}
}
pub mod qu_x {
// @generated
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub foo: ::core::option::Option<::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::Foo>,
#[prost(message, optional, tag = "3")]
pub nested_foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::foo::NestedFoo,
>,
}
/// Nested message and enum types in `Bar`.
pub mod bar {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Baz {
#[prost(string, tag = "4")]
pub name: ::prost::alloc::string::String,
}
}
// @@protoc_insertion_point(module)
}
```
Now:
```rust
// @generated
pub use foo_proto;
pub mod b_ar {
pub mod b_az {
pub mod qaz {
pub mod qu_x {
// @generated
// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Bar {
#[prost(string, tag = "1")]
pub name: ::prost::alloc::string::String,
#[prost(message, optional, tag = "2")]
pub foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::Foo,
>,
#[prost(message, optional, tag = "3")]
pub nested_foo: ::core::option::Option<
::foo_proto::foo::quu_x::co_rg_e::grault::ga_rply::foo::NestedFoo,
>,
}
/// Nested message and enum types in `Bar`.
pub mod bar {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Baz {
#[prost(string, tag = "4")]
pub name: ::prost::alloc::string::String,
}
}
// @@protoc_insertion_point(module)
}
}
}
}
```
## Including transitive depenencies.
Because the proto rules rely on aspects, we are compiling targets which
may not be directly accessible as a label in Bazel. Out of convenience,
the original implementation automatically included all transitive
dependencies to the `rust_library` or `rust_binary` that depended on the
proto crate.
As an example, let's say we have the following hierarchy:
```
my_crate -> a.proto
a.proto -> b.proto
b.proto -> c.proto
```
The previous setup made it possible for `my_crate` to directly import
`a_proto`, `b_proto`, and `c_proto`.
This is problematic though because it leads to significant crate
dependency bloat. `my_crate` may only use `a_proto` directly but it now
also depends on the unused crates `b_proto` and `c_proto`.
This PR makes every generated crate re-export its direct dependencies.
So, for the same example we now can import via those reexports:
```rust
//! my_crate
use a_proto::A;
use a_proto::b_proto::B;
use a_proto::b_proto::c_proto::C;
```
This means that you can also add the rustc flag
`-Dunused_crate_dependencies` to ensure you're not depending on crates
which are unused. In our monorepo we've had to disable this flag for
crates depending on protos.
If you need to depend on an intermediate crate directly, you can just
add a `rust_prost_library` definition and since it's all generated with
aspects, it will be the identical underlying crate.
Finally, if you really want the extra crates included, you can set the
flag `include_transitive_deps` on the prost toolchain and that will
bring back the previous behavior.
# PR Summary
Commit c080d7bfa1 moved the location of
`BUILD.bazel`. This PR adjusts sources to changes.
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
This change allows for the definition of platform constraints without
introducing plumbing in repository rules that would attempt to fetch
artifacts that do not exist.
---------
Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
This PR adds support for `wasm64-unknown-unknown` although there are no
prebuilt `wasm64-unknown-unknown` rustc toolchains. Compiling for
`wasm64` will be left up to anyone using this target triple until we get
T2 support for wasm64.
---------
Co-authored-by: Andre Brisco <andre.brisco@freeform.co>
This is a rollforward of
https://github.com/bazelbuild/rules_rust/pull/2803, but behind the
`incompatible_change_rust_test_compilation_output_directory`
incompatible flag.
This PR also makes `rust_test` put its compilation outputs in the same
directory as the `rust_library` rule (i.e. not in a `test-{hash}`
subdirectory anymore).
After this change both the `rust_library` and `rust_test` rules will put
all its compilation outputs in the same directory, but there won't be
any name collisions in non-sandboxed environments (see
https://github.com/bazelbuild/rules_rust/pull/1427 for more context).
Issue with context: https://github.com/bazelbuild/rules_rust/issues/2827
https://github.com/bazelbuild/rules_rust/pull/2826 ended up introducing
a regression for crates with build scripts that require extra data at
compile time. This change adds a default glob for all
`cargo_build_script` targets generated by `crate_universe` and also
introduces an `annotation.build_script_compile_data` attribute to add
custom targets to generated outputs.
This updates docs to be built using
[mdbook](https://rust-lang.github.io/mdBook/) for improved readability
and search-ability. Note that the raw markdown from stardoc is still
committed to the repo and can be easily viewed at any commit to ensure
users are able to get the correct docs for their current version of
rules_rust.
Bazel `6.4` was released in, Oct 2023, we're well beyond the support
window.
I updated straight to `7.3.1` because according to the
[docs](https://github.com/bazelbuild/bazel/releases/tag/7.3.1) it's
fully compatible with `7.0`:
> Bazel 7.3.1 is a patch LTS release. It is fully backward compatible
with Bazel 7.0 and contains selected changes by the Bazel community and
Google engineers.
Co-authored-by: UebelAndre <github@uebelandre.com>
## Problem
Tracking Issue: https://github.com/bazelbuild/rules_rust/issues/2814
The command in doc looks outdated and I'm getting
```
❯ bazel build --@rules_rust//:output_diagnostics=true --output_groups=+rust_lib_rustc_output,+rust_metadata_rustc_output //hellow_world
WARNING: Target pattern parsing failed.
ERROR: Skipping '@rules_rust//:output_diagnostics': no such target '@@rules_rust~//:output_diagnostics': target 'output_diagnostics' not declared in package '' defined by /private/var/tmp/_bazel_xxx/c947415ff64a19538545e009b2e6df63/external/rules_rust~/BUILD.bazel
ERROR: @rules_rust//:output_diagnostics :: Error loading option @rules_rust//:output_diagnostics: no such target '@@rules_rust~//:output_diagnostics': target 'output_diagnostics' not declared in package '' defined by /private/var/tmp/_bazel_xxx/c947415ff64a19538545e009b2e6df63/external/rules_rust~/BUILD.bazel
```
After checking the code
dff064e21b/BUILD.bazel (L39),
I feel it should be `rustc_output_diagnostics`
## Test
```
bazel build --@rules_rust//:rustc_output_diagnostics=true --output_groups=+rust_lib_rustc_output,+rust_metadata_rustc_output //hello_world
INFO: Analyzed target //hello_world:hello_world (2 packages loaded, 1443 targets configured).
ERROR: /Users/xxx/dev/bazel-rust-example/hello_world/BUILD:3:12: Compiling Rust bin hello_world (1 files) failed: (Exit 1): process_wrapper failed: error executing Rustc command (from target //hello_world:hello_world) bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/external/rules_rust~/util/process_wrapper/process_wrapper --subst 'pwd=${pwd}' --rustc-output-format rendered --output-file ... (remaining 27 arguments skipped)
Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
error[E0423]: expected function, found macro `println`
--> hello_world/src/main.rs:2:5
|
2 | println("Hello, world!");
| ^^^^^^^ not a function
|
help: use `!` to invoke the macro
|
2 | println!("Hello, world!");
| +
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0423`.
Target //hello_world:hello_world failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.322s, Critical Path: 0.06s
INFO: 4 processes: 4 internal.
ERROR: Build did NOT complete successfully
```
This reverts commit b4ccc97bbe.
That commit makes a behavior change, and thus should have been protected
by a feature flag. This will be done in a future PR.
This PR also makes `rust_test` put its compilation outputs in the same
directory as the `rust_library` rule (i.e. not in a `test-{hash}`
subdirectory anymore).
After this change both the `rust_library` and `rust_test` rules will put
all its compilation outputs in the same directory, but there won't be
any name collisions in non-sandboxed environments (see
https://github.com/bazelbuild/rules_rust/pull/1427 for more context).
This is a partial rollback of
10185339dd
and
26344d4cd7.
Somehow in PR #2713, the bazelmod docs that I wrote went missing.
This PR restores the bazelmod docs.
Should be easy to review.
---------
Signed-off-by: Marvin Hansen <marvin.hansen@gmail.com>
Most notably in places where `version` and `iso_date` were used, the
`versions` parameter should be used instead. There have long since
prints guiding folks to use `versions` over the previous two.
This is similar to our `$(location)` expansion support for
`stdlib_linkflags`. We found ourselves needing this when defining a
custom rust dfsan-enabled toolchain. There, rustc needs an extra
`-Zsanitizer-dataflow-abilist=/path/to/abilist.txt` flag.
This patch lets us do this by specifying the abilist file in
`rust_stdlib_filegroup.srcs` and passing it as
`rust_toolchain.extra_rustc_flags =
["-Zsanitizer-dataflow-abilist=$(location :dfsan_abilist)"]`.
While looking into this, I noticed and fixed an issue: the sources of
the `rust_stdlib_filegroup` passed to `rust_toolchain.rust_std` weren't
made available downstream to the compile actions. I believe location
expansion was working before for `stdlib_linkflags` just because the
targets used in this location expansion were made available indirectly
e.g., implicitly by the underlying C++ toolchain, or via `rustc_lib`.
Add customizable instructions to re-pin the repository if required. Many
people have wrapper scripts for keeping dependencies up to date, and
would like to point users to that instead of the default.
This follows the logic of
https://github.com/bazelbuild/rules_jvm_external/pull/1000
---------
Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
This allows using a shared (generally pre-built) protoc definition,
rather than needing to supply one via a label.
We allow (but deprecate) users to still bring along their own protoc as
a label, but if they want to do so, they will need to ensure they have
the correct repositories registered in order to build/bring their
protoc, rather than us setting them up by default.
Currently, `rust_test_suite` is not capable of supporting integration
tests with shared modules. A very basic example is:
```
[crate]/
src/
lib.rs
tests/
integration_test_a.rs
integration_test_b.rs
util/
mod.rs
```
With `integration_test_a.rs` and `integration_test_b.rs` importing code
from `util`.
This PR adds a `shared_srcs` argument to `rust_test_suite`. That way, we
can finally compile and run those tests with:
```python
rust_test_suite(
name = "integrated_tests_suite",
srcs = glob(["tests/**"]),
shared_srcs = ["tests/util/mod.rs"],
deps = [":example_dep"],
)
```
Previously the bzlmod symbols were not documented at all, and in places
they are different from the WORKSPACE versions.
This at least hosts both.
Fixes #2696
Allow external crates to be aliased to local targets
In some cases it may be desirable to alias a public crate to a locally
define rust_library target, even for dependencies of crates that are not
being overriden.
Support this use case by swapping out `alias` for `rust_library` when
a user overrides the target using an annotation.
---------
Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
This PR shortens the path to the build script (potentially
significantly), which helps with long-path issues on Windows.
This change is motivated by the crate `tree-sitter-embedded-template`,
which has a too long path to the compiled build script otherwise, and
which then fails to build on Windows in a `bzlmod` setting.
Fixes https://github.com/bazelbuild/rules_rust/issues/2520.
The cargo build script name was used to automatically derive the cargo
package name, this now went into a separate override parameter that is
generated by the crate-universe tooling.
There's never two build scripts in a single crate, so I don't see how
having a single build script target with a single name would be a
problem.
Modifies the default of `rustfmt_version` so that it uses the first
version in the `versions` list. If there are multiple versions given
then falls back to the original default of `DEFAULT_NIGHTLY_VERSION`.
We tripped over this where supply a single Rust version (non-nightly),
but started using nightly features in `rustfmt.toml` and could no longer
format directly with `cargo fmt`. This helped align the versions of both
toolchains.
---------
Co-authored-by: Daniel Wagner-Hall <dwagnerhall@apple.com>
Bazel's `http_archive` and `http_file` use `~/.netrc` file to load HTTP
credentials.
Now rules_rust use the same approach if no other authorization
parameters are provided.
NOTE: `http_archive` supports passing a custom `netrc` and
`auth_patterns`. But this parameters are not added to the rust rules at
the moment.
---------
Co-authored-by: Daniel Wagner-Hall <dawagner@gmail.com>
This allows us to build more things with bzlmod with `WORKSPACE.bazel`
disabled. This is required because when you use it as a module, it can't
load dependencies from `WORKSPACE.bazel`.
`bazel build --enable_bzlmod --noenable_workspace --lockfile_mode=off
//... --keep_going`
Before: Analysis succeeded for 937 of 978 top-level targets
After: Analysis succeeded for 978 of 982 top-level targets
Once we get those remaining targets working, we can then change our CI
to add `--noenable_workspace` to our bzlmod configuration.
Attempts to follow the cargo proposal linked below to remove debug info
for release builds. Furthermore this should uphold the expected behavior
of bazel for cc builds which automatically strips debug symbols for
optimized builds.
https://github.com/rust-lang/cargo/issues/4122#issuecomment-1868318860
This change updates rules_rust repository rules to report when the rules
are not guaranteed to be reproducible due to missing sha256 values when
fetching Rust artifacts. A common case for this is when users request
newer versions of Rust than the ones referenced in
[@rules_rust//rust:known_shas.bzl](https://github.com/bazelbuild/rules_rust/blob/0.41.0/rust/known_shas.bzl),
thus leading to unnecessary downloads or potential security issues.
After this PR, users will see warnings similar to when `integrity` is
omitted from `http_archive` definitions
```
DEBUG: Rule 'rust_analyzer_1.77.1_tools' indicated that a canonical reproducible form can be obtained by modifying arguments sha256s = {"rust-src-1.77.1.tar.xz": "ad2064aa4a444ae35d55b8b57bec837b90c4f10fe6d721f8fce86d7a1992f607", "rustc-1.77.1-aarch64-apple-darwin.tar.xz": "8da1814eb8358236e88ceb155e32f0f34bbe71cb19cd9700c7eb40e675738d77"}
DEBUG: Repository rust_analyzer_1.77.1_tools instantiated at:
/Users/user/rules_rust/WORKSPACE.bazel:7:25: in <toplevel>
/Users/user/rules_rust/rust/repositories.bzl:202:10: in rust_register_toolchains
/private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/external/bazel_tools/tools/build_defs/repo/utils.bzl:240:18: in maybe
/Users/user/rules_rust/rust/repositories.bzl:737:45: in rust_analyzer_toolchain_repository
Repository rule rust_analyzer_toolchain_tools_repository defined at:
/Users/user/rules_rust/rust/repositories.bzl:705:59: in <toplevel>
DEBUG: Rule 'rust_darwin_aarch64__aarch64-apple-darwin__stable_tools' indicated that a canonical reproducible form can be obtained by modifying arguments sha256s = {"rustc-1.77.1-aarch64-apple-darwin.tar.xz": "8da1814eb8358236e88ceb155e32f0f34bbe71cb19cd9700c7eb40e675738d77", "clippy-1.77.1-aarch64-apple-darwin.tar.xz": "343f125b0c05dd756e71992c04fb5a4a29ce705c50a739d76eb7f38b088103ac", "cargo-1.77.1-aarch64-apple-darwin.tar.xz": "c0249b6c247953cbe3b01e276988b6ca600aeba4e91332cd2ddaa0b7eee4dfb7", "llvm-tools-1.77.1-aarch64-apple-darwin.tar.xz": "dfee0aaed6b24d34362f229f2926e6b1805f2a8ec4a090c7c28837b82a9bdfd1", "rust-std-1.77.1-aarch64-apple-darwin.tar.xz": "3b5b71e40d934de25dcb553b9df28289d38640e6fa2654b42410f4cc8d9bed2a"}
DEBUG: Repository rust_darwin_aarch64__aarch64-apple-darwin__stable_tools instantiated at:
/Users/user/rules_rust/WORKSPACE.bazel:7:25: in <toplevel>
/Users/user/rules_rust/rust/repositories.bzl:230:14: in rust_register_toolchains
/private/var/tmp/_bazel_user/76282c66b0dfe3c5cb9a230bdc913a52/external/bazel_tools/tools/build_defs/repo/utils.bzl:240:18: in maybe
/Users/user/rules_rust/rust/repositories.bzl:1043:61: in rust_repository_set
/Users/user/rules_rust/rust/repositories.bzl:601:36: in rust_toolchain_repository
Repository rule rust_toolchain_tools_repository defined at:
/Users/user/rules_rust/rust/repositories.bzl:475:50: in <toplevel>
```
The above was produced by running `bazel test //...` after applying the
following diff:
```diff
diff --git a/rust/private/common.bzl b/rust/private/common.bzl
index 52728fe7..c035ad5a 100644
--- a/rust/private/common.bzl
+++ b/rust/private/common.bzl
@@ -31,7 +31,7 @@ load(":providers.bzl", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo
#
# Note: Code in `.github/workflows/crate_universe.yaml` looks for this line, if
# you remove it or change its format, you will also need to update that code.
-DEFAULT_RUST_VERSION = "1.77.0"
+DEFAULT_RUST_VERSION = "1.77.1"
DEFAULT_NIGHTLY_ISO_DATE = "2024-03-21"
```
closes https://github.com/bazelbuild/rules_rust/issues/1825
This also plumbs `target_compatible_with` and `exec_compatible_with`
into the `rust_bindgen` target from `rust_bindgen_library` to account
for cases where the `"manual"` tag is not sufficient in disabling the
target (e.g. `cquery`).