Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,9 +1412,12 @@ public static void WriteProjectFile(
{
Debug.Assert(targetFilePath is not null);

// Only add explicit Compile item when EnableDefaultCompileItems is not true.
// When EnableDefaultCompileItems=true, the file is included via default MSBuild globbing.
// See https://github.com/dotnet/sdk/issues/51785
writer.WriteLine($"""
<ItemGroup>
<Compile Include="{EscapeValue(targetFilePath)}" />
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{EscapeValue(targetFilePath)}" />
</ItemGroup>

""");
Expand Down
36 changes: 30 additions & 6 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ public void ClassLibrary_EntryPointFileDoesNotExist()

/// <summary>
/// Other files in the folder are not part of the compilation.
/// See <see href="https://github.com/dotnet/sdk/issues/51785"/>.
/// </summary>
[Fact]
public void MultipleFiles_RunEntryPoint()
Expand All @@ -750,9 +751,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.HaveStdOutContaining("Hello, String from Util");
.And.HaveStdOut("Hello, String from Util");
}

/// <summary>
/// Setting EnableDefaultCompileItems=true via Directory.Build.props should not cause CS2002 warning.
/// See <see href="https://github.com/dotnet/sdk/issues/51785"/>.
/// </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.HaveStdOut("Hello, String from Util");
}

/// <summary>
Expand Down Expand Up @@ -3941,7 +3965,7 @@ public void Api()
</ItemGroup>

<ItemGroup>
<Compile Include="{programPath}" />
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{programPath}" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -4008,7 +4032,7 @@ public void Api_Diagnostic_01()
</PropertyGroup>

<ItemGroup>
<Compile Include="{programPath}" />
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{programPath}" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -4078,7 +4102,7 @@ public void Api_Diagnostic_02()
</PropertyGroup>

<ItemGroup>
<Compile Include="{programPath}" />
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{programPath}" />
</ItemGroup>

<ItemGroup>
Expand Down