Skip to content

TCGC: Mark types only referenced through external alternate types with External usage in sdkPackage#4236

Merged
iscai-msft merged 7 commits intomainfrom
copilot/fix-external-alternate-type-creation
Apr 20, 2026
Merged

TCGC: Mark types only referenced through external alternate types with External usage in sdkPackage#4236
iscai-msft merged 7 commits intomainfrom
copilot/fix-external-alternate-type-creation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 10, 2026

When a type has @@alternateType with an external type, child types reachable only through that external type (models, unions) still appeared in sdkPackage.models/sdkPackage.unions with full usage flags (Input, Output, Json, etc.). Emitters had no way to distinguish these from types they actually need to generate.

model Geometry { type: string; coordinates: numeric[]; }
model Feature {
  type: "Feature";
  geometry: Geometry | null;
  id?: string | numeric;
}
@@alternateType(Feature, { identity: "geojson::Feature", package: "geojson" }, "rust");

Previously: Geometry and the FeatureId union both appeared in the package with Input | Json usage. Now: they remain in sdkPackage.models/sdkPackage.unions but with only External usage, allowing emitters to distinguish them from types they need to generate.

Changes

  • Block non-External usage on external types at the beginning of updateUsageOrAccess (types.ts): When encountering a type with external set, only UsageFlags.External is allowed to be set and propagated. All other usage (Input, Output, Json, etc.) and access propagation are blocked. The External flag itself still propagates to children so they are marked as reachable from external types.

  • Updated tests (alternate-type.test.ts): Two existing tests updated to assert child types are present in getAllModels() with only External usage. New test added covering the exact Geometry/Feature/union scenario from the issue, verifying child types have External usage but no Input/Output usage.

…s and filter out types only used by external types

Types that are only referenced through external alternate types (via @alternateType with external type info) should not appear in sdkPackage.models, sdkPackage.enums, or sdkPackage.unions.

Changes:
1. In updateUsageOrAccess, stop propagation of non-External usage/access
   past types with external alternate type. This prevents Input/Output/Json
   flags from being set on child types of external types.
2. In filterOutTypes, skip types that have External usage but no Input/Output
   usage, since they are only referenced by external types and don't need
   to be generated.

Agent-Logs-Url: https://github.com/Azure/typespec-azure/sessions/5be8cb07-aab2-4afa-811a-93c2d85e33fb

Co-authored-by: tadelesh <1726438+tadelesh@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the lib:tcgc Issues for @azure-tools/typespec-client-generator-core library label Apr 10, 2026
Copilot AI changed the title [WIP] Fix TCGC to avoid creating types used only in external alternate types TCGC: Exclude types only referenced through external alternate types from sdkPackage Apr 10, 2026
Copilot AI requested a review from tadelesh April 10, 2026 08:12
Copy link
Copy Markdown
Member

@tadelesh tadelesh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Resolve the comments.

Comment thread packages/typespec-client-generator-core/src/types.ts Outdated
Comment thread packages/typespec-client-generator-core/src/types.ts Outdated
Address reviewer feedback:
- Move the external type check before any usage/access is set.
  For external types, only External usage flag can be set; Input/Output/Json
  and access are blocked, and children are never visited.
- Remove the filterOutTypes change since it's now unnecessary:
  children of external types have zero usage and are naturally filtered out.

Agent-Logs-Url: https://github.com/Azure/typespec-azure/sessions/6b9774c6-28b8-4e22-9dee-3cf9a3195d0f

Co-authored-by: tadelesh <1726438+tadelesh@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 13, 2026

@copilot Resolve the comments.

Both comments addressed in 9cefb6f. The external type check is now at the beginning of updateUsageOrAccess (before usage/access is set), and the filterOutTypes change has been removed.

Copilot AI requested a review from tadelesh April 13, 2026 10:57
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Apr 14, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@azure-tools/typespec-client-generator-core@4236

commit: 374f181

Comment thread packages/typespec-client-generator-core/src/types.ts
Allow External usage flag to propagate through children of external
types. Add filter in filterOutTypes to exclude children that only
have External usage (not external types themselves).

Agent-Logs-Url: https://github.com/Azure/typespec-azure/sessions/063f7216-b104-4160-bfd1-8050847f12c8

Co-authored-by: tadelesh <1726438+tadelesh@users.noreply.github.com>
Copilot AI requested a review from tadelesh April 14, 2026 08:31
Comment thread packages/typespec-client-generator-core/src/types.ts Outdated
Copilot AI changed the title TCGC: Exclude types only referenced through external alternate types from sdkPackage TCGC: Mark types only referenced through external alternate types with External usage in sdkPackage Apr 14, 2026
Copilot AI requested a review from tadelesh April 14, 2026 09:22
@tadelesh tadelesh marked this pull request as ready for review April 14, 2026 09:36
@azure-sdk
Copy link
Copy Markdown
Collaborator

All changed packages have been documented.

  • @azure-tools/typespec-client-generator-core
Show changes

@azure-tools/typespec-client-generator-core - fix ✏️

Types that are only used within external alternate types are no longer included in sdkPackage models, enums, or unions.

@azure-sdk
Copy link
Copy Markdown
Collaborator

You can try these changes here

🛝 Playground 🌐 Website

@iscai-msft iscai-msft added this pull request to the merge queue Apr 20, 2026
Merged via the queue into main with commit 71c8dcb Apr 20, 2026
20 checks passed
@iscai-msft iscai-msft deleted the copilot/fix-external-alternate-type-creation branch April 20, 2026 19:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lib:tcgc Issues for @azure-tools/typespec-client-generator-core library

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TCGC should not create types that are only used in external alternate type

4 participants