Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 18, 2025

Fix CS2002 Warning with EnableDefaultCompileItems in File-Based Apps ✅

Status: Complete and Verified

Problem Fixed

When EnableDefaultCompileItems=true was set in a file-based app (via #:property directive or Directory.Build.props), the main .cs file was included twice:

  • Once explicitly via <Compile Include="file.cs" />
  • Once implicitly via MSBuild's default compile item globbing

This caused CS2002 warning: "Source file specified multiple times"

Solution Implemented

  • Modified VirtualProjectBuildingCommand.WriteProjectFile (line 1417)
  • Added condition: <Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{targetFilePath}" />
  • Added comment explaining why the condition is needed
  • Added issue link reference in test summaries
  • Updated tests to use HaveStdOut instead of HaveStdOutContaining
  • Updated all Api tests (Api(), Api_Diagnostic_01, Api_Diagnostic_02) with the new condition
  • All tests pass successfully
  • Manual verification completed for both scenarios
  • Build succeeds with no warnings or errors
  • Security scan (CodeQL) passed

Test Results

✅ Test 1: #:property EnableDefaultCompileItems=true - Works, no CS2002
✅ Test 2: Directory.Build.props with EnableDefaultCompileItems=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

  1. src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs - Added condition with spaces, comment, and issue link
  2. test/dotnet.Tests/CommandTests/Run/RunFileTests.cs - Updated tests with issue links, better assertions, and all Api_ test expectations

Fixes #51785

Original prompt

This section details on the original issue you should resolve

<issue_title>Setting EnableDefaultCompileItems=True in a file-based app results in CS2002 as app file is included multiple times</issue_title>
<issue_description>If you set EnableDefaultCompileItems to true via a directive in a file-based app, in order to re-enable the default compilation items (like a regular project), you end up with a CS2002 warning being raised because the main file-based app .cs file is still explicitly included in the virtual project.

  1. Add #:property EnableDefaultCompileItems=True to a file-based app
  2. Build or run the file-based app
  3. See CS2002 emitted for the app

We should consider conditioning the explicitly added compile item so that it's not added if EnableDefaultCompileItems is already true.</issue_description>

<agent_instructions>Let's avoid adding <Compile Include=""/> in file-based apps if EnableDefaultCompileItems is true. That can be set also in a Directory.Build.props file, not only as a #:property directive. Add tests for both cases. Hence we should probably do it at the msbuild level like <Compile Condition="'$(EnableDefaultCompileItems)'!='true'" Include=""/>.</agent_instructions>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@jjonescz jjonescz added the Area-run-file Items related to the "dotnet run <file>" effort label Nov 18, 2025
…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>
Copilot AI changed the title [WIP] Fix CS2002 warning by conditionally including compile items Fix CS2002 when EnableDefaultCompileItems=true in file-based apps Nov 18, 2025
Copilot AI requested a review from jjonescz November 18, 2025 10:37
Copilot finished work on behalf of jjonescz November 18, 2025 10:37
Copy link
Member

@jjonescz jjonescz left a 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>
Copilot finished reviewing on behalf of jjonescz November 19, 2025 10:25
Copy link
Contributor

Copilot AI left a 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 when EnableDefaultCompileItems=true to 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

@jjonescz jjonescz requested a review from MiYanni November 20, 2025 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-run-file Items related to the "dotnet run <file>" effort

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants