diff --git a/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs b/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs index ddd68ad109f5..ac292e837c8a 100644 --- a/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs +++ b/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs @@ -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($""" - + """); diff --git a/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs b/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs index 1eebfddaef95..774e917d814d 100644 --- a/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs +++ b/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs @@ -726,6 +726,7 @@ public void ClassLibrary_EntryPointFileDoesNotExist() /// /// Other files in the folder are not part of the compilation. + /// See . /// [Fact] public void MultipleFiles_RunEntryPoint() @@ -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"); + } + + /// + /// Setting EnableDefaultCompileItems=true via Directory.Build.props should not cause CS2002 warning. + /// See . + /// + [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"), """ + + + true + + + """); + + new DotnetCommand(Log, "run", "Program.cs") + .WithWorkingDirectory(testInstance.Path) + .Execute() + .Should().Pass() + .And.HaveStdOut("Hello, String from Util"); } /// @@ -3941,7 +3965,7 @@ public void Api() - + @@ -4008,7 +4032,7 @@ public void Api_Diagnostic_01() - + @@ -4078,7 +4102,7 @@ public void Api_Diagnostic_02() - +