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

idl: Fix potential panic on external type resolution #2954

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 @@ -24,6 +24,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- cli: Use npm's configured default license for new projects made with `anchor init` ([#2929](https://github.com/coral-xyz/anchor/pull/2929)).
- cli: add filename to 'Unable to read keypair file' errors ([#2932](https://github.com/coral-xyz/anchor/pull/2932)).
- idl: Fix path resolution of the `Cargo.lock` of the project when generating idls for external types ([#2946](https://github.com/coral-xyz/anchor/pull/2946)).
- idl: Fix potential panic on external type resolution ([#2954](https://github.com/coral-xyz/anchor/pull/2954)).

### Breaking

Expand Down
19 changes: 8 additions & 11 deletions lang/syn/src/idl/common.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
use std::{
fs,
path::{Path, PathBuf},
};
use std::path::{Path, PathBuf};

use anyhow::Result;
use anyhow::{anyhow, Result};
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

pub fn find_path(name: &str, path: impl AsRef<Path>) -> Result<PathBuf> {
let parent_path = path.as_ref().parent().unwrap();
for entry in fs::read_dir(parent_path)? {
let entry = entry?;
if entry.file_name().to_string_lossy() == name {
return entry.path().canonicalize().map_err(Into::into);
let path = path.as_ref();
for ancestor in path.ancestors() {
let file_path = ancestor.join(name);
if file_path.exists() {
return file_path.canonicalize().map_err(Into::into);
}
}

find_path(name, parent_path)
Err(anyhow!("Path ({path:?}) not found"))
}

pub fn get_no_docs() -> bool {
Expand Down
Loading