Skip to content

Commit

Permalink
idl: Fix detecting false-positives from doc comments during module pa…
Browse files Browse the repository at this point in the history
…th conversion (#3359)
  • Loading branch information
acheroncrypto authored Nov 12, 2024
1 parent bc3d7ae commit 762ef5e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ The minor version will be incremented upon a breaking change and the patch versi
- avm: Use `rustc 1.79.0` when installing versions older than v0.31 ([#3315](https://github.com/coral-xyz/anchor/pull/3315)).
- cli: Fix priority fee calculation causing panic on localnet ([#3318](https://github.com/coral-xyz/anchor/pull/3318)).
- cli: Fix `shell` command failing due to outdated program initialization ([#3351](https://github.com/coral-xyz/anchor/pull/3351)).
- idl: Fix detecting false-positives from doc comments during module path conversion ([#3359](https://github.com/coral-xyz/anchor/pull/3359)).

### Breaking

Expand Down
8 changes: 5 additions & 3 deletions idl/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,18 +303,20 @@ fn install_toolchain_if_needed(toolchain: &str) -> Result<()> {
/// Convert paths to name if there are no conflicts.
fn convert_module_paths(idl: Idl) -> Idl {
let idl = serde_json::to_string(&idl).unwrap();
let idl = Regex::new(r#""((\w+::)+)(\w+)""#)
let idl = Regex::new(r#""(\w+::)+(\w+)""#)
.unwrap()
.captures_iter(&idl.clone())
.fold(idl, |acc, cur| {
let path = cur.get(0).unwrap().as_str();
let name = cur.get(3).unwrap().as_str();
let name = cur.get(2).unwrap().as_str();

// Replace path with name
let replaced_idl = acc.replace(path, &format!(r#""{name}""#));

// Check whether there is a conflict
let has_conflict = replaced_idl.contains(&format!(r#"::{name}""#));
let has_conflict = Regex::new(&format!(r#""(\w+::)+{name}""#))
.unwrap()
.is_match(&replaced_idl);
if has_conflict {
acc
} else {
Expand Down
9 changes: 9 additions & 0 deletions tests/idl/programs/new-idl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,15 @@ pub mod new_idl {
}
}

/// IDL test for the issue explained in https://github.com/coral-xyz/anchor/issues/3358
///
/// For example, using [`SimpleAccount`] and adding the full path at the end of a doc comment
/// used to result in a false-positive when detecting conflicts.
///
/// [`SimpleAccount`]: crate::SimpleAccount
#[constant]
pub const TEST_CONVERT_MODULE_PATHS: &[u8] = b"convert_module_paths";

#[account]
#[derive(InitSpace)]
pub struct SimpleAccount {
Expand Down

0 comments on commit 762ef5e

Please sign in to comment.