Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
142 stars 165 forks source link

Can't generate contoso project with typespec-csharp #4827

Closed catalinaperalta closed 1 week ago

catalinaperalta commented 3 months ago

Describe the issue or request Someone from the API mgmt team tried to run tsp-client init -c https://github.com/Azure/azure-rest-api-specs/blob/431eb865a581da2cd7b9e953ae52cb146f31c2a6/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml in the azure-sdk-for-net repo and ran into the following emitter issue:

C:\Users\caperal\repos\azure-sdk-for-net> tsp-client init -c https://github.com/Azure/azure-rest-api-specs/blob/431eb865a581da2cd7b9e953ae52cb146f31c2a6/specification/contosowidgetmanager/Contoso.WidgetManager/tspconfig.yaml
Use output directory 'C:/Users/caperal/repos/azure-sdk-for-net'? (y/n) 
Using output directory 'C:/Users/caperal/repos/azure-sdk-for-net'

888                                      888 d8b                   888
888                                      888 Y8P                   888
888                                      888                       888
888888 .d8888b  88888b.          .d8888b 888 888  .d88b.  88888b.  888888
888    88K      888 "88b        d88P"    888 888 d8P  Y8b 888 "88b 888
888    "Y8888b. 888  888 888888 888      888 888 88888888 888  888 888
Y88b.       X88 888 d88P        Y88b.    888 888 Y8b.     888  888 Y88b.
 "Y888  88888P' 88888P"          "Y8888P 888 888  "Y8888  888  888  "Y888
                888
                888
                888

tsp-client version: 0.8.1
Found emitter package @azure-tools/typespec-csharp
SDK initialized in C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso
Processing additional directory: specification/contosowidgetmanager/Contoso.WidgetManager.Shared
Syncing additional directory: specification/contosowidgetmanager/Contoso.WidgetManager.Shared
Found emitter package @azure-tools/typespec-csharp
Installing dependencies from npm...

added 85 packages, and audited 86 packages in 3s

12 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
Resolved path: C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso/TempTypeSpecFiles/Contoso.WidgetManager/node_modules/@typespec/compiler
Compiling tsp using @azure-tools/typespec-csharp...
Unhandled exception. System.Exception: Unknown type
   at AutoRest.CSharp.Generation.Types.TypeFactory.CreateType(InputType inputType) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Common\Generation\Types\TypeFactory.cs:line 92
   at AutoRest.CSharp.Output.Models.OperationMethodChainBuilder.GetLongRunningResultRetrievalMethod(OperationLongRunning longRunning) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\Output\OperationMethodChainBuilder.cs:line 174
   at AutoRest.CSharp.Output.Models.OperationMethodChainBuilder.BuildOperationMethodChain() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\Output\OperationMethodChainBuilder.cs:line 115
   at AutoRest.CSharp.Output.Models.LowLevelClient.BuildMethods(LowLevelClient client, TypeFactory typeFactory, IEnumerable`1 operations, ClientFields fields, String namespaceName, String clientName, SourceInputModel sourceInputModel)+MoveNext() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\Output\LowLevelClient.cs:line 131
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at AutoRest.CSharp.Output.Models.LowLevelClient.get_AllClientMethods() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\Output\LowLevelClient.cs:line 95
   at AutoRest.CSharp.Output.Models.LowLevelClient.get_ClientMethods() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\Output\LowLevelClient.cs:line 98
   at AutoRest.CSharp.LowLevel.Output.Samples.DpgClientSampleProvider..ctor(String defaultNamespace, LowLevelClient client, SourceInputModel sourceInputModel) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\Output\Samples\DpgClientSampleProvider.cs:line 30
   at AutoRest.CSharp.Output.Models.Types.DpgOutputLibrary.EnsureDpgSampleProviders() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\AutoRest\DpgOutputLibrary.cs:line 110
   at AutoRest.CSharp.Output.Models.Types.DpgOutputLibrary.get_DpgClientSampleProviders() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\AutoRest\DpgOutputLibrary.cs:line 98
   at AutoRest.CSharp.Output.Models.Types.DpgOutputLibrary.GetSampleForClient(LowLevelClient client) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\AutoRest\DpgOutputLibrary.cs:line 119
   at AutoRest.CSharp.AutoRest.Plugins.LowLevelTarget.ExecuteAsync(GeneratedCodeWorkspace project, InputNamespace inputNamespace, SourceInputModel sourceInputModel, Boolean isTspInput) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\LowLevel\AutoRest\LowLevelTarget.cs:line 43
   at AutoRest.CSharp.AutoRest.Plugins.CSharpGen.ExecuteAsync(InputNamespace rootNamespace) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Common\AutoRest\Plugins\CSharpGen.cs:line 104
   at AutoRest.CSharp.AutoRest.Communication.StandaloneGeneratorRunner.RunAsync(CommandLineOptions options) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Common\AutoRest\Communication\StandaloneGeneratorRunner.cs:line 51
   at AutoRest.CSharp.Program.Run(CommandLineOptions options) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Program.cs:line 47
   at AutoRest.CSharp.Program.<>c.<<Main>b__2_0>d.MoveNext() in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Program.cs:line 29
--- End of stack trace from previous location ---
   at AutoRest.CSharp.Program.Main(String[] args) in D:\a\_work\1\s\autorest.csharp\src\AutoRest.CSharp\Program.cs:line 26     
   at AutoRest.CSharp.Program.<Main>(String[] args)
ExternalError: Emitter "@azure-tools/typespec-csharp" crashed! This is a bug.
Please file an issue at https://github.com/Azure/autorest.csharp/issues

Error: Command failed: dotnet --roll-forward Major C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso/TempTypeSpecFiles/Contoso.WidgetManager/node_modules/@autorest/csharp/AutoRest.CSharp.dll --project-path C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso --new-project  --clear-output-folder true      
    at checkExecSyncError (node:child_process:890:11)
    at execSync (node:child_process:962:15)
    at Object.$onEmit [as emitFunction] (file:///C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso/TempTypeSpecFiles/Contoso.WidgetManager/node_modules/@azure-tools/typespec-csharp/dist/src/emitter.js:106:21)       
    at async runEmitter (file:///C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso/TempTypeSpecFiles/Contoso.WidgetManager/node_modules/@typespec/compiler/dist/src/core/program.js:555:13)
    at async compile (file:///C:/Users/caperal/repos/azure-sdk-for-net/sdk/contosowidgetmanager/Azure.Template.Contoso/TempTypeSpecFiles/Contoso.WidgetManager/node_modules/@typespec/compiler/dist/src/core/program.js:237:9)
    at async compileTsp (file:///C:/Users/caperal/AppData/Roaming/npm/node_modules/@azure-tools/typespec-client-generator-cli/dist/typespec.js:76:21)
    at async generate (file:///C:/Users/caperal/AppData/Roaming/npm/node_modules/@azure-tools/typespec-client-generator-cli/dist/index.js:204:5)
    at async main (file:///C:/Users/caperal/AppData/Roaming/npm/node_modules/@azure-tools/typespec-client-generator-cli/dist/index.js:277:17)

--------------------------------------------------
Library Version                0.2.0-beta.20240614.3
TypeSpec Compiler Version      0.56.0
--------------------------------------------------

Describe your ideas for solutions I would expect the contoso project in the azure-rest-api-specs repo to be able to be generated with the csharp emitter.

Add labels As appropriate, select a label to describe how hard the issue is to work around or how hard it would be to do without a particular feature that could help make it easier. These labels start with "workaround".

Also select a label that describes how many instances of the workaround you would have to do without the bug being fixed or feature implemented. These labels start with "instances".

These labels will help priority bug fixes and feature requests.

If this bug or feature request is for older versions of autorest, please remove the v3 label and add the v2 label as appropriate.

catalinaperalta commented 3 months ago

cc @lirenhe @chunyu3

chunyu3 commented 3 months ago

hello @catalinaperalta The error show that there is a type which is not supported. Would you please provide the spec used for us to investigate? thanks

catalinaperalta commented 3 months ago

Here it is: https://github.com/Azure/azure-rest-api-specs/tree/main/specification/contosowidgetmanager

chunyu3 commented 3 months ago

The root cause is that the ReturnType of LRO operation is null but the ResultPath is not null. @archerzz would you please help on this? thanks

archerzz commented 2 months ago

The root cause is the @pollingOperation: https://github.com/Azure/azure-rest-api-specs/blob/934f8b595b6aba70096c6fd19089c3986b344c0c/specification/contosowidgetmanager/Contoso.WidgetManager/main.tsp#L53C29-L53C53

@pollingOperation(Widgets.getWidgetOperationStatus)

Widgets.getWidgetOperationStatus is defined as a polling with return value: https://github.com/Azure/azure-rest-api-specs/blob/934f8b595b6aba70096c6fd19089c3986b344c0c/specification/contosowidgetmanager/Contoso.WidgetManager/main.tsp#L46

getWidgetOperationStatus is GetResourceOperationStatus<WidgetSuite>;

So, the LroMetadata contains ResultPath (because you have a resource polling), but no ReturnType (because its delete).

archerzz commented 2 months ago

@catalinaperalta I created a PR to fix the problematic spec: https://github.com/Azure/azure-rest-api-specs/pull/30012

microsoft-github-policy-service[bot] commented 3 weeks ago

Hi, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!