Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: Add version name from programs Cargo.toml to IDL #1061

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ incremented for features.
### Fixes

* lang: Add `deprecated` attribute to `ProgramAccount` ([#1014](https://github.com/project-serum/anchor/pull/1014)).
* cli: Add version number from programs `Cargo.toml` into extracted IDL

### Features

Expand Down
23 changes: 18 additions & 5 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,21 @@ impl Manifest {
}
}

// Climbs each parent directory until we find a Cargo.toml.
pub fn version(&self) -> String {
match &self.package {
Some(package) => package.version.to_string(),
_ => "0.0.0".to_string(),
}
}

// Climbs each parent directory from the current dir until we find a Cargo.toml
pub fn discover() -> Result<Option<WithPath<Manifest>>> {
let _cwd = std::env::current_dir()?;
let mut cwd_opt = Some(_cwd.as_path());
Manifest::discover_from_path(std::env::current_dir()?)
}

// Climbs each parent directory from a given starting directory until we find a Cargo.toml.
pub fn discover_from_path(start_from: PathBuf) -> Result<Option<WithPath<Manifest>>> {
let mut cwd_opt = Some(start_from.as_path());

while let Some(cwd) = cwd_opt {
for f in fs::read_dir(cwd)? {
Expand Down Expand Up @@ -145,8 +156,10 @@ impl WithPath<Config> {
pub fn read_all_programs(&self) -> Result<Vec<Program>> {
let mut r = vec![];
for path in self.get_program_list()? {
let idl = anchor_syn::idl::file::parse(path.join("src/lib.rs"))?;
let lib_name = Manifest::from_path(&path.join("Cargo.toml"))?.lib_name()?;
let cargo = Manifest::from_path(&path.join("Cargo.toml"))?;
let lib_name = cargo.lib_name()?;
let version = cargo.version();
let idl = anchor_syn::idl::file::parse(path.join("src/lib.rs"), version)?;
r.push(Program {
lib_name,
path,
Expand Down
6 changes: 5 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,11 @@ fn fetch_idl(cfg_override: &ConfigOverride, idl_addr: Pubkey) -> Result<Idl> {

fn extract_idl(file: &str) -> Result<Option<Idl>> {
let file = shellexpand::tilde(file);
anchor_syn::idl::file::parse(&*file)
let manifest_from_path =
std::env::current_dir()?.join(PathBuf::from(&*file).parent().unwrap().to_path_buf());
let cargo = Manifest::discover_from_path(manifest_from_path)?
.ok_or_else(|| anyhow!("Cargo.toml not found"))?;
anchor_syn::idl::file::parse(&*file, cargo.version())
}

fn idl(cfg_override: &ConfigOverride, subcmd: IdlCommand) -> Result<()> {
Expand Down
4 changes: 2 additions & 2 deletions lang/syn/src/idl/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DERIVE_NAME: &str = "Accounts";
const ERROR_CODE_OFFSET: u32 = 300;

// Parse an entire interface file.
pub fn parse(filename: impl AsRef<Path>) -> Result<Option<Idl>> {
pub fn parse(filename: impl AsRef<Path>, version: String) -> Result<Option<Idl>> {
let ctx = CrateContext::parse(filename)?;

let program_mod = match parse_program_mod(&ctx) {
Expand Down Expand Up @@ -224,7 +224,7 @@ pub fn parse(filename: impl AsRef<Path>) -> Result<Option<Idl>> {
}

Ok(Some(Idl {
version: "0.0.0".to_string(),
version,
name: p.name.to_string(),
state,
instructions,
Expand Down