diff --git a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets index a2a3f285dbaa..73c6643b6053 100644 --- a/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets +++ b/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.RuntimeIdentifierInference.targets @@ -67,17 +67,29 @@ Copyright (c) .NET Foundation. All rights reserved. $(PublishSelfContained) + true + ( + '$(SelfContained)' == 'true' or + ('$(_IsPublishing)' == 'true' and + ( + '$(PublishReadyToRun)' == 'true' or + '$(PublishSingleFile)' == 'true' or + '$(PublishAot)' == 'true' + ) + ) + )">true @@ -87,7 +99,7 @@ Copyright (c) .NET Foundation. All rights reserved. $(PublishRuntimeIdentifier) - + <_UsingDefaultPlatformTarget>true @@ -179,15 +191,15 @@ Copyright (c) .NET Foundation. All rights reserved. ResourceName="ImplicitRuntimeIdentifierResolutionForPublishPropertyFailed" FormatArguments="SelfContained"/> - - - diff --git a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildASelfContainedApp.cs b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildASelfContainedApp.cs index 82fbfd804c79..426ce36dcc6a 100644 --- a/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildASelfContainedApp.cs +++ b/src/Tests/Microsoft.NET.Build.Tests/GivenThatWeWantToBuildASelfContainedApp.cs @@ -404,6 +404,33 @@ public void It_does_not_build_SelfContained_due_to_PublishSelfContained_being_tr outputDirectory.Should().NotHaveFile("hostfxr.dll"); // This file will only appear if SelfContained. } + [Theory] + [InlineData("PublishReadyToRun")] + [InlineData("PublishSingleFile")] + [InlineData("PublishSelfContained")] + [InlineData("PublishAot")] + public void It_builds_without_implicit_rid_with_RuntimeIdentifier_specific_during_publish_only_properties(string property) + { + var tfm = ToolsetInfo.CurrentTargetFramework; + var testProject = new TestProject() + { + IsExe = true, + TargetFrameworks = tfm, + }; + testProject.AdditionalProperties[property] = "true"; + testProject.RecordProperties("RuntimeIdentifier"); + var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: property); + + var buildCommand = new DotnetBuildCommand(testAsset); + buildCommand + .Execute() + .Should() + .Pass(); + + var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: tfm); + properties["RuntimeIdentifier"].Should().Be(""); + } + [Theory] [InlineData("net7.0")] public void It_builds_a_runnable_output_with_Prefer32Bit(string targetFramework) diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs index beb6d858e700..978dc4677f8a 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAHelloWorldProject.cs @@ -8,6 +8,7 @@ using System.Runtime.InteropServices; using System.Security.Cryptography; using System.Xml.Linq; +using FluentAssertions; using Microsoft.DotNet.Cli; using Microsoft.DotNet.Cli.Utils; using Microsoft.Extensions.DependencyModel; @@ -1081,7 +1082,6 @@ public void It_publishes_with_full_path_publish_profile() [InlineData("--p:PublishReadyToRun=true")] [InlineData("-p:PublishSingleFile=true")] [InlineData("-p:PublishSelfContained=true")] - [InlineData("")] public void It_publishes_with_implicit_rid_with_rid_specific_properties(string executeOptionsAndProperties) { var testProject = new TestProject() @@ -1092,7 +1092,7 @@ public void It_publishes_with_implicit_rid_with_rid_specific_properties(string e testProject.AdditionalProperties.Add("IsPublishable", "false"); var testAsset = _testAssetsManager.CreateTestProject(testProject, identifier: executeOptionsAndProperties); - var publishCommand = new PublishCommand(testAsset); + var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); publishCommand .Execute(executeOptionsAndProperties) .Should() diff --git a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs index 64c7127452ad..226b33804d8f 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/GivenThatWeWantToPublishAnAotApp.cs @@ -655,10 +655,11 @@ public void It_publishes_with_implicit_rid_with_NativeAotApp(string targetFramew testProject.AdditionalProperties["PublishAot"] = "true"; var testAsset = _testAssetsManager.CreateTestProject(testProject); - var publishCommand = new PublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); + var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); publishCommand .Execute() - .Should().Pass(); + .Should() + .Pass(); } } diff --git a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs index fbe09c52e9a9..a8fc4ead6c30 100644 --- a/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs +++ b/src/Tests/Microsoft.NET.Publish.Tests/RuntimeIdentifiersTests.cs @@ -285,7 +285,7 @@ public void ImplicitRuntimeIdentifierOptOutCorrectlyOptsOut() var testAsset = _testAssetsManager.CreateTestProject(testProject); - var publishCommand = new PublishCommand(testAsset); + var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); publishCommand .Execute() .Should() @@ -335,7 +335,7 @@ public void PublishSuccessfullyWithRIDRequiringPropertyAndRuntimeIdentifiersNoRu testProject.AdditionalProperties["PublishReadyToRun"] = "true"; var testAsset = _testAssetsManager.CreateTestProject(testProject); - var publishCommand = new PublishCommand(testAsset); + var publishCommand = new DotnetPublishCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)); publishCommand .Execute() .Should() diff --git a/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs b/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs index 81abb499c35f..e3f2de3311e2 100644 --- a/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs +++ b/src/Tests/Microsoft.NET.TestFramework/TestAssetsManager.cs @@ -59,6 +59,7 @@ public TestAsset CopyTestAsset( /// Defaults to the name of the caller function (presumably the test). /// Used to prevent file collisions on tests which share the same test project. /// Use this for theories. + /// Pass in the unique theory parameters that can indentify that theory from others. /// The Identifier is used to distinguish between theory child tests. Generally it should be created using a combination of all of the theory parameter values. /// This is distinct from the test project name and is used to prevent file collisions between theory tests that use the same test project. /// The extension type of the desired test project, e.g. .csproj, or .fsproj. diff --git a/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs b/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs index c2871b869b4b..07414392a3cc 100644 --- a/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs +++ b/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs @@ -254,24 +254,30 @@ public void It_does_not_warn_on_rid_with_self_contained_options_prior_to_net6() [Theory] [InlineData("--self-contained")] - [InlineData("-p:PublishTrimmed=true")] - [InlineData("")] - public void It_builds_with_implicit_rid_with_rid_specific_properties(string executeOptionsAndProperties) + public void It_builds_with_implicit_rid_with_SelfContained(string executeOptions) { - var testInstance = _testAssetsManager.CopyTestAsset("HelloWorld") - .WithSource() - .WithTargetFrameworkOrFrameworks("net6.0", false) - .Restore(Log); + var targetFramework = ToolsetInfo.CurrentTargetFramework; + var testProject = new TestProject() + { + IsExe = true, + TargetFrameworks = targetFramework + }; - new DotnetBuildCommand(Log) - .WithWorkingDirectory(testInstance.Path) - .Execute(executeOptionsAndProperties) + testProject.RecordProperties("RuntimeIdentifier"); + var testAsset = _testAssetsManager.CreateTestProject(testProject); + + + new DotnetBuildCommand(Log, Path.Combine(testAsset.TestRoot, testProject.Name)) + .Execute(executeOptions) .Should() .Pass() .And .NotHaveStdOutContaining("NETSDK1031") // Self Contained Checks .And - .NotHaveStdErrContaining("NETSDK1190"); // Check that publish properties don't interfere with build either + .NotHaveStdErrContaining("NETSDK1190"); // Check that publish properties don't interfere with build either + + var properties = testProject.GetPropertyValues(testAsset.TestRoot, targetFramework: targetFramework); + Assert.NotEqual("", properties["RuntimeIdentifier"]); } [RequiresMSBuildVersionFact("17.4.0.41702")] diff --git a/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs b/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs index 49ca2bd8fffe..872f19968158 100644 --- a/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs +++ b/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs @@ -143,36 +143,5 @@ public void CommandsRunWithArchOption(string command) .Should() .Pass(); } - - [Fact] - public void ItUsesImplicitRidWhenNoneIsSpecifiedForSelfContained() - { - CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => - { - var msbuildPath = ""; - var currentRid = CommonOptions.GetCurrentRuntimeId(); - var command = BuildCommand.FromArgs(new string[] { "--self-contained" }, msbuildPath); - command.GetArgumentsToMSBuild() - .Should() - .StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary " + - $"-property:SelfContained=True -property:_CommandLineDefinedSelfContained=true"); - }); - } - - [Fact] - public void ItDoesNotUseImplicitRidWhenOneIsSpecifiedForSelfContained() - { - CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () => - { - var msbuildPath = ""; - var currentRid = CommonOptions.GetCurrentRuntimeId(); - var command = BuildCommand.FromArgs(new string[] { "--self-contained", "--runtime", "fake-rid" }, msbuildPath); - command.GetArgumentsToMSBuild() - .Should() - .StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary " + - $"-property:RuntimeIdentifier=fake-rid -property:_CommandLineDefinedRuntimeIdentifier=true " + - $"-property:SelfContained=True -property:_CommandLineDefinedSelfContained=true"); - }); - } } }