From 1902b8e5868d65e4d5b8dd351192a6936d4d6130 Mon Sep 17 00:00:00 2001 From: CanardMandarin Date: Mon, 5 Jun 2023 14:16:10 +0200 Subject: [PATCH] cli: Update programs in `Anchor.toml` when using `anchor new` (#2516) --- CHANGELOG.md | 1 + cli/src/lib.rs | 28 ++++++++++++++++++++++++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index eb033dd1e0..4fd4560e20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,7 @@ The minor version will be incremented upon a breaking change and the patch versi - lang: Fix inability to use identifiers `program_id`, `accounts`, `ix_data`, `remaining_accounts` in instruction arguments ([#2464](https://github.com/coral-xyz/anchor/pull/2464)) - cli: Fix incorrect `metadata.address` generation in IDL after deploying with a custom keypair ([#2485](https://github.com/coral-xyz/anchor/pull/2485)) - cli: IDL commands no longer hang when the payer doesn't have funds to pay for the transaction fee ([#2492](https://github.com/coral-xyz/anchor/pull/2492)) +- cli: Fix `anchor new` not updating `Anchor.toml` ([#2516](https://github.com/coral-xyz/anchor/pull/2516)). ### Breaking diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 1f2d33cc2a..41ec676838 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -776,11 +776,31 @@ fn new(cfg_override: &ConfigOverride, solidity: bool, name: String) -> Result<() } Some(parent) => { std::env::set_current_dir(parent)?; - if solidity { - new_solidity_program(&name)?; - } else { - new_rust_program(&name)?; + + let cluster = cfg.provider.cluster.clone(); + let programs = cfg.programs.entry(cluster).or_insert(BTreeMap::new()); + if programs.contains_key(&name) { + return Err(anyhow!("Program already exists")); } + + programs.insert( + name.clone(), + ProgramDeployment { + address: if solidity { + new_solidity_program(&name)?; + solidity_template::default_program_id() + } else { + new_rust_program(&name)?; + rust_template::get_or_create_program_id(&name) + }, + path: None, + idl: None, + }, + ); + + let toml = cfg.to_string(); + fs::write("Anchor.toml", toml)?; + println!("Created new program."); } };