Skip to content

Commit 3ce4a6a

Browse files
authored
cli: Update anchor init (#3958)
* fix(cli): Update default program template to 'multiple' * docs: update
1 parent e0297f3 commit 3ce4a6a

File tree

5 files changed

+105
-14
lines changed

5 files changed

+105
-14
lines changed

cli/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub enum Command {
9292
#[clap(long)]
9393
no_git: bool,
9494
/// Rust program template to use
95-
#[clap(value_enum, short, long, default_value = "single")]
95+
#[clap(value_enum, short, long, default_value = "multiple")]
9696
template: ProgramTemplate,
9797
/// Test template to use
9898
#[clap(value_enum, long, default_value = "mocha")]
@@ -230,7 +230,7 @@ pub enum Command {
230230
/// Program name
231231
name: String,
232232
/// Rust program template to use
233-
#[clap(value_enum, short, long, default_value = "single")]
233+
#[clap(value_enum, short, long, default_value = "multiple")]
234234
template: ProgramTemplate,
235235
/// Create new program even if there is already one
236236
#[clap(long, action)]

cli/src/rust_template.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ const ANCHOR_MSRV: &str = "1.89.0";
2323
/// Program initialization template
2424
#[derive(Clone, Debug, Default, Eq, PartialEq, Parser, ValueEnum)]
2525
pub enum ProgramTemplate {
26-
/// Program with a single `lib.rs` file
27-
#[default]
26+
/// Program with a single `lib.rs` file (not recommended for production)
2827
Single,
29-
/// Program with multiple files for instructions, state...
28+
/// Program with multiple files for instructions, state... (recommended)
29+
#[default]
3030
Multiple,
3131
}
3232

@@ -44,7 +44,10 @@ pub fn create_program(name: &str, template: ProgramTemplate, with_mollusk: bool)
4444
];
4545

4646
let template_files = match template {
47-
ProgramTemplate::Single => create_program_template_single(name, &program_path),
47+
ProgramTemplate::Single => {
48+
println!("Note: Using single-file template. For better code organization and maintainability, consider using --template multiple (default).");
49+
create_program_template_single(name, &program_path)
50+
}
4851
ProgramTemplate::Multiple => create_program_template_multiple(name, &program_path),
4952
};
5053

docs/content/docs/installation.mdx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,16 @@ anchor init my-project
727727
```
728728

729729
This command creates a new directory with the project name and initializes a new
730-
Anchor project with a basic Rust program and TypeScript test template.
730+
Anchor project with a modular Rust program structure and TypeScript test
731+
template.
732+
733+
<Callout type="info">
734+
735+
By default, Anchor uses a **modular structure** with separate files for
736+
instructions, state, constants, and errors. This organization improves code
737+
maintainability and is recommended for production code.
738+
739+
</Callout>
731740

732741
Navigate to the project directory:
733742

docs/content/docs/quickstart/local.mdx

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,73 @@ Navigate to the new project directory and open it in your code editor.
6565
cd my-project
6666
```
6767

68-
The default Anchor program is located at `/programs/my-project/src/lib.rs`.
68+
By default, Anchor generates a modular program structure to promote better code
69+
organization and maintainability. The program files are organized as follows:
70+
71+
- `/programs/my-project/src/lib.rs` - Main entry point with module declarations
72+
- `/programs/my-project/src/instructions/` - Instruction handlers
73+
- `/programs/my-project/src/state/` - Account structures and state
74+
- `/programs/my-project/src/constants.rs` - Program constants
75+
- `/programs/my-project/src/error.rs` - Custom error definitions
6976

7077
<Accordions>
71-
<Accordion title="Default Program">
78+
<Accordion title="Default Program Structure">
7279

7380
The value in the `declare_id!` macro is the program ID, a unique identifier for
7481
your program.
7582

7683
By default, it is the public key of the keypair generated in
7784
`/target/deploy/my_project-keypair.json`.
7885

86+
**Main entry point** (`lib.rs`):
87+
7988
```rust filename="lib.rs"
89+
pub mod constants;
90+
pub mod error;
91+
pub mod instructions;
92+
pub mod state;
93+
8094
use anchor_lang::prelude::*;
8195

96+
pub use constants::*;
97+
pub use instructions::*;
98+
pub use state::*;
99+
82100
declare_id!("3ynNB373Q3VAzKp7m4x238po36hjAGFXFJB4ybN2iTyg");
83101

84102
#[program]
85103
pub mod my_project {
86104
use super::*;
87105

88106
pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
89-
msg!("Greetings from: {:?}", ctx.program_id);
90-
Ok(())
107+
initialize::handler(ctx)
91108
}
92109
}
110+
```
111+
112+
**Instruction handler** (`instructions/initialize.rs`):
113+
114+
```rust filename="instructions/initialize.rs"
115+
use anchor_lang::prelude::*;
93116

94117
#[derive(Accounts)]
95118
pub struct Initialize {}
119+
120+
pub fn handler(ctx: Context<Initialize>) -> Result<()> {
121+
msg!("Greetings from: {:?}", ctx.program_id);
122+
Ok(())
123+
}
96124
```
97125

126+
<Callout type="info">
127+
128+
For simpler projects or quick prototyping, you can use the single-file template
129+
with `anchor init --template single`. However, the modular structure is
130+
recommended for production code as it improves code organization, readability,
131+
and maintainability.
132+
133+
</Callout>
134+
98135
</Accordion>
99136
</Accordions>
100137

@@ -356,9 +393,17 @@ Below is an overview of default file structure in an Anchor workspace:
356393
<Folder name="[project-name]" defaultOpen={true}>
357394
<Folder name="src" defaultOpen={true}>
358395
<File name="lib.rs" />
359-
<File name="Cargo.toml" />
360-
<File name="Xargo.toml" />
396+
<File name="constants.rs" />
397+
<File name="error.rs" />
398+
<Folder name="instructions" defaultOpen={true}>
399+
<File name="mod.rs" />
400+
<File name="initialize.rs" />
401+
</Folder>
402+
<Folder name="state">
403+
<File name="mod.rs" />
404+
</Folder>
361405
</Folder>
406+
<File name="Cargo.toml" />
362407
</Folder>
363408
</Folder>
364409
<Folder name="target" defaultOpen={true}>
@@ -387,6 +432,17 @@ Below is an overview of default file structure in an Anchor workspace:
387432
The `/programs` directory contains your project's Anchor programs. A single
388433
workspace can contain multiple programs.
389434

435+
By default, programs are organized with a modular structure:
436+
437+
- `lib.rs` - Main entry point that declares and exports modules
438+
- `instructions/` - Directory containing instruction handler functions
439+
- `state/` - Directory for account structures and state definitions
440+
- `constants.rs` - Program-wide constants
441+
- `error.rs` - Custom error codes
442+
443+
This modular organization makes it easier to navigate and maintain your code,
444+
especially as your program grows in complexity.
445+
390446
### Tests Folder
391447

392448
The `/tests` directory contains test files for your project. A default test file

docs/content/docs/references/cli.mdx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ Sets a new authority on the IDL account. Both the `new-authority` and
218218
## Init
219219

220220
```shell
221-
anchor init
221+
anchor init <project-name>
222222
```
223223

224224
Initializes a project workspace with the following structure.
@@ -231,6 +231,21 @@ Initializes a project workspace with the following structure.
231231
- `tests/`: Directory for JavaScript integration tests.
232232
- `migrations/deploy.js`: Deploy script.
233233

234+
By default, programs are initialized with a **modular structure** (multiple
235+
files) to promote better code organization. This is the recommended approach for
236+
production code.
237+
238+
**Template Options:**
239+
240+
```shell
241+
anchor init --template multiple # Default: Modular structure (recommended)
242+
anchor init --template single # Single lib.rs file (for prototyping)
243+
```
244+
245+
The modular template organizes code into separate files for instructions, state,
246+
constants, and errors, making it easier to navigate and maintain as your program
247+
grows.
248+
234249
## Keys
235250

236251
Program keypair commands.
@@ -284,6 +299,14 @@ anchor new <program-name>
284299
Creates a new program in the workspace's `programs/` directory initialized with
285300
boilerplate.
286301

302+
By default, uses the **modular structure** template (recommended). You can
303+
specify a different template with the `--template` flag:
304+
305+
```shell
306+
anchor new --template multiple <program-name> # Default: Modular (recommended)
307+
anchor new --template single <program-name> # Single file (for prototyping)
308+
```
309+
287310
## Shell
288311

289312
```shell

0 commit comments

Comments
 (0)