coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

Auto-run `anchor keys sync` or similar on first deployment #3022

Closed jacobcreech closed 2 weeks ago

jacobcreech commented 2 weeks ago

Something that often trips up first time Anchor devs is needing to update the declare_id! after they've deployed the program for the first time. This is automatic within playground, it'd be cool to have this automatic within local development as well.

acheroncrypto commented 2 weeks ago

New projects created with anchor init or anchor new should have the correct program id by default. We can also run anchor keys sync on the first deployment like you've suggested, but the problem is that it could trip out the editor since the file will be overwritten from an outside process.

jacobcreech commented 2 weeks ago

Could you do this as part of first build instead of first deployment? Replace if not already replaced with key that is generated during build?

acheroncrypto commented 2 weeks ago

Adding this in https://github.com/coral-xyz/anchor/pull/3023, but this might cause unwanted behavior in cases where the local tests depend on the program id declared in Anchor.toml. This is also why I didn't add this feature before.

acheroncrypto commented 2 weeks ago

Something that often trips up first time Anchor devs is needing to update the declare_id! after they've deployed the program for the first time. This is automatic within playground, it'd be cool to have this automatic within local development as well.

How do they not have the correct id in the first place? Is it a project from GitHub? If it's not a project initialized by the user, and the program id is declared under [programs.localnet] in Anchor.toml, updating the program id can cause all sorts of issues:

Users only need to update the program's id if they didn't initialize the project, and they are deploying to a non-local cluster.

For example, we don't even use the generated program keypair to run our tests:

https://github.com/coral-xyz/anchor/blob/852809254552c4c31a2d1c30b1c14decb200d586/tests/declare-program/programs/declare-program/src/lib.rs#L3

https://github.com/coral-xyz/anchor/blob/852809254552c4c31a2d1c30b1c14decb200d586/tests/declare-program/Anchor.toml#L1-L2

And it works for everyone without needing to update anything.

If we did the proposed change of updating the ID on the initial build, it would override these declarations and result in uncommitted changes for everyone.