De-dup types early in CLI/macro so that derives/substitutes work for de-duped types (#2015)

* De-dup types early to enable derives for them

* ensure_unique_type_paths in CLI codegen, too

* Update UI test outpuot

---------

Co-authored-by: José Duarte <duarte.gmj@gmail.com>
This commit is contained in:
James Wilson
2025-06-03 12:03:55 +01:00
committed by GitHub
parent a4331dfa08
commit f831e8f87c
5 changed files with 26 additions and 5 deletions
Generated
+1
View File
@@ -5602,6 +5602,7 @@ dependencies = [
"pretty_assertions",
"quote",
"scale-info",
"scale-typegen",
"scale-typegen-description",
"scale-value",
"serde",
+1
View File
@@ -27,6 +27,7 @@ chain-spec-pruning = ["smoldot"]
[dependencies]
subxt-codegen = { workspace = true }
scale-typegen = { workspace = true }
subxt-utils-fetchmetadata = { workspace = true, features = ["url"] }
subxt-utils-stripmetadata = { workspace = true }
subxt-metadata = { workspace = true }
+12 -2
View File
@@ -210,8 +210,18 @@ fn codegen(
codegen.no_docs()
}
let metadata = subxt_metadata::Metadata::decode(&mut &*metadata_bytes)
.map_err(|e| eyre!("Cannot decode the provided metadata: {e}"))?;
let metadata = {
let mut metadata = subxt_metadata::Metadata::decode(&mut &*metadata_bytes)
.map_err(|e| eyre!("Cannot decode the provided metadata: {e}"))?;
// Run this first to ensure type paths are unique (which may result in 1,2,3 suffixes being added
// to type paths), so that when we validate derives/substitutions below, they are allowed for such
// types. See <https://github.com/paritytech/subxt/issues/2011>.
scale_typegen::utils::ensure_unique_type_paths(metadata.types_mut())
.expect("ensure_unique_type_paths should not fail; please report an issue.");
metadata
};
// Configure derives:
let global_derives = raw_derives
+12 -1
View File
@@ -102,7 +102,17 @@ fn subxt_inner(args: TokenStream, item_mod: syn::ItemMod) -> Result<TokenStream,
.map_err(|e| TokenStream::from(e.write_errors()))?;
// Fetch metadata first, because we need it to validate some of the chosen codegen options.
let metadata = fetch_metadata(&args)?;
let metadata = {
let mut metadata = fetch_metadata(&args)?;
// Run this first to ensure type paths are unique (which may result in 1,2,3 suffixes being added
// to type paths), so that when we validate derives/substitutions below, they are allowed for such
// types. See <https://github.com/paritytech/subxt/issues/2011>.
scale_typegen::utils::ensure_unique_type_paths(metadata.types_mut())
.expect("ensure_unique_type_paths should not fail; please report an issue.");
metadata
};
let mut codegen = CodegenBuilder::new();
@@ -135,6 +145,7 @@ fn subxt_inner(args: TokenStream, item_mod: syn::ItemMod) -> Result<TokenStream,
.into_iter()
.collect(),
);
for d in args.derive_for_type {
validate_type_path(&d.path.path, &metadata);
codegen.add_derives_for_type(d.path, d.derive.into_iter(), d.recursive);
@@ -11,9 +11,7 @@ error: Type `Event` does not exist at path `sp_runtime::multiaddress::Event`
pallet_grandpa::pallet::Event
pallet_treasury::pallet::Event
pallet_conviction_voting::pallet::Event
pallet_referenda::pallet::Event
pallet_ranked_collective::pallet::Event
pallet_referenda::pallet::Event
pallet_whitelist::pallet::Event
polkadot_runtime_common::claims::pallet::Event
pallet_utility::pallet::Event