Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[http-client-csharp] the post processor should always keep customized code as root documents #5481

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

ArcturusZhang
Copy link
Member

@ArcturusZhang ArcturusZhang commented Jan 3, 2025

Fixes #5441
Previously in our generator, we have two instances of GeneratedCodeWorkspace: one for the project that is being generated right now (the generated code project), one for the existing part of the generated library (the customized code project).
This leads to an issue that in the post processor, only the generated documents are passed into the post processor, therefore the post processor actually does not know about the existence of the customized files.

This is not very correct because the generated files must need those customized files to work properly therefore they should be in the same project.
This PR refactors this part to change the structure of GeneratedCodeWorkspace: now we only create one instance of GeneratedCodeWorkspace, and the project inside it will be initialized with shared files and all the customized files in it.

In this way, when we get to the post processor, it should be able to access all the necessary documents.

@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 Jan 3, 2025
@ArcturusZhang ArcturusZhang force-pushed the post-processor-should-always-keep-customized-types branch from c4a9678 to 668c68b Compare January 3, 2025 07:08
@azure-sdk
Copy link
Collaborator

API change check

API changes are not detected in this pull request.

@ArcturusZhang ArcturusZhang marked this pull request as ready for review January 6, 2025 06:49
Comment on lines -252 to -256
private async Task<Compilation> GetProjectCompilationAsync()
{
_compilation ??= await _project.GetCompilationAsync();
return _compilation!;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

this is a duplicate with GetCompilationAsync method in this same class.

Comment on lines +50 to +51
var compilation = await project.GetCompilationAsync();
return compilation!;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: return it instead of declaring a variable

Copy link
Member Author

Choose a reason for hiding this comment

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

I would like to do that but the return of this method is Task<Compilation?> and here we want a Task<Compilation>, we must have a local variable and assert it is not null.

Copy link
Member Author

Choose a reason for hiding this comment

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

otherwise it might look like this:

return (await project.GetCompilationAsync())!;

I prefer local variable rather than the above

Copy link
Contributor

Choose a reason for hiding this comment

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

So it will be null if SupportsCompilation is false. It's not clear to me when that would happen but perhaps we should add some assertion that it is not null rather than using the null-forgiving operator.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see this is specifically for the test helper, so probably less of an issue here. But for the GetCompilationAsync method we should add validation with a helpful error message that says SupportsCompilation must be true.

…rp/src/PostProcessing/GeneratedCodeWorkspace.cs

Co-authored-by: Wei Hu <[email protected]>
@@ -29,7 +29,7 @@ public async Task ExecuteAsync()
var generatedTestOutputPath = CodeModelPlugin.Instance.Configuration.TestGeneratedDirectory;

GeneratedCodeWorkspace workspace = await GeneratedCodeWorkspace.Create();
await CodeModelPlugin.Instance.InitializeSourceInputModelAsync();
CodeModelPlugin.Instance.SourceInputModel = new SourceInputModel(await workspace.GetCompilationAsync());
Copy link
Contributor

Choose a reason for hiding this comment

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

Just curious why we change the initializing here.

Copy link
Contributor

@JoshLove-msft JoshLove-msft left a comment

Choose a reason for hiding this comment

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

Can we add a test?

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.

[http-client-csharp] post processor is not treating the customization code as root node
4 participants