Skip to content

Invalidate the full constructor with the constructor cache - #11745

Open
JoshLove-msft wants to merge 1 commit into
microsoft:mainfrom
JoshLove-msft:josh/reset-full-constructor-on-cache-invalidation
Open

Invalidate the full constructor with the constructor cache#11745
JoshLove-msft wants to merge 1 commit into
microsoft:mainfrom
JoshLove-msft:josh/reset-full-constructor-on-cache-invalidation

Conversation

@JoshLove-msft

Copy link
Copy Markdown
Contributor

Summary

  • centralize constructor-cache invalidation in a private protected hook
  • clear ModelProvider.FullConstructor whenever the constructor list is invalidated
  • cover same-value identity updates that still rebuild customization and declaration metadata

Context

Follow-up to #11737. That fix keys FullConstructor only on name and namespace, but Update(name: provider.Name) still invalidates Constructors while preserving that identity. Derived providers that mutate FullConstructor during BuildConstructors then apply those mutations repeatedly. This is exercised downstream by Azure/azure-sdk-for-net#61958 while fixing Azure/azure-sdk-for-net#61851.

Validation

  • Microsoft.TypeSpec.Generator ModelProviderTests (166 passed)
  • targeted constructor cache tests (2 passed)

Ensure ModelProvider clears its cached FullConstructor whenever TypeProvider invalidates the constructor list, including same-value identity updates.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI lite review requested due to automatic review settings August 22, 2026 18:19
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Aug 22, 2026
@pkg-pr-new

pkg-pr-new Bot commented Aug 22, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-csharp@11745

commit: 5526d4c

@github-actions

Copy link
Copy Markdown
Contributor

No changes needing a change description found.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR refines constructor caching in the C# generator’s provider layer by centralizing constructor-cache invalidation and ensuring ModelProvider.FullConstructor is rebuilt whenever the constructor list is invalidated (including “same identity” updates). This prevents repeated application of constructor mutations when providers rebuild constructors without changing name/namespace.

Changes:

  • Introduces a constructor-reset hook in TypeProvider and routes constructor invalidation through it.
  • Clears ModelProvider’s cached FullConstructor whenever constructors are reset.
  • Extends regression coverage to include same-value identity updates that still force constructor rebuilds.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/TypeProvider.cs Centralizes constructor invalidation through a dedicated reset hook used by Reset() and identity-based updates.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs Hooks constructor invalidation to also clear the cached FullConstructor, simplifying FullConstructor caching logic.
packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ModelProviderTests.cs Adds regression assertions for same-identity Update calls to ensure FullConstructor is rebuilt and mutations aren’t duplicated.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@JoshLove-msft

Copy link
Copy Markdown
Contributor Author

Validated this fix against the downstream management generator path from Azure/azure-sdk-for-net#61958:

  • removed the management-local constructor cache workaround
  • replaced the runtime Microsoft.TypeSpec.Generator.dll with this branch's build
  • passed all 42 DynamicModelConstructorTests and FlattenPropertyVisitorTests
  • regenerated the Mgmt-TypeSpec project through the actual emitter/generator pipeline
  • regeneration produced no output diff, with exactly one _patch = patch; assignment per dynamic model constructor

Restoring the published 20260821.2 core generator reproduces the duplicate assignments, propagator calls, and pragmas.

--generated by Copilot

@JoshLove-msft

Copy link
Copy Markdown
Contributor Author

Jorge Rangel (@jorgerangel-msft) this is the follow-up to #11737 for the same-value identity update path that the identity tuple does not catch. Full CI is green, and I validated it end-to-end against Azure/azure-sdk-for-net#61958 with the local workaround removed: 42 focused tests pass and regeneration is unchanged. When you have a chance, could you review this follow-up?

--generated by Copilot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants