diff --git a/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs b/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs index c6ab42bbc498..8558751eafbd 100644 --- a/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs +++ b/src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs @@ -1180,11 +1180,16 @@ public static void WriteProjectFile( // Note that ArtifactsPath needs to be specified before Sdk.props // (usually it's recommended to specify it in Directory.Build.props // but importing Sdk.props manually afterwards also works). + // + // An empty value is always given for BaseIntermediateOutputPath and BaseOutputPath, to ensure that + // the SDK targets will initialize them based on ArtifactsPath, and not use values for those paths coming in from Directory.Build.props. writer.WriteLine($""" false + + {EscapeValue(artifactsPath)} artifacts/$(MSBuildProjectName) artifacts/$(MSBuildProjectName) diff --git a/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs b/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs index 5d7b53c65a3e..82c23728028e 100644 --- a/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs +++ b/test/dotnet.Tests/CommandTests/Run/RunFileTests.cs @@ -919,6 +919,39 @@ public void DefaultProps_DirectoryBuildProps() .And.HaveStdOutContaining("error CS0103"); } + /// + /// 'BaseIntermediateOutputPath'/'BaseOutputPath' specified in Directory.Build.props should effectively be ignored. + /// We want the standard logic for determining these from an 'ArtifactsPath' to always be used. + /// See also https://github.com/dotnet/sdk/blob/2b9fc02a265c735f2132e4e3626e94962e48bdf5/src/Tasks/Microsoft.NET.Build.Tasks/sdk/UseArtifactsOutputPath.props#L25. + /// + [Fact] + public void BaseOutputPathProps_FromDirectoryBuildProps_NotUsed() + { + var testInstance = _testAssetsManager.CreateTestDirectory(); + File.WriteAllText(Path.Join(testInstance.Path, "Program.cs"), """ + Console.WriteLine("Hi"); + """); + + var dbPropsObjPath = Path.Join(testInstance.Path, "MyOutput", "obj"); + var dbPropsBinPath = Path.Join(testInstance.Path, "MyOutput", "bin"); + File.WriteAllText(Path.Join(testInstance.Path, "Directory.Build.props"), $""" + + + {dbPropsObjPath} + {dbPropsBinPath} + + + """); + + new DotnetCommand(Log, "run", "Program.cs") + .WithWorkingDirectory(testInstance.Path) + .Execute() + .Should().Pass(); + + Assert.False(Directory.Exists(dbPropsObjPath)); + Assert.False(Directory.Exists(dbPropsBinPath)); + } + /// /// Overriding default (implicit) properties of file-based apps from custom SDKs. ///