Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 13, 2026

  • Explore repository structure and understand the codebase
  • Locate relevant files: lib.ts, decorators.ts, package.ts, clients.ts
  • Understand how clientInitialization works
  • Understand test structure in alternate-type.test.ts
  • Add diagnostic warning definition in lib.ts for unused client initialization parameters
  • Add validation function to check if clientInitialization parameters are used in operations
  • Call validation function in package.ts after client hierarchy is created
  • Add comprehensive tests in client-initialization.test.ts
  • Add changeset describing the feature
  • Fix build failures:
    • Fixed TypeScript compilation errors (return type and invalid kind check)
    • Changed from createDiagnostic to reportDiagnostic to ensure diagnostics are properly reported
    • Fixed validation logic to avoid duplicate warnings for inherited parameters
  • All tests passing (82 test files, 1098 tests)
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: no warning on unused client initializer param(s)</issue_title>
<issue_description>### Describe the bug

If clientInitialization.parameters contains values that aren't used in any routes (client or its sub-clients), a diagnostic warning should be produced (maybe even an error as this seems like an authoring mistake).

Reproduction

tcgc version 0.54.0

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>

<agent_instructions>1. Add a linter rule warning definition here https://github.com/Azure/typespec-azure/blob/main/packages/typespec-client-generator-core/src/lib.ts
2. Add verification checks here https://github.com/Azure/typespec-azure/blob/main/packages/typespec-client-generator-core/src/decorators.ts#L1007. If this doesn't work with the test cases later, then add it once the whole client hierarchy is set up
3. Add tests like the one here to check whether a warning is raised https://github.com/Azure/typespec-azure/blob/main/packages/typespec-client-generator-core/test/decorators/alternate-type.test.ts#L199
4. Make sure this works with the @client, @operationGroup, and @clientLocation decorator. For example, make sure the code works if we use @operationGroup decorators to make something a sub client and it has those parameters, that we don't raise a warning.</agent_instructions>

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI self-assigned this Jan 13, 2026
Copilot AI changed the title [WIP] Fix no warning on unused client initializer parameters Add warning for unused client initialization parameters Jan 13, 2026
Copilot AI requested a review from iscai-msft January 13, 2026 20:11
@iscai-msft
Copy link
Contributor

@copilot fix build failures

Copy link
Contributor Author

Copilot AI commented Jan 13, 2026

@copilot fix build failures

Fixed in commit 3a28190. The build failures were caused by:

  1. TypeScript compilation error: validation function was returning readonly Diagnostic[] instead of being integrated with the existing diagnostic reporting mechanism
  2. Invalid method kind check: attempted to check for "clientaccessor" which doesn't exist in the SdkMethod type union
  3. Diagnostics not being reported to the program, so tests couldn't see them

Changes made:

  • Switched from createDiagnostic to reportDiagnostic to properly report diagnostics to the program
  • Removed invalid clientaccessor kind check
  • Fixed validation logic to only validate at top-level clients (not recursively) to avoid duplicate warnings for inherited parameters

All 1098 tests now pass including the 8 new tests for unused client initialization parameter warnings.

iscai-msft added 2 commits January 26, 2026 15:19
@microsoft-github-policy-service microsoft-github-policy-service bot added the lib:tcgc Issues for @azure-tools/typespec-client-generator-core library label Jan 26, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Jan 26, 2026

Open in StackBlitz

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

commit: 3169c5a

@iscai-msft iscai-msft marked this pull request as ready for review January 26, 2026 20:59
@azure-sdk
Copy link
Collaborator

All changed packages have been documented.

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

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

Add warning diagnostic for unused client initialization parameters. If clientInitialization.parameters contains values that aren't used in any routes (client or its sub-clients), a diagnostic warning is now produced.

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website

@iscai-msft iscai-msft enabled auto-merge January 26, 2026 21:16
Copy link
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.

The validation should not apply to all clients since the parameter gathering iteration will cause perf issue. We should check it only when there is customized client initialization parameter. Another problem for this PR is it does not take @paramAlias into account.

>(context: TCGCContext, client: SdkClientType<TServiceOperation>): void {
// Only validate when there's a customized @clientInitialization decorator with parameters
// Skip validation for default/auto-generated client initialization
if (!client.clientInitialization.__raw) {
Copy link
Member

Choose a reason for hiding this comment

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

Oh, I just found a problem for this skip. This will also skip the check for nested clients with client initialization customization.

Copy link
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.

@iscai-msft I missed some comments when approval. I could not fine the dismiss button. So, please check my new comments before merge. Thanks.

for (const path of param.methodParameterSegments) {
for (const methodParam of path) {
if (methodParam.kind === "method" && methodParam.onClient) {
parameterNames.add(methodParam.name);
Copy link
Member

Choose a reason for hiding this comment

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

You should not add name, but just add instance.

// Check each custom client initialization parameter
for (const param of customParams) {
// Check if this parameter is used in any operation
if (!allOperationParameterNames.has(param.name)) {
Copy link
Member

Choose a reason for hiding this comment

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

You could use instance check directly since the methodParameterSegments stores the direct reference, which could resolve potential naming and alias issue.

@tadelesh tadelesh disabled auto-merge January 28, 2026 05:52
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.

[Bug]: no warning on unused client initializer param(s)

4 participants