nervosnetwork / capsule

Capsule is an out-of-box development framework for creating smart contract on Nervos' CKB.
MIT License
60 stars 34 forks source link

Configuration flags not forwarded to rust compiler #132

Closed phroi closed 11 months ago

phroi commented 11 months ago

Configuration flags are not forwarded to rust compiler. To test this I created this repo: phroi/capsule-feature-error. The following test script:

use crate::error::Error;
use core::result::Result;

const RESULT: Result<(), Error> = if cfg!(myfeature) {
    Ok(())
} else {
    panic!("myfeature not found in command line args");
};

pub fn main() -> Result<(), Error> {
    return RESULT;
}

Compiled with the following command:

capsule build --release -- --features myfeature

Or with:

capsule build --release

Results in the same kind error:

$ capsule build --release -- --features myfeature
Building contract capsule-feature-error
RUSTFLAGS=--remap-path-prefix=/home/user=~ --remap-path-prefix=/home/user/capsule-feature-error=
$ cross build -p capsule-feature-error --release --features myfeature
   Compiling capsule-feature-error v0.1.0 (/home/user/capsule-feature-error/contracts/capsule-feature-error)
error[E0080]: evaluation of constant value failed
 --> contracts/capsule-feature-error/src/entry.rs:7:5
  |
7 |     panic!("myfeature not found in command line args");
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'myfeature not found in command line args', contracts/capsule-feature-error/src/entry.rs:7:5
  |
  = note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `panic` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0080`.
error: could not compile `capsule-feature-error` due to previous error
error: command exited with non-zero code `cross build -p capsule-feature-error --release --features myfeature`: 101

Keep up the great work :muscle: Phroi

PS: of course the use of panic is not the issue here. This can be proven by inverting the two linesOk(()) and panic!("myfeature not found in command line args"); and noticing that it compiles just fine.

XuJiandong commented 11 months ago

https://rust-lang.github.io/rfcs/3013-conditional-compilation-checking.html correct version:

#[cfg(feature = "myfeature")] 

or

if cfg!(feature = "myfeature")] {
//...
}
phroi commented 11 months ago

@XuJiandong that's actually works, thanks a lot!!

I may be wrong, but in my opinion the behavior changed since I started developing iCKB :thinking:

With Capsule 0.9.0 it was still generating the correct contract with the syntax cfg!(myfeature)], I know this as I tested all iCKB functionalities on a devnet. Now on Capsule 0.10.1 the same setup would generate a contract for mainnet (which is the default when no feature is specified), so it cannot possibly work on a devnet.