Remove unused usings when internalizing types#10969
Conversation
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
commit: |
Agent-Logs-Url: https://github.com/microsoft/typespec/sessions/fd547137-3f63-4177-82be-da0c260abc81 Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
jorgerangel-msft
left a comment
There was a problem hiding this comment.
@copilot we need to ensure all test libraries are regenerated as well for each change we make
…alidated tests Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
… to avoid stripping needed usings Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Regenerated all test libraries (Sample-TypeSpec + Spector). This surfaced a real bug: the unused-using pass ran before the simplification stage, so type references were still fully qualified and |
…eedback Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…sedUsings Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
|
@copilot address merge conflicts |
…-system-diagnostic-import # Conflicts: # packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/PostProcessing/PostProcessor.cs # packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/PostProcessing/PostProcessorTests.cs Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Merged |
| private readonly HashSet<string> _typesToKeep; | ||
| private INamedTypeSymbol? _modelFactorySymbol; | ||
|
|
||
| private static readonly string[] _experimentalAttributeNames = ["Experimental", "ExperimentalAttribute"]; |
There was a problem hiding this comment.
Removed the [Experimental]-stripping logic that #10972 reverted (_experimentalAttributeNames, RemoveExperimentalAttribute, IsExperimentalAttribute, and the call in MarkInternal). The PR now only does the generic unused-using removal: the CS8019-based pass runs after internalizing types and pruning model-factory methods and drops any directive left unused. Also removed the experimental-specific test/assets (kept RemovesUnusedUsingFromModelFactoryWhenInternalizing) and regenerated all test libraries — zero diff, since no generated type carried [Experimental].
jorgerangel-msft
left a comment
There was a problem hiding this comment.
@copilot it looks like we still have bugs in our implementation. The diff from regenerating all the libraries is here https://github.com/Azure/azure-sdk-for-net/pull/59868/changes. You can see, for serialization we incorrectly removed the using System.ClientModel statements
…sing cleanup Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
…ping needed usings) Co-authored-by: jorgerangel-msft <102122018+jorgerangel-msft@users.noreply.github.com>
Fixed. Root cause: when the post-processing compilation can't resolve some external type references (e.g. |
When the post-processor internalizes an unreferenced public type or prunes a model factory method, a
usingdirective can be left dangling on the generated type when nothing else in the file references that namespace. This adds a generic pass that removes any such now-unusedusingdirective.Changes
PostProcessor.InternalizeAsync: after internalizing types and pruning model factory methods, runs a cleanup pass over the affected documents to drop now-unused using directives.RemoveUnusedUsingsAsync: a generic pass that removes any using directive flagged by the C# compiler'sCS8019("unnecessary using directive") diagnostic, while preserving a using when the namespace is still referenced.RemoveMethodsFromModelFactoryAsyncnow reports the model factory document it modified, and that document is included in the unused-using cleanup pass. When a model in a different namespace is internalized and its model factory method removed, the now-unusedusingdirective for that namespace is also dropped from the model factory.CS0103/CS0234/CS0246/CS0305/CS0308) theCS8019diagnostic is unreliable and can falsely flag a genuinely-used directive (for exampleusing System.ClientModel;in a serialization file when that assembly is not referenced).RemoveUnusedUsingsnow skips removal for any document that has such unresolved-reference diagnostics, so needed usings are preserved and the generated code still builds.> Note: An earlier revision of this PR re-introduced the reverted
[Experimental]-attribute stripping logic (#10972 reverting #10958). Per review feedback, that logic has been removed — this PR no longer re-adds the experimental-attribute stripping and instead focuses solely on the generic removal of unused using statements.Tests
RemovesUnusedUsingFromModelFactoryWhenInternalizing:Sample.Models) is unreferenced and internalized, itsSampleModelFactorymethod is removed, and the now-unusedusing Sample.Models;is dropped from the model factory document while the referenced model and its factory method are preserved. The generated model factory output is validated against an expectedTestDatafile.KeepsUsedUsingWhenTypeReferencesAreUnresolved:System.ClientModelconversion operators is internalized in a compilation that lacks theSystem.ClientModelreference (soBinaryContent/ClientResultcannot be resolved). The test confirms the genuinely-usedusing System.ClientModel;is preserved rather than removed, and validates the full document output against an expectedTestDatafile.The experimental-specific test (
RemovesExperimentalAttributeWhenInternalizing) and its TestData assets were removed along with the reverted logic. Regenerated all test libraries (Sample-TypeSpec + Spector) with zero diff, confirming the genuine unused-using removals come from the retained pass and that the unresolved-reference guard introduces no regression; all generator tests pass.