Skip to content

Commit 755692a

Browse files
lang: Don't make anchor_lang implicitly mandatory as an extra dependency when using declare_program! via anchor_client (solana-foundation#4157)
1 parent a1af193 commit 755692a

File tree

5 files changed

+19
-0
lines changed

5 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi
1616
- cli: Added a `check_program_id_mismatch` in build time to check if the program ID in the source code matches the program ID in the keypair file ([#4018](https://github.com/solana-foundation/anchor/pull/4018)). This check will be skipped during `anchor test`.
1717
- lang: Add instruction parser to `declare_program!` ([#4118](https://github.com/solana-foundation/anchor/pull/4118)).
1818
- ts: Export all IDL types from the root. Users can now update `dist/cjs/idl` imports to import directly from `@anchor-lang/core` ([#3948](https://github.com/solana-foundation/anchor/pull/3948)).
19+
- lang: Add `declare_program!` support with just `anchor_client` and not `anchor_lang` ([#4157](https://github.com/solana-foundation/anchor/pull/4157)).
1920

2021
### Fixes
2122

lang/attribute/program/src/declare_program/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ fn gen_program(idl: &Idl, name: &syn::Ident) -> proc_macro2::TokenStream {
7474
quote! {
7575
#docs
7676
pub mod #name {
77+
#[cfg(any(target_os = "solana", feature = "idl-build"))]
78+
use ::anchor_lang;
79+
#[cfg(all(not(target_os = "solana"), not(feature = "idl-build")))]
80+
use super::anchor_lang;
81+
7782
use anchor_lang::prelude::*;
7883
use accounts::*;
7984
use events::*;

lang/attribute/program/src/declare_program/mods/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn gen_errors_mod(idl: &Idl) -> proc_macro2::TokenStream {
1515
/// Program error type definitions.
1616
#[cfg(not(feature = "idl-build"))]
1717
pub mod errors {
18+
use super::*;
1819
}
1920
};
2021
}
@@ -23,6 +24,7 @@ pub fn gen_errors_mod(idl: &Idl) -> proc_macro2::TokenStream {
2324
/// Program error type definitions.
2425
#[cfg(not(feature = "idl-build"))]
2526
pub mod errors {
27+
use super::*;
2628

2729
#[anchor_lang::error_code(offset = 0)]
2830
pub enum ProgramError {

lang/attribute/program/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ pub fn program(
5858
/// definition, you should consider creating a separate crate for the external program definition
5959
/// and reusing it in your programs.
6060
///
61+
/// # Off-chain usage
62+
///
63+
/// When using in off-chain environments, for example via `anchor_client`, you must have
64+
/// `anchor_lang` in scope:
65+
///
66+
/// ```ignore
67+
/// use anchor_client::anchor_lang;
68+
/// ```
69+
///
6170
/// # Example
6271
///
6372
/// A full on-chain CPI usage example can be found [here].

lang/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ pub mod prelude {
498498
InitSpace, Key, Lamports, Owner, ProgramData, Result, Space, ToAccountInfo, ToAccountInfos,
499499
ToAccountMetas,
500500
};
501+
// Re-export the crate as anchor_lang for declare_program! macro
502+
pub use crate as anchor_lang;
501503
pub use crate::solana_program::account_info::{next_account_info, AccountInfo};
502504
pub use crate::solana_program::instruction::AccountMeta;
503505
pub use crate::solana_program::program_error::ProgramError;

0 commit comments

Comments
 (0)