Skip to content

Commit f0a1662

Browse files
committed
feat: implement pre-flight checks for library validation in build process
1 parent 63f919d commit f0a1662

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

src/main.rs

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use solana_sdk::pubkey::Pubkey;
1818
use solana_transaction_status_client_types::UiTransactionEncoding;
1919
use std::{
2020
io::Read,
21-
path::PathBuf,
21+
path::{Path, PathBuf},
2222
process::{Command, Output, Stdio},
2323
sync::{
2424
atomic::{AtomicBool, Ordering},
@@ -42,6 +42,7 @@ use crate::solana_program::{
4242
};
4343

4444
const MAINNET_GENESIS_HASH: &str = "5eykt4UsFv8P8NJdTREpY1vzqKqZKvdpKuc147dw2N9d";
45+
const VERIFIED_BUILDS_DOC_URL: &str = "https://solana.com/docs/programs/verified-builds";
4546

4647
pub fn get_network(network_str: &str) -> &str {
4748
match network_str {
@@ -1212,6 +1213,82 @@ fn build_args(
12121213
Ok((args, mount_path.to_str().unwrap().to_string(), library_name))
12131214
}
12141215

1216+
fn run_preflight_checks(
1217+
repo_root: &str,
1218+
relative_mount_path: &str,
1219+
mount_path: &str,
1220+
library_name: &str,
1221+
) -> anyhow::Result<()> {
1222+
println!("Running pre-flight validation...");
1223+
let mount_path_buf = Path::new(mount_path);
1224+
ensure!(
1225+
mount_path_buf.exists(),
1226+
"Pre-flight check failed: mount path '{}' does not exist. Pass --mount-path relative to the repository root ({}) or run from the workspace root described in {}.",
1227+
if relative_mount_path.is_empty() {
1228+
"."
1229+
} else {
1230+
relative_mount_path
1231+
},
1232+
repo_root,
1233+
VERIFIED_BUILDS_DOC_URL
1234+
);
1235+
1236+
let manifest_path = mount_path_buf.join("Cargo.toml");
1237+
ensure!(
1238+
manifest_path.exists(),
1239+
"Pre-flight check failed: missing Cargo.toml at {}. Ensure --mount-path points at your program workspace per {}.",
1240+
manifest_path.display(),
1241+
VERIFIED_BUILDS_DOC_URL
1242+
);
1243+
1244+
let lock_path = mount_path_buf.join("Cargo.lock");
1245+
ensure!(
1246+
lock_path.exists(),
1247+
"Pre-flight check failed: missing Cargo.lock at {}. Run `cargo generate-lockfile` in the workspace root (see {}).",
1248+
lock_path.display(),
1249+
VERIFIED_BUILDS_DOC_URL
1250+
);
1251+
1252+
let manifest = Manifest::from_path(&manifest_path)?;
1253+
let lib = manifest.lib.as_ref().ok_or_else(|| {
1254+
anyhow!(
1255+
"Pre-flight check failed: `[lib]` section not found in {}. Configure it as documented in {}.",
1256+
manifest_path.display(),
1257+
VERIFIED_BUILDS_DOC_URL
1258+
)
1259+
})?;
1260+
1261+
let configured_name = lib.name.as_ref().ok_or_else(|| {
1262+
anyhow!(
1263+
"Pre-flight check failed: `[lib] name` missing in {}. Set it per {}.",
1264+
manifest_path.display(),
1265+
VERIFIED_BUILDS_DOC_URL
1266+
)
1267+
})?;
1268+
1269+
ensure!(
1270+
configured_name == library_name,
1271+
"Pre-flight check failed: `[lib] name = \"{configured_name}\"` but the CLI resolved `{library_name}`. Update --library-name or your Cargo.toml."
1272+
);
1273+
1274+
if lib.crate_type.is_empty() {
1275+
println!(
1276+
"Warning: `[lib] crate-type` is not set in {}. Add `cdylib` per {}.",
1277+
manifest_path.display(),
1278+
VERIFIED_BUILDS_DOC_URL
1279+
);
1280+
} else {
1281+
ensure!(
1282+
lib.crate_type.iter().any(|t| t == "cdylib"),
1283+
"Pre-flight check failed: `[lib] crate-type` must include \"cdylib\" so Solana produces a deployable .so (see {}).",
1284+
VERIFIED_BUILDS_DOC_URL
1285+
);
1286+
}
1287+
1288+
println!("Pre-flight checks passed ✅");
1289+
Ok(())
1290+
}
1291+
12151292
fn clone_repo_and_checkout(
12161293
repo_url: &str,
12171294
current_dir: bool,
@@ -1340,6 +1417,12 @@ pub async fn verify_from_repo(
13401417
)?;
13411418
println!("Build path: {mount_path:?}");
13421419
println!("Verifying program: {library_name}");
1420+
run_preflight_checks(
1421+
&verify_tmp_root_path,
1422+
&relative_mount_path,
1423+
&mount_path,
1424+
&library_name,
1425+
)?;
13431426

13441427
check_signal(container_id_opt, temp_dir_opt);
13451428

0 commit comments

Comments
 (0)