Skip to content

Commit

Permalink
cli: Make conflicting account names a compile-time error (#2621)
Browse files Browse the repository at this point in the history
  • Loading branch information
acheroncrypto authored Sep 5, 2023
1 parent b9fa898 commit a1e4453
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
16 changes: 16 additions & 0 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2501,6 +2501,22 @@ fn generate_idl_build(no_docs: bool) -> Result<Vec<Idl>> {
})
.collect::<Vec<_>>();

// Verify IDLs are valid
for idl in &idls {
let full_path_account = idl
.accounts
.iter()
.find(|account| account.name.contains("::"));

if let Some(account) = full_path_account {
return Err(anyhow!(
"Conflicting accounts names are not allowed.\nProgram: {}\nAccount: {}",
idl.name,
account.name
));
}
}

Ok(idls)
}

Expand Down
1 change: 1 addition & 0 deletions tests/idl/Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs = "Docs111111111111111111111111111111111111111"
external = "Externa1111111111111111111111111111111111111"
generics = "Generics111111111111111111111111111111111111"
idl = "id11111111111111111111111111111111111111111"
idl_build_features = "id1Bui1dFeatures111111111111111111111111111"
relations_derivation = "Re1ationsDerivation111111111111111111111111"
non_existent = { address = "NonExistent11111111111111111111111111111111", idl = "non-existent.json" }
numbers_123 = { address = "Numbers111111111111111111111111111111111111", idl = "idls/relations_build.json" }
Expand Down
20 changes: 20 additions & 0 deletions tests/idl/programs/idl-build-features/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "idl-build-features"
version = "0.1.0"
description = "Created with Anchor"
rust-version = "1.60"
edition = "2021"

[lib]
crate-type = ["cdylib", "lib"]
name = "idl_build_features"

[features]
no-entrypoint = []
no-idl = []
cpi = ["no-entrypoint"]
idl-build = ["anchor-lang/idl-build"]
default = []

[dependencies]
anchor-lang = { path = "../../../../lang" }
2 changes: 2 additions & 0 deletions tests/idl/programs/idl-build-features/Xargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[target.bpfel-unknown-unknown.dependencies.std]
features = []
47 changes: 47 additions & 0 deletions tests/idl/programs/idl-build-features/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use anchor_lang::prelude::*;

declare_id!("id1Bui1dFeatures111111111111111111111111111");

#[program]
pub mod idl_build_features {
use super::*;

pub fn full_path(
ctx: Context<FullPath>,
my_struct: MyStruct,
some_module_my_struct: some_module::MyStruct,
) -> Result<()> {
ctx.accounts.account.my_struct = my_struct;
ctx.accounts.account.some_module_my_struct = some_module_my_struct;
Ok(())
}
}

#[derive(Accounts)]
pub struct FullPath<'info> {
#[account(zero)]
pub account: Account<'info, FullPathAccount>,
}

#[account]
pub struct FullPathAccount {
pub my_struct: MyStruct,
pub some_module_my_struct: some_module::MyStruct,
}

mod some_module {
use super::*;

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct MyStruct {
pub data: u8,
}
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone)]
pub struct MyStruct {
pub u8: u8,
pub u16: u16,
pub u32: u32,
pub u64: u64,
}
35 changes: 35 additions & 0 deletions tests/idl/tests/idl-build-features.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import * as anchor from "@coral-xyz/anchor";
import { assert } from "chai";

import { IdlBuildFeatures } from "../target/types/idl_build_features";

describe("idl-build features", () => {
anchor.setProvider(anchor.AnchorProvider.env());
const program = anchor.workspace
.idlBuildFeatures as anchor.Program<IdlBuildFeatures>;

it("Can use full module path types", async () => {
const kp = anchor.web3.Keypair.generate();

const outerMyStructArg = { u8: 1, u16: 2, u32: 3, u64: new anchor.BN(4) };
const someModuleMyStructArg = { data: 5 };

await program.methods
.fullPath(outerMyStructArg, someModuleMyStructArg)
.accounts({ account: kp.publicKey })
.preInstructions([
await program.account.fullPathAccount.createInstruction(kp),
])
.signers([kp])
.rpc();

const fullPathAccount = await program.account.fullPathAccount.fetch(
kp.publicKey
);
assert.strictEqual(fullPathAccount.myStruct.u8, outerMyStructArg.u8);
assert.strictEqual(fullPathAccount.myStruct.u16, outerMyStructArg.u16);
assert.strictEqual(fullPathAccount.myStruct.u32, outerMyStructArg.u32);
assert(fullPathAccount.myStruct.u64.eq(outerMyStructArg.u64));
assert.deepEqual(fullPathAccount.someModuleMyStruct, someModuleMyStructArg);
});
});

1 comment on commit a1e4453

@vercel
Copy link

@vercel vercel bot commented on a1e4453 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

anchor-docs – ./

anchor-docs-git-master-200ms.vercel.app
anchor-docs-200ms.vercel.app
www.anchor-lang.com
anchor-lang.com

Please sign in to comment.