Skip to content

Commit ada2ecc

Browse files
committed
add fail fast collision detection
1 parent 8ddb399 commit ada2ecc

1 file changed

Lines changed: 25 additions & 5 deletions

File tree

packages/gapic-generator/gapic/schema/wrappers.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,13 +2326,33 @@ def gen_indirect_resources_used(message):
23262326
)
23272327
)
23282328

2329-
return tuple(
2330-
sorted(
2331-
unique_messages,
2332-
key=lambda m: m.resource_type_full_path or m.name
2333-
)
2329+
# 1. Deterministic AST Sort to avoid potential pipeline flakiness.
2330+
sorted_messages = sorted(
2331+
unique_messages,
2332+
key=lambda m: m.resource_type_full_path or m.name
23342333
)
23352334

2335+
# 2. Fail-fast collision detection
2336+
seen_types = {}
2337+
for msg in sorted_messages:
2338+
res_type = msg.resource_type
2339+
if not res_type:
2340+
continue
2341+
2342+
if res_type in seen_types:
2343+
incumbent = seen_types[res_type]
2344+
raise ValueError(
2345+
f"\n\nFatal: Namespace collision detected for resource type '{res_type}'.\n"
2346+
f"Resources '{incumbent.resource_type_full_path}' and '{msg.resource_type_full_path}' "
2347+
f"both flatten to the exact same method name.\n"
2348+
f"To protect backward compatibility, explicitly alias one of these using "
2349+
f"the `--resource-name-aliases` CLI parameter.\n"
2350+
f"Example: --resource-name-aliases='{{\"{msg.resource_type_full_path}\": \"CustomName\"}}'\n"
2351+
)
2352+
seen_types[res_type] = msg
2353+
2354+
return tuple(sorted_messages)
2355+
23362356
@utils.cached_property
23372357
def resource_messages_dict(self) -> Dict[str, MessageType]:
23382358
"""Returns a dict from resource reference to

0 commit comments

Comments
 (0)