You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The declare_program! macro is panicking with the error "Instruction must exist" when used with a program that has no direct instructions but uses shared contexts across multiple instructions.
Steps to Reproduce
Create a Solana program using Anchor with the following structure:
use anchor_lang::prelude::*;declare_id!("7VMBwUQPsKb1G41qusehNJZ5vYQrFNg8pdw2ViaApm4Y");#[program]pubmod composite {usesuper::*;pubfninitialize_state(_ctx:Context<Initialize>) -> Result<()>{Ok(())}// Note: This instruction is removed in the problematic scenario// pub fn empty(_ctx: Context<InnerContext>) -> Result<()> {// Ok(())// }}#[derive(Accounts)]pubstructInitialize<'info>{/// CHECK:pubsome_acc:AccountInfo<'info>,pubinner_ctx:InnerContext<'info>,}#[derive(Accounts)]pubstructInnerContext<'info>{/// CHECK:pubinner_acc:AccountInfo<'info>,}
Use the declare_program! macro in another crate:
declare_program!(composite);
Attempt to build the project.
Expected Behavior
The declare_program! macro should successfully generate the necessary code for interacting with the program, recognizing the shared context structure.
In real-world scenarios, it's common to use contexts with a subset of accounts and share them between many instructions. This allows for more modular and reusable code. However, this can lead to situations where a context is not used directly in any instructions within a specific program module.
Possible Cause
The declare_program! macro is checking for the existence of instructions that directly use the defined contexts, rather than recognizing the shared context structure.
the root of the problem is here
Additional Information
Anchor version: 0.30.0, 0.30.1
Proposed Solution
The declare_program! macro should be updated to recognize and handle programs that use shared contexts without direct instructions. It should generate the necessary code based on the context structures, even when they're not directly used in instructions within the current program module.
Workaround
Currently, adding a dummy instruction that uses the shared context allows the macro to work:
Thanks for creating the issue and giving a comprehensive explanation of the problem. Your assessment is correct, and this has been on my todo list for a while, but I haven't been able to get to it just yet.
Description
The
declare_program!
macro is panicking with the error "Instruction must exist" when used with a program that has no direct instructions but uses shared contexts across multiple instructions.Steps to Reproduce
declare_program!
macro in another crate:Expected Behavior
The
declare_program!
macro should successfully generate the necessary code for interacting with the program, recognizing the shared context structure.Actual Behavior
The macro panics with the following error:
Context
In real-world scenarios, it's common to use contexts with a subset of accounts and share them between many instructions. This allows for more modular and reusable code. However, this can lead to situations where a context is not used directly in any instructions within a specific program module.
Possible Cause
The
declare_program!
macro is checking for the existence of instructions that directly use the defined contexts, rather than recognizing the shared context structure.the root of the problem is here
Additional Information
Proposed Solution
The
declare_program!
macro should be updated to recognize and handle programs that use shared contexts without direct instructions. It should generate the necessary code based on the context structures, even when they're not directly used in instructions within the current program module.Workaround
Currently, adding a dummy instruction that uses the shared context allows the macro to work:
However, this is not ideal for production code.
The text was updated successfully, but these errors were encountered: