8

I couldn't compile ink_env when doing cargo contract build. Tried cargo update by referencing this other [post] (error: duplicate lang item in crate `std` (which `arrayvec` depends on): `panic_impl`). But it still doesn't work.

error: duplicate lang item in crate `std` (which `num_traits` depends on): `oom`.                                                                                             |                                                                                                                                                                           = note: the lang item is first defined in crate `ink_allocator` (which `ink_env` depends on)                                                                                = note: first definition in `ink_allocator` loaded from /home/christopherfkk/phala-network-fat-contract-use-case/target/ink/wasm32-unknown-unknown/release/deps/libink_allocator-20507a21218d777e.rmeta                                                                                                                                                 = note: second definition in `std` loaded from /home/christopherfkk/phala-network-fat-contract-use-case/target/ink/wasm32-unknown-unknown/release/deps/libstd-a0c0252c27199310.rmeta                                                                                                                                                                                                                                                                                                                                              error[E0152]: found duplicate lang item `panic_impl`                                                                                                                          --> /home/christopherfkk/.cargo/registry/src/github.com-1ecc6299db9ec823/ink_env-3.0.0/src/lib.rs:53:1                                                                       |                                                                                                                                                                        53 | fn panic(info: &core::panic::PanicInfo) -> ! {                                                                                                                            | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                                                                                                              |                                                                                                                                                                           = note: the lang item is first defined in crate `std` (which `num_traits` depends on)                                                                                       = note: first definition in `std` loaded from /home/christopherfkk/phala-network-fat-contract-use-case/target/ink/wasm32-unknown-unknown/release/deps/libstd-a0c0252c27199310.rmeta                                                                                                                                                                     = note: second definition in the local crate (`ink_env`)                                                                                                                                                                                                                                                                                             For more information about this error, try `rustc --explain E0152`.                                                                                                         error: could not compile `ink_env` due to 2 previous errors 

4 Answers 4

8

There are two primary troubleshooting steps based off comments and messages surrounding the problem.

  • Ensure all your dependencies are set to the same branch and tag, mismatches are known to be a cause of this problem (source)

  • It is possible that std is leaking into your runtime code when it shouldn't. If your runtime relies on a crate, it should be no-std capable.

    One possible culprit can be serde, a dependency that does have no-std support but can have std enabled by default, triggering the problem. (source).

    This was the cause of the problem in my case and applying the patch in the comment resolved it

It might be a good idea to go through all your dependencies and ensure that

  • they support no-std environments and

  • std is not enabled by default (that's what default-features = false accomplishes) but also that [package]/std is added to the std feature within Cargo.toml

4

If you have a local crate, add this in the lib.rs:

#![cfg_attr(not(feature = "std"), no_std)]

If you use remote crates, add default-features = false, in your Cargo.toml. For example:

imported_crate = { git = "...", default-features = false, branch = "..." }
3

Typically this is an issue with versioning - you must have the same versions from Substrate upstream in your Cargo.toml and Cargo.lock file.

Try cargo tree to investigate the root cause if unclear from the errors you are seeing. Typically resolved by an update cargo update -p <some substrate crate>

Another common cause, that might be applicable here specifically, is "leaking" std in a no-std environment, like a Substrate runtime. In the context of ink this is also critical, as it compiles to Wasm where you likely cannot use std

3

You need to import num_traits with default-features = false in your Cargo.toml file. This will solve the problem. The problem here being that ink itself declares the oom handler, but you also import std through num_traits. std will then also provide the oom handler, which is not allowed and leading to your problem.

2
  • Tried this num-traits = { version = "0.2", default-features = false } but still threw the same error. Mar 28, 2022 at 19:39
  • Maybe you are having some other dependency that pulls in std.
    – bkchr
    Mar 28, 2022 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.