From 419f513a7871967f3ea9fa2bddb98779430809ac Mon Sep 17 00:00:00 2001 From: sfoslund Date: Mon, 25 Oct 2021 16:11:07 -0700 Subject: [PATCH] Add implicit RIDs when not specified on self contained option --- src/Cli/dotnet/CommonOptions.cs | 27 ++++++++++++++-- .../GivenDotnetBuildBuildsCsproj.cs | 17 ++++++++++ .../GivenDotnetOsArchOptions.cs | 32 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) diff --git a/src/Cli/dotnet/CommonOptions.cs b/src/Cli/dotnet/CommonOptions.cs index ca6c94e07f4a..4f97de9a41d6 100644 --- a/src/Cli/dotnet/CommonOptions.cs +++ b/src/Cli/dotnet/CommonOptions.cs @@ -142,13 +142,14 @@ internal static string ArchOptionValue(ParseResult parseResult) => new ForwardedOption( new string[] { "--sc", "--self-contained" }, CommonLocalizableStrings.SelfContainedOptionDescription) - .ForwardAsMany(o => new string[] { $"-property:SelfContained={o}", "-property:_CommandLineDefinedSelfContained=true" }); + .SetForwardingFunction(ForwardSelfContainedOptions); public static Option NoSelfContainedOption = new ForwardedOption( "--no-self-contained", CommonLocalizableStrings.FrameworkDependentOptionDescription) - .ForwardAsMany(o => new string[] { "-property:SelfContained=false", "-property:_CommandLineDefinedSelfContained=true" }); + // Flip the argument so that if this option is specified we get selfcontained=false + .SetForwardingFunction((arg, p) => ForwardSelfContainedOptions(!arg, p)); public static readonly Option TestPlatformOption = new Option("--Platform"); @@ -223,7 +224,7 @@ internal static string ResolveRidShorthandOptionsToRuntimeIdentifier(string os, return $"{os}-{arch}"; } - private static string GetCurrentRuntimeId() + public static string GetCurrentRuntimeId() { var dotnetRootPath = Path.GetDirectoryName(Environment.ProcessPath); // When running under test the path does not always contain "dotnet" and Product.Version is empty. @@ -245,6 +246,26 @@ private static string GetCurrentRuntimeId() private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-")); private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-") + 1, rid.Length - rid.LastIndexOf("-") - 1); + + private static IEnumerable ForwardSelfContainedOptions(bool isSelfContained, ParseResult parseResult) + { + IEnumerable selfContainedProperties = new string[] { $"-property:SelfContained={isSelfContained}", "-property:_CommandLineDefinedSelfContained=true" }; + + if (!UserSpecifiedRidOption(parseResult) && isSelfContained) + { + var ridProperties = RuntimeArgFunc(GetCurrentRuntimeId()); + selfContainedProperties = selfContainedProperties.Concat(ridProperties); + } + + return selfContainedProperties; + } + + private static bool UserSpecifiedRidOption(ParseResult parseResult) => + parseResult.HasOption(RuntimeOption) || + parseResult.HasOption(LongFormRuntimeOption) || + parseResult.HasOption(ArchitectureOption) || + parseResult.HasOption(LongFormArchitectureOption) || + parseResult.HasOption(OperatingSystemOption); } public enum VerbosityOptions diff --git a/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs b/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs index 1a4d48fc57f7..f1b53c439660 100644 --- a/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs +++ b/src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs @@ -230,6 +230,23 @@ public void It_does_not_warn_on_rid_with_self_contained_options_prior_to_net6() .NotHaveStdOutContaining("NETSDK1179"); } + [Fact] + public void It_builds_with_implicit_rid_with_self_contained_option() + { + var testInstance = _testAssetsManager.CopyTestAsset("HelloWorld") + .WithSource() + .WithTargetFrameworkOrFrameworks("net6.0", false) + .Restore(Log); + + new DotnetBuildCommand(Log) + .WithWorkingDirectory(testInstance.Path) + .Execute("--self-contained") + .Should() + .Pass() + .And + .NotHaveStdOutContaining("NETSDK1031"); + } + [Theory] [InlineData("roslyn3.9")] [InlineData("roslyn4.0")] diff --git a/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs b/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs index 872f19968158..6f68a648b745 100644 --- a/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs +++ b/src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs @@ -143,5 +143,37 @@ 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 " + + $"-property:RuntimeIdentifier={currentRid} -property:_CommandLineDefinedRuntimeIdentifier=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"); + }); + } } }