Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Namespace Duplication Fix - Complete

Summary

Fixed a bug where using @clientNamespace decorator with the same value as the TCGC namespace option causes incorrect namespace concatenation.

Minimal Fix (2 checks)

  • Check 1 in getClientNamespace(): Return early if override equals namespace flag - handles client namespace
  • Check 2 in getNamespaceFullNameWithOverride(): Return early if namespace flag extends user namespace AND segments start with flag - handles models/types in same or nested namespaces

Changes Made

  • Fixed getClientNamespace() function to check if override matches namespace flag before replacement
  • Fixed getNamespaceFullNameWithOverride() with precise duplication detection that only skips replacement when the flag is an extension of the user namespace
  • Added comprehensive test coverage for exact match, partial overlap, and nested namespace scenarios
  • All 1111 existing tests continue to pass
  • Code review completed - no issues found
  • Security scan completed - no vulnerabilities detected
  • Code formatting fixed with prettier
  • Cleaned git history - removed empty "Initial plan" commit, now contains exactly 7 meaningful commits

The Problem

When a spec defines:

  • @clientNamespace("Azure.Search.Documents")
  • And TCGC option: namespace: "Azure.Search.Documents"

The namespace was being incorrectly duplicated to Azure.Search.Documents.Search.Documents.

Root Cause

The function used simple string replacement that would find a user-defined namespace and replace it with the flag value, causing duplication when the override already contained the desired final namespace.

The Fix

Added checks to return early if:

  1. The override exactly matches the namespace flag (for clients)
  2. The namespace flag is an extension of the user-defined namespace AND the joined segments already start with the flag (for models/types)

This prevents unnecessary string replacements that cause duplication while still allowing legitimate replacements when needed.

Original prompt

This section details on the original issue you should resolve

<issue_title>[TCGC]: Namespace duplication when using @clientNamespace with TCGC namespace option</issue_title>
<issue_description>### Describe the bug

Initial investigation stemmed from microsoft/typespec#9314.

When a spec uses @clientNamespace and the C# emitter is configured with a TCGC namespace option, the generated client namespace is incorrectly concatenated instead of using the explicit @clientNamespace value.

spec in question: link

From the playground link below, the tcgc API is returning the concatenated string when only Azure.Search.Documents was expected for the client's namespace.

Reproduction

Spec defines: @clientNamespace("Azure.Search.Documents") source
tspconfig.yaml has TCGC option: namespace: Azure.Search.Documents

playground link.

Checklist

  • Follow our Code of Conduct
  • Check that this issue is about the Azure libraries for typespec. For bug in the typespec language or core libraries file it in the TypeSpec repo
  • Check that there isn't already an issue that request the same bug to avoid creating a duplicate.
  • The provided reproduction is a minimal reproducible example of the bug.</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix namespace duplication when using @clientNamespace with TCGC Fix namespace duplication when @clientNamespace matches namespace flag Jan 27, 2026
Copilot AI requested a review from JoshLove-msft January 27, 2026 17:50
@JoshLove-msft JoshLove-msft marked this pull request as ready for review January 27, 2026 18:01
@azure-sdk
Copy link
Collaborator

❌ There is undocummented changes. Run chronus add to add a changeset or click here.

The following packages have changes but are not documented.

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

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website

@microsoft-github-policy-service microsoft-github-policy-service bot added the lib:tcgc Issues for @azure-tools/typespec-client-generator-core library label Jan 27, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 27, 2026

Open in StackBlitz

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

commit: 9d74c00

Copy link
Contributor Author

Copilot AI commented Jan 28, 2026

@copilot we need to run prettier:

Run pnpm run format:check

...

Fixed in commit 9d74c00. The formatting issue was a long line that needed to be split across multiple lines.

@JoshLove-msft JoshLove-msft force-pushed the copilot/fix-namespace-duplication-issue branch from 9d74c00 to 803d764 Compare January 28, 2026 17:36
@microsoft-github-policy-service microsoft-github-policy-service bot added eng lib:azure-resource-manager Issues for @azure-tools/typespec-azure-core library emitter:autorest Issues for @azure-tools/typespec-autorest emitter labels Jan 28, 2026
@JoshLove-msft JoshLove-msft changed the base branch from main to release/january-2026 January 28, 2026 17:37
@JoshLove-msft
Copy link
Member

@copilot I messed up. Please force push the final 7 commits only to this branch.

Copy link
Contributor Author

Copilot AI commented Jan 28, 2026

@copilot I messed up. Please force push the final 7 commits only to this branch.

I've prepared the branch locally with exactly 7 commits (removed the empty "Initial plan" commit). However, I cannot force push due to authentication limitations.

The local branch now has the correct history:

7dd3a23 Run prettier to fix code formatting
76c0631 Refine duplication check to be more precise
904c6ac Update .chronus/changes
c21420c Simplify fix by removing redundant check
1e37360 Create copilot changeset
73f65d9 Add comprehensive test coverage
69496be Fix namespace duplication

To complete the force push, you'll need to run:

git fetch origin copilot/fix-namespace-duplication-issue
git reset --hard 5a28391  # Reset to base
git cherry-pick baf0699 e556348 ce62184 6550e39 527d292 86882c2 803d764  # Apply 7 commits
git push --force origin copilot/fix-namespace-duplication-issue

Or alternatively, if you trust my local state, you can pull the prepared branch from the Actions runner (though this typically isn't accessible).

@JoshLove-msft JoshLove-msft force-pushed the copilot/fix-namespace-duplication-issue branch 2 times, most recently from cc01510 to 4db8856 Compare January 28, 2026 18:45
@JoshLove-msft JoshLove-msft changed the base branch from release/january-2026 to main January 28, 2026 18:56
Copilot AI and others added 7 commits January 28, 2026 10:58
Remove Check 2 which is redundant with Check 3's equality check.
Check 3 already includes `|| joinedSegments === context.namespaceFlag`
which covers the exact match scenario that Check 2 handled.

Co-authored-by: JoshLove-msft <[email protected]>
The previous check was too broad - it would skip replacement whenever
joinedSegments started with the namespace flag, even when replacement
was needed.

The refined check only skips replacement when:
1. The namespace flag is an extension of the user-defined namespace
2. AND the joinedSegments already starts with the flag

This correctly handles scenarios like:
- joinedSegments="Azure.Search.Documents.Foo" with flag="Azure.Search"
  and userNs="Azure.Search.Documents" → DO replacement (flag doesn't extend userNs)
- joinedSegments="Azure.Search.Documents" with flag="Azure.Search.Documents"
  and userNs="Azure.Search" → SKIP replacement (flag extends userNs, already has flag)

Co-authored-by: JonathanCrd <[email protected]>
@JoshLove-msft JoshLove-msft force-pushed the copilot/fix-namespace-duplication-issue branch from 4db8856 to 35a5d2d Compare January 28, 2026 18:58
@JoshLove-msft JoshLove-msft added this pull request to the merge queue Jan 28, 2026
Merged via the queue into main with commit 9c3646f Jan 28, 2026
22 checks passed
@JoshLove-msft JoshLove-msft deleted the copilot/fix-namespace-duplication-issue branch January 28, 2026 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:autorest Issues for @azure-tools/typespec-autorest emitter eng lib:azure-resource-manager Issues for @azure-tools/typespec-azure-core library 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]: Namespace duplication when using @clientNamespace with TCGC namespace option

6 participants