From 762ef5e91cb8824a0dd712082837191cc7ff3b76 Mon Sep 17 00:00:00 2001 From: acheron <98934430+acheroncrypto@users.noreply.github.com> Date: Tue, 12 Nov 2024 23:29:09 +0100 Subject: [PATCH] idl: Fix detecting false-positives from doc comments during module path conversion (#3359) --- CHANGELOG.md | 1 + idl/src/build.rs | 8 +++++--- tests/idl/programs/new-idl/src/lib.rs | 9 +++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 754731e34e..c93f47e583 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/idl/src/build.rs b/idl/src/build.rs index fe706bac8d..d6dd9c2477 100644 --- a/idl/src/build.rs +++ b/idl/src/build.rs @@ -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 { diff --git a/tests/idl/programs/new-idl/src/lib.rs b/tests/idl/programs/new-idl/src/lib.rs index 7f3c9a47ab..90aebb7c23 100644 --- a/tests/idl/programs/new-idl/src/lib.rs +++ b/tests/idl/programs/new-idl/src/lib.rs @@ -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 {