-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
BUG: candid_method macro messes up intellisense and stops it from working #13918
Comments
We don't expand the macro correctly.
mod api {
use crate::CANISTER_DATA;
#[export_name = "canister_update increment_1"]
fn increment_1_0_() {
ic_cdk::setup();
ic_cdk::spawn(async {
let () = ic_cdk::api::call::arg_data();
let result = increment_1();
ic_cdk::api::call::reply((result,))
});
}
fn increment_1() -> u64 {
CANISTER_DATA
.with(|data| {
let mut data = data.borrow_mut();
data.counter += 1;
data.counter
})
}
}
#[export_name = "canister_update increment_1"]
fn increment_1_40_() {
ic_cdk::setup();
ic_cdk::spawn(async {
let () = ic_cdk::api::call::arg_data();
let result = increment_1();
ic_cdk::api::call::reply((result,))
});
}
compile_error! {
"duplicate method name increment_1"
} I suspect we handle the macro's state differently from |
This most likely occurs due to our proc-macro caching, so not really something we can do about I think. |
We expand the macro on every typed character, but the macro stays loaded, so on each expansion it sees a new definition of the function, which causes it to fail with that error. I wonder how well the other crates making use of the same trick work. |
Also, this is very bad for incrementality (using a global counter to generate identifiers). |
IMO this is a bug in the proc macro. I really think we need some official definition of what proc macros are allowed to do, and impure behavior based on global state should not be part of it. |
Thank you for the diagnosis. I've updated the original library author about the discussion here and waiting for their inputs. At this stage, is there a resolution that you would suggest? I understand that the library has impure behaviour that leads to intellisense breaking, but ideally, the extension should also handle such scenarios so as to gracefully fail and not turn off intellisense completely, right? |
We fail gracefully, i.e. don't crash. Do you mean we should ignore the attribute if it fails to expand? That won't work in general, because the macro could take anything as in input, and because our expansion won't be correct anyway, see #11014 (comment). But you can get that effect by setting In addition, there's some non-trivial caching involved, and we can't tell why the macro failed. Maybe it wasn't able to parse something? Maybe it's crashing due to some questionable use of local state? Maybe it crashed because a database server wasn't accessible? Will it crash the same way if executed again? |
Also, the macro doesn't fail. It successfully expands to an expansion with a compile_error and which doesn't contain the original code. That means we just don't have any semantic information for the original code, because it doesn't exist in the macro-expanded code. If the macro expanded to something that still contains the original function body, we would be able to provide completions inside it. |
Anyway, let's close this issue. |
Applying the
#[candid::candid_method]
macro for autogeneration of Candid in a Rust project breaks intellisense on that function. Here's a minimal reproduction:https://github.com/saikatdas0790/ic_cdk_macro_intellisense_test
Steps to reproduce:
increment_1
andincrement_2
functionsHere are some screenshots:
Intellisense shows generic word results here
Intellisense shows relevant results received from the Rust language server
Additional environment details:
Additional context:
I reported this bug to the author of the
candid
library here but they confirmed that it seems to be a problem withrust-analyzer
. However, if you need additional details/context, I can follow up with them to provide more detailsThank you for building this awesome extension 😃
The text was updated successfully, but these errors were encountered: