Skip to content

Commit 0112a0e

Browse files
committed
lang: Allow references to the program ID in doctests
1 parent 5da4d6a commit 0112a0e

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

lang/attribute/account/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,11 @@ pub fn account(
133133
#[automatically_derived]
134134
impl #impl_gen anchor_lang::Owner for #account_name #type_gen #where_clause {
135135
fn owner() -> Pubkey {
136-
crate::ID
136+
// In a doctest the ID will be in the current scope, not the crate root
137+
#[cfg(not(doctest))]
138+
{ crate::ID }
139+
#[cfg(doctest)]
140+
{ ID }
137141
}
138142
}
139143
}

lang/attribute/event/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,11 @@ pub fn emit_cpi(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
174174
.collect();
175175

176176
let ix = anchor_lang::solana_program::instruction::Instruction::new_with_bytes(
177-
crate::ID,
177+
// In a doctest the ID will be in the current scope, not the crate root
178+
#[cfg(not(doctest))]
179+
{ crate::ID },
180+
#[cfg(doctest)]
181+
{ ID },
178182
&ix_data,
179183
vec![
180184
anchor_lang::solana_program::instruction::AccountMeta::new_readonly(

lang/syn/src/codegen/accounts/mod.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ pub fn generate(accs: &AccountsStruct) -> proc_macro2::TokenStream {
2121
let impl_exit = exit::generate(accs);
2222
let bumps_struct = bumps::generate(accs);
2323

24-
let __client_accounts_mod = __client_accounts::generate(accs, quote!(crate::ID));
25-
let __cpi_client_accounts_mod = __cpi_client_accounts::generate(accs, quote!(crate::ID));
24+
let program_id = quote! {
25+
// In a doctest the ID will be in the current scope, not the crate root
26+
{
27+
#[cfg(not(doctest))]
28+
{ crate::ID }
29+
#[cfg(doctest)]
30+
{ ID }
31+
}
32+
};
33+
34+
let __client_accounts_mod = __client_accounts::generate(accs, program_id.clone());
35+
let __cpi_client_accounts_mod = __cpi_client_accounts::generate(accs, program_id);
2636

2737
let ret = quote! {
2838
#impl_try_accounts

lang/syn/src/codegen/program/idl.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ pub fn idl_accounts_and_functions() -> proc_macro2::TokenStream {
2929
// works. Namespaces are the root of most of the problem.
3030
impl anchor_lang::Owner for IdlAccount {
3131
fn owner() -> Pubkey {
32-
crate::ID
32+
// In a doctest the ID will be in the current scope, not the crate root
33+
#[cfg(not(doctest))]
34+
{ crate::ID }
35+
#[cfg(doctest)]
36+
{ ID }
3337
}
3438
}
3539

0 commit comments

Comments
 (0)