Skip to content

Commit 988fc49

Browse files
cli: Warn if a manifest has solana-program dependency (otter-sec#3250)
1 parent 1ae42e7 commit 988fc49

3 files changed

Lines changed: 33 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ The minor version will be incremented upon a breaking change and the patch versi
4747
- ts: Include unresolved accounts in the resolution error message ([#3207](https://github.com/coral-xyz/anchor/pull/3207)).
4848
- lang: Add `LazyAccount` ([#3194](https://github.com/coral-xyz/anchor/pull/3194)).
4949
- avm: Ask whether to install if the version is not installed with the `use` command ([#3230](https://github.com/coral-xyz/anchor/pull/3230)).
50+
- cli: Warn if a manifest has `solana-program` dependency ([#3250](https://github.com/coral-xyz/anchor/pull/3250)).
5051

5152
### Fixes
5253

cli/src/checks.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,36 @@ pub fn check_anchor_version(cfg: &WithPath<Config>) -> Result<()> {
8181
Ok(())
8282
}
8383

84+
/// Check for potential dependency improvements.
85+
///
86+
/// The main problem people will run into with Solana v2 is that the `solana-program` version
87+
/// specified in users' `Cargo.toml` might be incompatible with `anchor-lang`'s dependency.
88+
/// To fix this and similar problems, users should use the crates exported from `anchor-lang` or
89+
/// `anchor-spl` when possible.
90+
pub fn check_deps(cfg: &WithPath<Config>) -> Result<()> {
91+
// Check `solana-program`
92+
cfg.get_rust_program_list()?
93+
.into_iter()
94+
.map(|path| path.join("Cargo.toml"))
95+
.map(cargo_toml::Manifest::from_path)
96+
.map(|man| man.map_err(|e| anyhow!("Failed to read manifest: {e}")))
97+
.collect::<Result<Vec<_>>>()?
98+
.into_iter()
99+
.filter(|man| man.dependencies.contains_key("solana-program"))
100+
.for_each(|man| {
101+
eprintln!(
102+
"WARNING: Adding `solana-program` as a separate dependency might cause conflicts.\n\
103+
To solve, remove the `solana-program` dependency and use the exported crate from \
104+
`anchor-lang`.\n\
105+
`use solana_program` becomes `use anchor_lang::solana_program`.\n\
106+
Program name: `{}`\n",
107+
man.package().name()
108+
)
109+
});
110+
111+
Ok(())
112+
}
113+
84114
/// Check whether the `idl-build` feature is being used correctly.
85115
///
86116
/// **Note:** The check expects the current directory to be a program directory.

cli/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use anchor_lang::{AccountDeserialize, AnchorDeserialize, AnchorSerialize, Discri
99
use anchor_lang_idl::convert::convert_idl;
1010
use anchor_lang_idl::types::{Idl, IdlArrayLen, IdlDefinedFields, IdlType, IdlTypeDefTy};
1111
use anyhow::{anyhow, Context, Result};
12-
use checks::{check_anchor_version, check_idl_build_feature, check_overflow};
12+
use checks::{check_anchor_version, check_deps, check_idl_build_feature, check_overflow};
1313
use clap::Parser;
1414
use dirs::home_dir;
1515
use flate2::read::GzDecoder;
@@ -1328,6 +1328,7 @@ pub fn build(
13281328

13291329
// Check whether there is a mismatch between CLI and crate/package versions
13301330
check_anchor_version(&cfg).ok();
1331+
check_deps(&cfg).ok();
13311332

13321333
let idl_out = match idl {
13331334
Some(idl) => Some(PathBuf::from(idl)),

0 commit comments

Comments
 (0)