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

Proposal: Exclude CompilerGeneratedFilesOutputPath from Compilation by Default #47046

Open
kasperk81 opened this issue Feb 23, 2025 · 4 comments · May be fixed by dotnet/roslyn#77325
Open

Proposal: Exclude CompilerGeneratedFilesOutputPath from Compilation by Default #47046

kasperk81 opened this issue Feb 23, 2025 · 4 comments · May be fixed by dotnet/roslyn#77325
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@kasperk81
Copy link
Contributor

Problem

When CompilerGeneratedFilesOutputPath is set in csproj, the .NET SDK emits source generator output to disk. However, these files are not excluded from compilation by default, leading to errors when running dotnet build a second time.

Currently, developers must manually add:

<ItemGroup>
    <Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
</ItemGroup>

Without this, builds fail due to duplicate type definitions: https://andrewlock.net/creating-a-source-generator-part-6-saving-source-generator-output-in-source-control/#excluding-emitted-files-from-the-compilation

Proposal

The .NET SDK should automatically exclude the CompilerGeneratedFilesOutputPath from compilation when it is set, preventing these errors.

Would love to hear thoughts on whether this can be supported natively!

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels Feb 23, 2025
@kasperk81
Copy link
Contributor Author

@jjonescz @jaredpar

@jjonescz
Copy link
Member

I imagine some users might be running generators explicitly (via some MSBuild property), including their outputs in the Git-tracked source tree, disabling the generators again, and from then on compiling normally without generators. I guess that's not a very good approach. But just wanted to note that adding <Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" /> would break these users (if they exist).

@kasperk81
Copy link
Contributor Author

Would exclusion by default with bailout option ExcludeGeneratedFilesFromCompilation (default true) work for those cases?

@jaredpar
Copy link
Member

Would exclusion by default with bailout option ExcludeGeneratedFilesFromCompilation (default true) work for those cases?

It would work but I'm not sure that is the direction that we want to go. The compiler has lots of knobs today, every one that we add is yet another knob that we have to track, support, etc ... Given that this issue already has a single line fix I don't think a new knob is the best way to go here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants