-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix CS2002 when EnableDefaultCompileItems=true in file-based apps #51790
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
base: release/10.0.2xx
Are you sure you want to change the base?
Conversation
…apps Add condition to only include explicit Compile item when EnableDefaultCompileItems is not true. This prevents the main file from being included twice (once explicitly and once via default globbing). Updated existing test to verify no CS2002 warning is emitted. Added new test for Directory.Build.props scenario. Co-authored-by: jjonescz <3669664+jjonescz@users.noreply.github.com>
jjonescz
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
…_ tests - Add spaces around != operator in condition - Add comment explaining why condition is needed - Link to issue #51785 in test summaries - Use HaveStdOut instead of HaveStdOutContaining to ensure no warnings - Update Api_Diagnostic_01 and Api_Diagnostic_02 tests with condition Co-authored-by: jjonescz <3669664+jjonescz@users.noreply.github.com>
Add condition to Compile item in Api() test to match the fix Co-authored-by: jjonescz <3669664+jjonescz@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes CS2002 warning ("Source file specified multiple times") that occurred when EnableDefaultCompileItems=true was set in file-based apps. The fix prevents the main .cs file from being included twice by adding a condition to the explicit <Compile> item.
- Conditionally excludes explicit
<Compile>item whenEnableDefaultCompileItems=trueto avoid duplicate file inclusion - Adds comprehensive test coverage for both directive-based and Directory.Build.props-based configuration scenarios
- Updates API test expectations to reflect the new conditional compile item
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs | Added MSBuild condition to <Compile> item to skip explicit inclusion when EnableDefaultCompileItems is true, with explanatory comment and issue reference |
| test/dotnet.Tests/CommandTests/Run/RunFileTests.cs | Updated existing test to verify fix via #:property directive, added new test for Directory.Build.props scenario, and updated API test expectations with the new condition |
Fix CS2002 Warning with EnableDefaultCompileItems in File-Based Apps ✅
Status: Complete and Verified
Problem Fixed
When
EnableDefaultCompileItems=truewas set in a file-based app (via#:propertydirective orDirectory.Build.props), the main .cs file was included twice:<Compile Include="file.cs" />This caused CS2002 warning: "Source file specified multiple times"
Solution Implemented
VirtualProjectBuildingCommand.WriteProjectFile(line 1417)<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{targetFilePath}" />HaveStdOutinstead ofHaveStdOutContainingApi(),Api_Diagnostic_01,Api_Diagnostic_02) with the new conditionTest Results
✅ Test 1:
#:property EnableDefaultCompileItems=true- Works, no CS2002✅ Test 2:
Directory.Build.propswithEnableDefaultCompileItems=true- Works, no CS2002✅ Api tests: All updated and passing (Api, Api_Diagnostic_01, Api_Diagnostic_02)
✅ Unit tests: All passing
✅ SDK Build: Successful
Files Changed
src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs- Added condition with spaces, comment, and issue linktest/dotnet.Tests/CommandTests/Run/RunFileTests.cs- Updated tests with issue links, better assertions, and all Api_ test expectationsFixes #51785
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.