Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix CS2002 warning when EnableDefaultCompileItems=true in file-based …
…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 <[email protected]>
  • Loading branch information
Copilot and jjonescz committed Nov 18, 2025
commit 619b9e44273f83a19b6888039d6f646f5da65b3a
Original file line number Diff line number Diff line change
Expand Up @@ -1414,7 +1414,7 @@ public static void WriteProjectFile(

writer.WriteLine($"""
<ItemGroup>
<Compile Include="{EscapeValue(targetFilePath)}" />
<Compile Condition="'$(EnableDefaultCompileItems)'!='true'" Include="{EscapeValue(targetFilePath)}" />
</ItemGroup>

""");
Expand Down
28 changes: 26 additions & 2 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,8 +750,32 @@ public void MultipleFiles_RunEntryPoint()
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
// warning CS2002: Source file 'Program.cs' specified multiple times
.And.HaveStdOutContaining("warning CS2002")
.And.NotHaveStdOutContaining("warning CS2002")
.And.HaveStdOutContaining("Hello, String from Util");
}

/// <summary>
/// Setting EnableDefaultCompileItems=true via Directory.Build.props should not cause CS2002 warning.
/// </summary>
[Fact]
public void MultipleFiles_EnableDefaultCompileItemsViaDirectoryBuildProps()
{
var testInstance = _testAssetsManager.CreateTestDirectory();
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_programDependingOnUtil);
File.WriteAllText(Path.Join(testInstance.Path, "Util.cs"), s_util);
File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), """
<Project>
<PropertyGroup>
<EnableDefaultCompileItems>true</EnableDefaultCompileItems>
</PropertyGroup>
</Project>
""");

new DotnetCommand(Log, "run", "Program.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.NotHaveStdOutContaining("warning CS2002")
.And.HaveStdOutContaining("Hello, String from Util");
}

Expand Down
Loading