Bevy setup fails because of foreign APIs/missing dependencies - rust

I'm trying to set up Bevy 0.7.0 for the first time on Ubuntu 20.04, following the official guide on their website, and as soon as I configured it for fast compiles, it stopped compiling altogether. At first, it didn't find the clang linker, even though I had installed lld as instructed, but I solved that by installing the entire clang package. Then, after switching to the nightly channel, cargo run gave me this output:
Compiling bevy-crevice-derive v0.7.0
error: Metal API enabled on non-Apple OS. If your project is not using resolver="2" in Cargo.toml, it should.
--> /home/lenerdv/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-hal-0.12.5/src/lib.rs:51:1
|
51 | compile_error!("Metal API enabled on non-Apple OS. If your project is not using resolver=\"2\" in Cargo.toml, it should.");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: DX12 API enabled on non-Windows OS. If your project is not using resolver="2" in Cargo.toml, it should.
--> /home/lenerdv/.cargo/registry/src/github.com-1ecc6299db9ec823/wgpu-hal-0.12.5/src/lib.rs:53:1
|
53 | compile_error!("DX12 API enabled on non-Windows OS. If your project is not using resolver=\"2\" in Cargo.toml, it should.");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
followed by a bunch of cannot find ___ and undeclared crate ___ type errors (hundreds of them), and a final type annotations needed error (full output). It's mainly complaining about mtl, so I'm guessing it might be a missing dependency, but I'm not sure as I'm quite new to rust.

mtl stands for "Metal" which is an Apple API. So your problem isn't a missing dependency but a wrong configuration. This should be fixed by adding resolver="2" in your Cargo.toml as per the compiler suggestion.
For more details, see: https://doc.rust-lang.org/cargo/reference/features.html#feature-resolver-version-2

Related

Why does cargo use a specific dependency version?

When trying to follow the instructions of the pathfinder library, i.e:
cd demo/native
cargo run --release
I get errors due to the compilation of the dependency winit version 0.19.3:
error[E0308]: mismatched types
--> /Users/yairchu/.cargo/registry/src/github.com-1ecc6299db9ec823/winit-0.19.3/src/platform/macos/view.rs:209:9
|
205 | extern fn has_marked_text(this: &Object, _sel: Sel) -> BOOL {
| ---- expected `bool` because of return type
...
209 | (marked_text.length() > 0) as i8
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `bool`, found `i8`
If I try changing the version used to the latest (which works fine for me) by changing Cargo.toml:
--- a/demo/native/Cargo.toml
+++ b/demo/native/Cargo.toml
## -43,7 +43,7 ## rev = "f3df871ac8c3926fe9106d86a3e51e20aa50d3cc"
[dependencies.winit]
-version = "<0.19.4" # 0.19.4 causes build errors https://github.com/rust-windowing/winit/pull/1105
+version = "0.27.2"
I still get the same errors!
Interestingly, I notice this in cargo's output:
Compiling winit v0.19.3
Compiling winit v0.27.2
It appears to now be building both the version I specified and the old version.
I'm lost. Also using --verbose didn't help elucidate why cargo chooses to build this specific dependency.
Is it using two versions of the same library in one executable?
How can I find out why cargo chooses to build this library? (so that I can update it to the working version)
Thanks! Rust noob
How can I find out why cargo chooses to build this library?
cargo tree elaborates on whose dependency is each sub-dependency.
Is it using two versions of the same library in one executable?
It is.
You can depend on different versions of the the same crate. This can be useful if you want to use one version of the dependency, but one of your own dependencies uses another version.
(thanks #Masklinn for the answers in the comments!)

Problems installing Inari library

I am new to Rust, so this could be a stupid mistake, but I can't figure out how to fix it.
The problem
I tried to install Inari (a Rust implementation of interval arithmetic, https://crates.io/crates/inari). But when I try to compile the project, I get some errors:
Compiling inari v1.0.0
error: RUSTFLAGS='-Ctarget-cpu=haswell' or later is required. See https://doc.rust-lang.org/rustc/codegen-options/#target-cpu
--> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/inari-1.0.0/src/simd/x86_64.rs:174:9
|
174 | ... compile_error!("RUSTFLAGS='-Ctarget-cpu=haswell' or later is required. See https://doc.rust-lang.org/rustc/codegen-options/#target-cpu...
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0425]: cannot find function `add_ru` in this scope
--> /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/inari-1.0.0/src/arith.rs:25:21
|
25 | Self { rep: add_ru(x, y) }
| ^^^^^^ help: a function with a similar name exists: `add_rn`
|
...
The error says something about using Haswell, so I tried to add the following lines to Cargo.toml:
[build]
rustflags = ["-Ctarget-cpu=haswell"]
rustdocflags = ["-Ctarget-cpu=haswell"]
but it didn't change anything.
Steps to reproduce
Create a new project with cargo new project-name. Add inari as a dependancy cargo add inari. Run the project with cargo run.
Environment
cargo 1.62.1 (a748cf5a3 2022-06-08)
rustc 1.62.1 (e092d0b6b 2022-07-16)
Ubuntu 20.04.3 LTS (in WSL)
You need to put that in .cargo/config.toml, not in Cargo.toml. Alternatively, set the environment variable RUSTFLAGS before running Cargo.
See Configuration - build.rustflags - The Cargo Book.

Error adding Custom RPCs for custom pallet Substrate

I've been working with parity's contracts node (latest version) and the substrate template node (tag polkadot-v0.9.18), both present the same issue when compiling.
I have a very simple pallet that stores certain items. The main structure is the following:
#[pallet::storage]
#[pallet::getter(fn items)]
/// 'Mapping' Item ID -> Item Data
pub(crate) type Items<T: Config> = StorageMap<_, Twox64Concat, T::Hash, Item<T>>;
I was trying to add a simple RPC method following this guides https://core.tetcoin.org/recipes/custom-rpc.html#rpc-to-call-a-runtime-api and https://core.tetcoin.org/recipes/runtime-api.html
I also checked some projects that already have custom RPC calls implementations, like de subsocial node and I have pretty much the same structure and dependencies.
My rpc method does nothing but return a number 2 just to make sure it works, but it doesn't. This is what the pallets directory looks like:
pallets directory
When I try to compile, the following error shows
error: the wasm32-unknown-unknown target is not supported by default, you may need to
enable the "js" feature. For more information see:
https://docs.rs/getrandom/#webassembly-support
I don't even use that module, but I've read that it is used somewhere as an indirect dependency.
I'm compiling my project with the following command
cargo build --release
Checking the documentation regarding the 'getrandom' crate issue, I added the following dependency in the Cargo.toml (I tried adding it in every Cargo.toml within the project, individually, by pairs, ...)
getrandom = { version = "0.2", features = ["js"] }
Then another error shows up:
error: failed to run custom build command for secp256k1-sys v0.4.1
Which again, doesn't make any sense to me.
The project itself has nothing but the node template base and a new pallet that implements a create and transfer function. Without the RPC implementation, it works perfectly using the Polkadot App, but as soon as I include the custom rpc, it just doesn't compile.
This is my rust configuration (rustup show)
installed toolchains
--------------------
stable-x86_64-apple-darwin (default)
nightly-2021-11-04-x86_64-apple-darwin
nightly-x86_64-apple-darwin
active toolchain
----------------
stable-x86_64-apple-darwin (default)
rustc 1.59.0 (9d1b2106e 2022-02-23)
I haven't found anyone who is dealing with this kind of issue, and I don't know where the problem might be.
This is the first issue logs:
error: the wasm32-unknown-unknown target is not supported by default, you may need to enable the "js" feature. For more information see: https://docs.rs/getrandom/#webassembly-support
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:229:9
|
229 | / compile_error!("the wasm32-unknown-unknown target is not supported by \
230 | | default, you may need to enable the \"js\" feature. \
231 | | For more information see: \
232 | | https://docs.rs/getrandom/#webassembly-support");
| |________________________________________________________________________^
error[E0433]: failed to resolve: use of undeclared crate or module `imp`
--> /home/ubuntu/.cargo/registry/src/github.com-1ecc6299db9ec823/getrandom-0.2.5/src/lib.rs:256:5
|
256 | imp::getrandom_inner(dest)
| ^^^ use of undeclared crate or module `imp`
For more information about this error, try `rustc --explain E0433`.
error: could not compile `getrandom` due to 2 previous errors
warning: build failed, waiting for other jobs to finish...
error: build failed
Current status (to reproduce error): https://github.com/andresvsm/substrate-pallet-rpc/tree/items-branch
Sometimes, you can get this error from a deep dependency of another dependency, e.g. when you really build for a wasm32-unknown-unknown target, and getrandom is linked but even not used. It can be fixed (worked around) with the following trick:
In Cargo.toml, add this line:
[dependencies]
getrandom = {version = "0.2", default-features = false, features = ["custom"]}
It tells the compiler to use a dummy implementation inside of getrandom.
Fixed for me when I added "default features = 'false'" into my Cargo.toml under the dependency in question.

Unable to build rust's Rocket project because of framework dependency error

I'm following this tutorial here and have also looked into Rocket's official guide of setting up a web server.
I've set as default the nightly builds. But I get the following error:
error: failed to select a version for the requirement `ring = "^0.11.0"`
candidate versions found which didn't match: 0.16.11, 0.16.10, 0.16.9, ...
location searched: crates.io index
required by package `cookie v0.9.1`
... which is depended on by `rocket v0.3.6`
... which is depended on by `my-project`
Obviously there's some mismatch with the dependencies, but since ring is something required by the framework itself, I'm not sure how to debug this... furthermore, I'm using the latest versions of cargo and rust:
cargo 1.43.0-nightly (... 2020-02-18)
rustc 1.43.0-nightly (... 2020-02-21)
ring v0.11.0 was yanked from crates.io (see this issue for some background). Since some versions rocket depended on versions of ring that were yanked, those versions of rocket will no longer work.
Upgrading to rocket v0.4.0 (or the latest v0.4.2) should solve this issue.

How to execute cargo test using the nightly channel?

I'm trying to run my tests with nightly Rust using Windows Powershell. I run cargo test in the directory, and I get
Compiling rustcraft v0.1.0 (file:///C:/Users/Phoenix/Desktop/Rust/rustcraft)
error[E0554]: #![feature] may not be used on the stable release channel
--> C:\Users\Phoenix\Desktop\Rust\rustcraft\src\main.rs:1:1
|
1 | #![feature(integer_atomics)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0554]: #![feature] may not be used on the stable release channel
--> C:\Users\Phoenix\Desktop\Rust\rustcraft\src\main.rs:2:1
|
2 | #![feature(collections)]
| ^^^^^^^^^^^^^^^^^^^^^^^^
Obviously, I have to tell Cargo to compile it on the nightly channel, but how? I can't find any reference to specifying a channel in the help section, or any website I've found.
The command line solution may help you to configure your IDE:
cargo +nightly test
Provided, of course, that you have the nightly channel installed. If not, perhaps install it with rustup install nightly (no need to switch to it, but check you're still on stable: rustup show).
The +<toolchain> functionality comes from rustup, the Rust toolchain manager. It works for both cargo +<toolchain> as well as rustc +<toolchain>.
In addition, you can use
rustup run <toolchain> <any arbitrary command goes here>
Since your project requires nightly features, you can change into the directory and run rustup override set <toolchain> to always use the nightly toolchain in that directory.
Create a file called rust-toolchain in your directory containing the name of the toolchain required (e.g. nightly). This has the safe effect as an override, but can be committed to source control.
See also:
Is it possible to have multiple coexisting Rust installations?

Resources