Skip to content

Commit 02fc8b9

Browse files
committed
Add implicit RIDs when not specified on self contained option
1 parent 957ae5c commit 02fc8b9

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

src/Cli/dotnet/CommonOptions.cs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ internal static string ArchOptionValue(ParseResult parseResult) =>
142142
new ForwardedOption<bool>(
143143
new string[] { "--sc", "--self-contained" },
144144
CommonLocalizableStrings.SelfContainedOptionDescription)
145-
.ForwardAsMany(o => new string[] { $"-property:SelfContained={o}", "-property:_CommandLineDefinedSelfContained=true" });
145+
.SetForwardingFunction(ForwardSelfContainedOptions);
146146

147147
public static Option<bool> NoSelfContainedOption =
148148
new ForwardedOption<bool>(
149149
"--no-self-contained",
150150
CommonLocalizableStrings.FrameworkDependentOptionDescription)
151-
.ForwardAsMany(o => new string[] { "-property:SelfContained=false", "-property:_CommandLineDefinedSelfContained=true" });
151+
.SetForwardingFunction(ForwardSelfContainedOptions);
152152

153153
public static readonly Option<string> TestPlatformOption = new Option<string>("--Platform");
154154

@@ -223,7 +223,7 @@ internal static string ResolveRidShorthandOptionsToRuntimeIdentifier(string os,
223223
return $"{os}-{arch}";
224224
}
225225

226-
private static string GetCurrentRuntimeId()
226+
public static string GetCurrentRuntimeId()
227227
{
228228
var dotnetRootPath = Path.GetDirectoryName(Environment.ProcessPath);
229229
// When running under test the path does not always contain "dotnet" and Product.Version is empty.
@@ -245,6 +245,26 @@ private static string GetCurrentRuntimeId()
245245
private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-"));
246246

247247
private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-") + 1, rid.Length - rid.LastIndexOf("-") - 1);
248+
249+
private static IEnumerable<string> ForwardSelfContainedOptions(bool arg, ParseResult parseResult)
250+
{
251+
IEnumerable<string> selfContainedProperties = new string[] { $"-property:SelfContained={arg}", "-property:_CommandLineDefinedSelfContained=true" };
252+
253+
if (!UserSpecifiedRidOption(parseResult))
254+
{
255+
var ridProperties = RuntimeArgFunc(GetCurrentRuntimeId());
256+
selfContainedProperties = selfContainedProperties.Concat(ridProperties);
257+
}
258+
259+
return selfContainedProperties;
260+
}
261+
262+
private static bool UserSpecifiedRidOption(ParseResult parseResult) =>
263+
parseResult.HasOption(RuntimeOption) ||
264+
parseResult.HasOption(LongFormRuntimeOption) ||
265+
parseResult.HasOption(ArchitectureOption) ||
266+
parseResult.HasOption(LongFormArchitectureOption) ||
267+
parseResult.HasOption(OperatingSystemOption);
248268
}
249269

250270
public enum VerbosityOptions

src/Tests/dotnet-build.Tests/GivenDotnetBuildBuildsCsproj.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ public void It_does_not_warn_on_rid_with_self_contained_options_prior_to_net6()
230230
.NotHaveStdOutContaining("NETSDK1179");
231231
}
232232

233+
[Fact]
234+
public void It_builds_with_implicit_rid_with_self_contained_option()
235+
{
236+
var testInstance = _testAssetsManager.CopyTestAsset("HelloWorld")
237+
.WithSource()
238+
.WithTargetFrameworkOrFrameworks("net6.0", false)
239+
.Restore(Log);
240+
241+
new DotnetBuildCommand(Log)
242+
.WithWorkingDirectory(testInstance.Path)
243+
.Execute("--self-contained")
244+
.Should()
245+
.Pass()
246+
.And
247+
.NotHaveStdOutContaining("NETSDK1031");
248+
}
249+
233250
[Theory]
234251
[InlineData("roslyn3.9")]
235252
[InlineData("roslyn4.0")]

src/Tests/dotnet.Tests/dotnet-msbuild/GivenDotnetOsArchOptions.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,5 +143,37 @@ public void CommandsRunWithArchOption(string command)
143143
.Should()
144144
.Pass();
145145
}
146+
147+
[Fact]
148+
public void ItUsesImplicitRidWhenNoneIsSpecifiedForSelfContained()
149+
{
150+
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
151+
{
152+
var msbuildPath = "<msbuildpath>";
153+
var currentRid = CommonOptions.GetCurrentRuntimeId();
154+
var command = BuildCommand.FromArgs(new string[] { "--self-contained" }, msbuildPath);
155+
command.GetArgumentsToMSBuild()
156+
.Should()
157+
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary " +
158+
$"-property:SelfContained=True -property:_CommandLineDefinedSelfContained=true " +
159+
$"-property:RuntimeIdentifier={currentRid} -property:_CommandLineDefinedRuntimeIdentifier=true");
160+
});
161+
}
162+
163+
[Fact]
164+
public void ItDoesNotUseImplicitRidWhenOneIsSpecifiedForSelfContained()
165+
{
166+
CommandDirectoryContext.PerformActionWithBasePath(WorkingDirectory, () =>
167+
{
168+
var msbuildPath = "<msbuildpath>";
169+
var currentRid = CommonOptions.GetCurrentRuntimeId();
170+
var command = BuildCommand.FromArgs(new string[] { "--self-contained", "--runtime", "fake-rid" }, msbuildPath);
171+
command.GetArgumentsToMSBuild()
172+
.Should()
173+
.StartWith($"{ExpectedPrefix} -restore -consoleloggerparameters:Summary " +
174+
$"-property:RuntimeIdentifier=fake-rid -property:_CommandLineDefinedRuntimeIdentifier=true " +
175+
$"-property:SelfContained=True -property:_CommandLineDefinedSelfContained=true");
176+
});
177+
}
146178
}
147179
}

0 commit comments

Comments
 (0)