Repository navigation
Description
Is your feature request related to a problem? Please describe.
I'm trying to use #:include to compose modular file-based C# apps, but duplicate inclusion through transitive include graphs currently produces a poor experience.
A simple example is:
- A includes B
- B includes C
- A also includes C
In this scenario, the app can still build and run successfully, but the build emits CS2002 because the same source file is passed to the compiler multiple times.
From a file-based app user's perspective, this is not really the same as accidentally invoking the compiler with the same source file twice. It is a natural outcome of source composition through #:include.
Requiring users to manually pre-deduplicate include graphs makes file-based apps less elegant and pushes them toward heavier project-style preprocessing, which works against the lightweight promise of the feature.
Related discussions / implementations:
dotnet/sdk#52347— added#:includesupport for file-based appsdotnet/roslyn#82625— editor completion support for#:include- discussion about declarative conditional directives:
dotnet/sdk#52532
Describe the solution you'd like
I'd like the SDK to recognize file-based app include graphs and deduplicate repeated source files before passing compile inputs to the compiler.
Preferred behavior:
- detect duplicate source files arising from
#:includeexpansion - normalize / resolve paths first
- compile each source file only once
- preserve stable ordering for the first occurrence
- keep build/run/convert behavior consistent
If possible, I think this should be handled at the SDK / virtual project layer rather than being left to the compiler to surface as a raw CS2002 experience.
If deduplication cannot be implemented immediately, an intermediate improvement would be to provide a more file-based-app-specific, lower-noise diagnostic instead of exposing the traditional duplicate-source warning experience directly.
Alternative solutions considered:
- leaving the current behavior as-is and expecting users to manually deduplicate include closures
- works, but does not feel appropriate for a lightweight file-based app model
- lowering the severity or contextualizing the diagnostic for file-based app scenarios
- better than today, but still less ideal than SDK-side deduplication
- runtime conditional inclusion
- not recommended; build-time declarative conditions would be a better direction for advanced composition than runtime compilation behavior
Additional context
This seems especially important if #:include is expected to scale beyond very small examples.
Without SDK-side deduplication, users are incentivized to add more manual preprocessing and scenario-specific closure management just to avoid duplicate include requests. That may be acceptable in specialized tooling, but it does not seem like the right default burden for typical file-based app users.
The reason I believe dotnet/sdk is the right place for this is that #:include itself appears to be implemented as an SDK / virtual-project feature (dotnet/sdk#52347), while Roslyn support such as editor completion is handled separately (dotnet/roslyn#82625).
If helpful, I can provide a minimal repro based on a file-based app where transitive #:include causes CS2002 even though the app still builds and runs successfully.
Activity
jjonescz commentedon Apr 27, 2026
Yes, deduplicating directives is a known feature gap currently. See also the spec which says:
Specifically for
#:include, you can probably work around that simply by adding#:property NoWarn=CS2002?code-gal commentedon Apr 27, 2026
Thanks for confirming this is a known feature gap and for sharing the spec reference.
I validated the workaround locally: NoWarn=CS2002 does suppress the warning in this transitive #:include case, so that works as a short-term mitigation.
For the long term, I’d like to keep this issue as a tracker for SDK-side deduplication of file-based include expansion, ideally based on resolved/normalized paths so transitive overlaps are handled predictably.
code-gal commentedon Apr 27, 2026
I opened a separate issue for deterministic handling of non-duplicate #: directive conflicts in multi-file composition: #54090
code-gal commentedon Oct 7, 2026
My project uses multi-file, file-based C# apps as infrastructure. Each reusable source file declares its own direct dependencies through #:include.
When an app includes two modules that both include a shared model, the same source file is reached twice. This is a normal diamond dependency, but it produces CS2002.
Removing one declaration makes a module rely on another module to supply its dependency. Moving all declarations to the entry point makes every consumer maintain the modules’ transitive dependencies. Neither preserves self-contained dependency declarations.
MSBuild deduplication is a workaround, but could the SDK support this directly? Within one compilation, multiple include paths resolving to the same source file should yield one compiler input without CS2002.
jjonescz commentedon Oct 7, 2026
Yes, I think that's what this issue is tracking. Given it's just a non-blocking warning, I haven't prioritized implementing that. Also, it might be a good idea to think about deduplication of other
#:directives first, so the design is coherent.code-gal commentedon Oct 14, 2026
Understood that comprehensive directive deduplication requires broader design work.
However, the main issue with persistent CS2002 warnings isn't cosmetic—it signals to developers that modular composition isn't officially supported or ready for production use. When consumers can properly manage their composition, this warning is unnecessary.
Could the SDK consider a lightweight default mitigation for .NET 11 GA (such as suppressing or downgrading CS2002 for file-based apps) to give developers production confidence?
jjonescz commentedon Oct 14, 2026
It's too late to make changes like this for .NET 11 GA at this point.
(FWIW, you can suppress the warning for file-based apps yourself via something like
<NoWarn Condition="'$(FileBasedProgram)'=='true'">$(NoWarn);CS2002</NoWarn>in aDirectory.Build.propsfile placed in a directory above all your file-based app files.)code-gal commentedon Oct 14, 2026
Got it, thanks for confirming the timeline and for sharing the
$(FileBasedProgram)property trick!