Skip to main content

discover_programs

Function discover_programs 

Source
pub fn discover_programs(
    workspace_root: &Path,
    current_package: Option<&str>,
) -> Result<BTreeMap<String, PathBuf>>
Expand description

Walk the workspace’s SBF build artifacts and return a base58 program-id → .so path map. Two location families are considered:

  1. target/deploy/cargo-build-sbf’s default output. Always preferred when the same <lib>.so exists in both locations, because this is the post-link form solana-sbpf can parse.
  2. target/sbpf-solana-solana/release/ — the cargo target dir when SBF builds are driven directly (e.g. bench workspaces). Used as a fallback only.

For each chosen .so, every pubkey we can associate with it is added as a key:

  • The pubkey from the sibling <lib>-keypair.json (if present).
  • The pubkey from declare_id!("...") in the crate’s source (if the crate uses anchor’s declare_id! macro).

Mapping all known pubkeys to the same .so matters because cargo-build-sbf generates a fresh random keypair on first build — declare_id! (the runtime id used by Program::id() and seen in traces) won’t match the keypair file unless the user runs anchor keys sync. Without dual-mapping, the trace’s pubkey resolves through the unstripped fallback path and ELF parse fails.

Defensive throughout: missing dirs are not errors (just skipped), 0-byte artifacts are skipped, malformed keypairs / declare_id literals are skipped. Empty result is legal — the caller surfaces a clear “build first?” message rather than failing here.