dfinity / dfinity/candid

export_service! does not respect modules

Open
#459 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
301
Forks
85
Avg merge
1h 4m
Merged PRs (30d)
4

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal Rust example and the candid::export_service! entry point shown in the issue, focusing on how module-defined methods and return types are resolved. Done means the macro handles methods inside nested modules without requiring fully qualified return-type paths, while preserving the exported service behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.