Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue using async_trait with zigbuild for x86_64-unknown-linux-gnu #4

Closed
nmoutschen opened this issue Feb 17, 2022 · 4 comments
Closed
Assignees
Labels
documentation Improvements or additions to documentation

Comments

@nmoutschen
Copy link

Hi! 馃憢 First of all, thanks a lot for this awesome project!

I'm trying to use cargo zigbuild with this project that uses the async_trait crate from Ubuntu on x86_64.

When I use cargo zigbuild --target aarch64-unknown-linux-gnu, it works without any error. Same with cargo build --target x86_64-unknown-linux-gnu.
However, when I run cargo zigbuild --target x86_64-unknown-linux-gnu, I get a pretty lengthy error about async functions not supported in traits, and that thiserror cannot allocate memory in static TLS block.

Error trace from compilation:

error[E0706]: functions in traits cannot be declared `async`
  --> src/event_bus/eventbridge/mod.rs:32:5
   |
32 |     #[instrument(skip(self))]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
33 |     async fn send_event(&self, event: &Self::E) -> Result<(), Error> {
   |     ----- `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0706]: functions in traits cannot be declared `async`
  --> src/event_bus/eventbridge/mod.rs:45:5
   |
45 |     #[instrument(skip(self, events))]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
46 |     async fn send_events(&self, events: &[Self::E]) -> Result<(), Error> {
   |     ----- `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0706]: functions in traits cannot be declared `async`
  --> src/event_bus/void.rs:18:5
   |
18 |       async fn send_event(&self, _: &Self::E) -> Result<(), Error> {
   |       ^----
   |       |
   |  _____`async` because of this
   | |
19 | |         Err(Error::InternalError("send_event is not supported"))
20 | |     }
   | |_____^
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/event_bus/void.rs:22:5
   |
22 |       async fn send_events(&self, _: &[Self::E]) -> Result<(), Error> {
   |       ^----
   |       |
   |  _____`async` because of this
   | |
23 | |         Err(Error::InternalError("send_events is not supported"))
24 | |     }
   | |_____^
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/event_bus/mod.rs:14:5
   |
14 |     async fn send_event(&self, event: &Self::E) -> Result<(), Error>;
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/event_bus/mod.rs:15:5
   |
15 |     async fn send_events(&self, events: &[Self::E]) -> Result<(), Error>;
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/dynamodb/mod.rs:32:5
   |
32 |     #[instrument(skip(self))]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
33 |     async fn all(&self, next: Option<&str>) -> Result<ProductRange, Error> {
   |     ----- `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/dynamodb/mod.rs:60:5
   |
60 |     #[instrument(skip(self))]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
61 |     async fn get(&self, id: &str) -> Result<Option<Product>, Error> {
   |     ----- `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/dynamodb/mod.rs:81:5
   |
81 |     #[instrument(skip(self))]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
82 |     async fn put(&self, product: &Product) -> Result<(), Error> {
   |     ----- `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/dynamodb/mod.rs:98:5
   |
98 |     #[instrument(skip(self))]
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^
99 |     async fn delete(&self, id: &str) -> Result<(), Error> {
   |     ----- `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait
   = note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/memory.rs:28:5
   |
28 |       async fn all(&self, _: Option<&str>) -> Result<ProductRange, Error> {
   |       ^----
   |       |
   |  _____`async` because of this
   | |
29 | |         Ok(ProductRange {
30 | |             products: self
31 | |                 .data
...  |
38 | |         })
39 | |     }
   | |_____^
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/memory.rs:44:5
   |
44 |       async fn get(&self, id: &str) -> Result<Option<Product>, Error> {
   |       ^----
   |       |
   |  _____`async` because of this
   | |
45 | |         Ok(self.data.read().unwrap().get(id).cloned())
46 | |     }
   | |_____^
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/memory.rs:51:5
   |
51 |       async fn put(&self, product: &Product) -> Result<(), Error> {
   |       ^----
   |       |
   |  _____`async` because of this
   | |
52 | |         self.data
53 | |             .write()
54 | |             .unwrap()
55 | |             .insert(product.id.clone(), product.clone());
56 | |         Ok(())
57 | |     }
   | |_____^
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/memory.rs:62:5
   |
62 |       async fn delete(&self, id: &str) -> Result<(), Error> {
   |       ^----
   |       |
   |  _____`async` because of this
   | |
63 | |         self.data.write().unwrap().remove(id);
64 | |         Ok(())
65 | |     }
   | |_____^
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/mod.rs:22:5
   |
22 |     async fn all(&self, next: Option<&str>) -> Result<ProductRange, Error>;
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/mod.rs:28:5
   |
28 |     async fn get(&self, id: &str) -> Result<Option<Product>, Error>;
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/mod.rs:34:5
   |
34 |     async fn put(&self, product: &Product) -> Result<(), Error>;
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error[E0706]: functions in traits cannot be declared `async`
  --> src/store/mod.rs:40:5
   |
40 |     async fn delete(&self, id: &str) -> Result<(), Error>;
   |     -----^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |     |
   |     `async` because of this
   |
   = note: `async` trait functions are not currently supported
   = note: consider using the `async-trait` crate: https://crates.io/crates/async-trait

error: /home/CENSORED/projects/serverless-rust-demo/target/debug/deps/libthiserror_impl-83b2547aa6b39328.so: cannot allocate memory in static TLS block
 --> src/event_bus/eventbridge/ext.rs:2:5
  |
2 | use aws_sdk_eventbridge::model::PutEventsRequestEntry;
  |     ^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0706`.
error: could not compile `products` due to 19 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed

For reference, here is one of the traits that cargo zigbuild complains about:

#[async_trait]
pub trait EventBus {
    type E;

    async fn send_event(&self, event: &Self::E) -> Result<(), Error>;
    async fn send_events(&self, events: &[Self::E]) -> Result<(), Error>;
}
@messense messense self-assigned this Feb 18, 2022
@messense
Copy link
Member

I can reproduce this issue on Ubuntu 20.04, but it seems to work fine on macOS.

@messense
Copy link
Member

messense commented Feb 18, 2022

So this issue is caused by Cargo using the same linker when --target is the same as the host target, for more information see rust-lang/cargo#3349 and there is already an unstable option target-applies-to-host to fix it. But you need to use the nightly Rust compiler because that feature is unstable.

First create the .cargo/config.toml file and add target-applies-to-host = false to its content, then run cargo zigbuild with the unstable option enabled:

cargo +nightly zigbuild --target x86_64-unknown-linux-gnu -Ztarget-applies-to-host

@messense messense added the documentation Improvements or additions to documentation label Feb 18, 2022
@messense
Copy link
Member

messense commented Feb 18, 2022

With commit 407e2e4, if you don't specify a glibc version it'll skip setting up zig as linker and just use system linker instead when the cross target is exactly the same as the host target. That should avoid having to use the target-applies-to-host option most of the time.

@messense
Copy link
Member

messense commented Feb 22, 2022

With #9 which is released in v0.5.2, the only workaround is switching to nightly Rust, cargo-zigbuild will take care of enabling the target-applies-to-host option automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants