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

Generated code should include all required using statements #514

Closed
shuebner opened this issue Dec 15, 2024 · 2 comments · Fixed by #518
Closed

Generated code should include all required using statements #514

shuebner opened this issue Dec 15, 2024 · 2 comments · Fixed by #518
Labels
enhancement New feature or request

Comments

@shuebner
Copy link

In 4.1.0, the generated code relies on some stdlib types being available, like Stream, ReadOnlyMemory, IDictionary etc. without having a using statement for the corresponding namespaces.

This means that the generated code only compiles if the consuming project sets global usings for those namespaces, e. g. by declaring Microsoft.NET.Sdk as the project SDK and setting ImplicitUsings to true.

Generated code should IMHO not put this burden on the consumer (and restrict consumers to a C# version where global usings are available in the first place).
Please generate the necessary using statements in the code requiring them.

@mwadams
Copy link
Contributor

mwadams commented Dec 15, 2024

If we do generate those usings, then you get warnings for the duplicate usings in what is the default situation when you do adotnet new or new project. We decided to default to the "do not add usings" case.

However, there is no reason this could not be another option.

I'll add that.

@shuebner
Copy link
Author

shuebner commented Dec 16, 2024

I don't think any complication or option is needed here:

  1. Which diagnostic are you referring to? If you mean IDE0005, then I cannot reproduce with the dotnet tool. I do not get such diagnostics from either the CLI build or VS on code marked with <auto-generated>, as it should be.

  2. If you really want to only generate the usings when actually necessary, the source generator should already know if there is a global using. No need for an option IMHO.

@mwadams mwadams added the enhancement New feature or request label Dec 17, 2024
@mwadams mwadams linked a pull request Dec 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants