Skip to content

Commit d36a95d

Browse files
authored
Merge branch 'main' into darc-main-42832304-ba9d-4420-8de4-5d0264fe440e
2 parents 8948734 + ca6c850 commit d36a95d

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1412,9 +1412,12 @@ public static void WriteProjectFile(
14121412
{
14131413
Debug.Assert(targetFilePath is not null);
14141414

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

test/dotnet.Tests/CommandTests/Run/RunFileTests.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,7 @@ public void ClassLibrary_EntryPointFileDoesNotExist()
726726

727727
/// <summary>
728728
/// Other files in the folder are not part of the compilation.
729+
/// See <see href="https://github.com/dotnet/sdk/issues/51785"/>.
729730
/// </summary>
730731
[Fact]
731732
public void MultipleFiles_RunEntryPoint()
@@ -750,9 +751,32 @@ public void MultipleFiles_RunEntryPoint()
750751
.WithWorkingDirectory(testInstance.Path)
751752
.Execute()
752753
.Should().Pass()
753-
// warning CS2002: Source file 'Program.cs' specified multiple times
754-
.And.HaveStdOutContaining("warning CS2002")
755-
.And.HaveStdOutContaining("Hello, String from Util");
754+
.And.HaveStdOut("Hello, String from Util");
755+
}
756+
757+
/// <summary>
758+
/// Setting EnableDefaultCompileItems=true via Directory.Build.props should not cause CS2002 warning.
759+
/// See <see href="https://github.com/dotnet/sdk/issues/51785"/>.
760+
/// </summary>
761+
[Fact]
762+
public void MultipleFiles_EnableDefaultCompileItemsViaDirectoryBuildProps()
763+
{
764+
var testInstance = _testAssetsManager.CreateTestDirectory();
765+
File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), s_programDependingOnUtil);
766+
File.WriteAllText(Path.Join(testInstance.Path, "Util.cs"), s_util);
767+
File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), """
768+
<Project>
769+
<PropertyGroup>
770+
<EnableDefaultCompileItems>true</EnableDefaultCompileItems>
771+
</PropertyGroup>
772+
</Project>
773+
""");
774+
775+
new DotnetCommand(Log, "run", "Program.cs")
776+
.WithWorkingDirectory(testInstance.Path)
777+
.Execute()
778+
.Should().Pass()
779+
.And.HaveStdOut("Hello, String from Util");
756780
}
757781

758782
/// <summary>
@@ -3941,7 +3965,7 @@ public void Api()
39413965
</ItemGroup>
39423966
39433967
<ItemGroup>
3944-
<Compile Include="{programPath}" />
3968+
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{programPath}" />
39453969
</ItemGroup>
39463970
39473971
<ItemGroup>
@@ -4008,7 +4032,7 @@ public void Api_Diagnostic_01()
40084032
</PropertyGroup>
40094033
40104034
<ItemGroup>
4011-
<Compile Include="{programPath}" />
4035+
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{programPath}" />
40124036
</ItemGroup>
40134037
40144038
<ItemGroup>
@@ -4078,7 +4102,7 @@ public void Api_Diagnostic_02()
40784102
</PropertyGroup>
40794103
40804104
<ItemGroup>
4081-
<Compile Include="{programPath}" />
4105+
<Compile Condition="'$(EnableDefaultCompileItems)' != 'true'" Include="{programPath}" />
40824106
</ItemGroup>
40834107
40844108
<ItemGroup>

0 commit comments

Comments
 (0)