Rust error on pre_upgrade and post_upgrade attributes


as seen in the screenshot copy and pasting (and a little editing) of cdk-rs/ at main · dfinity/cdk-rs · GitHub

I get a Post-upgrade function already declared error, everything works like it should but I really want to resolve this.

I tried renaming the fn pre_upgrade to fn pre_upgrade_something but it keeps the error.

What is causing the issue? (i’m new to Rust so maybe it’s just a beginner problem)

Thanks is advance

1 Like


Could you please clarify what you mean by that? Does the canister build when you run cargo?

My guess that it might be purely an IDE problem. I wonder if replacing #[pre_upgrade] with #[export_name = "canister_pre_upgrade"] and #[post_upgrade] with #[export_name = "canister_post_upgrade"] could help your IDE figure out what’s going on.

1 Like

:wave: @rom

What i mean by that is, that i can deploy the canister successfully and it works like it should.

When running a cargo build --verbose command, it does not build, i thought it was maybe due to running the ic crates locally or something.

i get the following error;

error: aborting due to previous error; 7 warnings emitted

error: could not compile `user_controller`

Caused by:
  process didn't exit successfully: `rustc --crate-name user_controller --edition=2018 src/backend/rust/user_controller/src/ --error-format=json --json=diagnostic-rendered-ansi --crate-type cdylib --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=309fc0f94d246419 --out-dir /home/rmcs/Documents/app/target/debug/deps -C incremental=/home/rmcs/Documents/app/target/debug/incremental -L dependency=/home/rmcs/Documents/app/target/debug/deps --extern
ic_cdk=/home/rmcs/Documents/app/target/debug/deps/libic_cdk-fbc39ac7a95afa8e.rlib --extern
ic_cdk_macros=/home/rmcs/Documents/app/target/debug/deps/libic_cdk_macros- --extern ic_types=/home/rmcs/Documents/app/target/debug/deps/libic_types-
319e16659f2698fe.rlib --extern serde=/home/rmcs/Documents/app/target/debug/deps/libserde-
65de3c84c101c7a1.rlib` (exit status: 1)

The #[export_name = "canister_post_upgrade"] fixed the issue! Thanks! Is there some documentation / github reference on this issue?

@roman-kashitsyn in addition to the earlier post
When i try to build the rust_hello from the dfinity tutorials project it also failed.

I believe this command tries to build a native binary from your canister code, which is not possible at the moment. Specifying the build target explicitly might resolve the issue:

cargo build --release --target=wasm32-unknown-unknown

If you use rustup, I also find it useful to add a toolchain file to the project (rust-toolchain.toml file living right next to Cargo.toml), this is how mine looks like:

channel = "1.51.0"
targets = ["wasm32-unknown-unknown"]

This will tell cargo to use the right rustc version and target by default.

Is there some documentation / github reference on this issue?

I’m not sure it’s an issue of the Rust CDK, because as you mentioned, the build works fine. For some reason your IDE becomes confused about the code. Unfortunately, I don’t know much about IDEs, maybe some VSCode users could help you understanding the issue better.

1 Like

The cargo build --release --target=wasm32-unknown-unknown works to build! Only need to figure out how to use the toolchain file.

Regarding the reference, the IC internet spec states the different options The Internet Computer Interface Specification :: Internet Computer

Thanks for your help! :pray:

1 Like

Just ran into this myself, (also fairly new to Rust).

Turns out the Rust for Visual Studio Code extension is no longer actively maintained, despite having the highest number of installs.

The one called rust-analyzer seems to be the unofficial successor, it’s much more up-to-date and doesn’t throw false errors like this one.

Just putting this here so other rust newbies don’t get tripped by this :slight_smile: