Skip to content
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

export_service! does not respect modules #459

Open
lastmjs opened this issue Aug 8, 2023 · 1 comment
Open

export_service! does not respect modules #459

lastmjs opened this issue Aug 8, 2023 · 1 comment

Comments

@lastmjs
Copy link
Contributor

lastmjs commented Aug 8, 2023

When I run this code:

mod my_module {
    pub struct TestRecord {
        id: String,
    }

    #[ic_cdk_macros::update]
    fn test() -> TestRecord {
        TestRecord {
            id: "hello".to_string(),
        }
    }
}

I get these errors:

error[E0412]: cannot find type `TestRecord` in this scope
    --> src/src/lib.rs:3192:1
     |
3192 | candid::export_service!();
     | ^^^^^^^^^^^^^^^^^^^^^^^^^ not found in this scope
     |
     = note: this error originates in the macro `candid::export_service` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider importing this struct
     |
2    | use crate::my_module::TestRecord;
     |

This is a minimally reproducible example of an issue we're running into in Azle (TypeScript CDK). A simple one-off solution is to add a fully qualified path to TestRecord in the method return type, such as crate::my_module::TestRecord. This is difficult for us to do in a generalized fashion for Azle though.

It would be ideal if the export_service! macro could fully handle methods defined within their own modules inside of the main canister crate.

@chenyan-dfinity
Copy link
Contributor

Is it possible to move the test function to the top-level? We are thinking of only allowing endpoint functions at the top level, or as an impl of a struct.

Defining endpoint functions in a module has security risks: when other people import this module, they suddenly get the same endpoint without notice. So while it's currently possible, it's not a recommended style.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants