Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implicit RIDs when not specified on self contained option #22314

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/Cli/dotnet/CommonOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,14 @@ internal static string ArchOptionValue(ParseResult parseResult) =>
new ForwardedOption<bool>(
new string[] { "--sc", "--self-contained" },
CommonLocalizableStrings.SelfContainedOptionDescription)
.ForwardAsMany(o => new string[] { $"-property:SelfContained={o}", "-property:_CommandLineDefinedSelfContained=true" });
.SetForwardingFunction(ForwardSelfContainedOptions);

public static Option<bool> NoSelfContainedOption =
new ForwardedOption<bool>(
"--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<string> TestPlatformOption = new Option<string>("--Platform");

Expand Down Expand Up @@ -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.
Expand All @@ -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<string> ForwardSelfContainedOptions(bool isSelfContained, ParseResult parseResult)
{
IEnumerable<string> 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
Expand Down
17 changes: 17 additions & 0 deletions src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
32 changes: 32 additions & 0 deletions src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,37 @@ public void CommandsRunWithArchOption(string command)
.Should()
.Pass();
}

[Fact]
public void ItUsesImplicitRidWhenNoneIsSpecifiedForSelfContained()
{
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
{
var msbuildPath = "<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 = "<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");
});
}
}
}