Skip to content

Commit d549243

Browse files
committed
Warn in using -r without self contained for net 6+
1 parent 9c221bd commit d549243

36 files changed

+248
-81
lines changed

src/Cli/dotnet/CommonLocalizableStrings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,4 +681,10 @@ The default is 'true' if a runtime identifier is specified.</value>
681681
<data name="NoSelfContainedOptionDescription" xml:space="preserve">
682682
<value>Publish your application as a framework dependent application without the .NET runtime. A supported .NET runtime must be installed to run your application.</value>
683683
</data>
684+
<data name="SelfContainAndNoSelfContainedConflict" xml:space="preserve">
685+
<value>The '--self-contained' and '--no-self-contained' options conflict with each other. Specify only one of the options.</value>
686+
</data>
687+
<data name="RuntimeOptionShouldBeUsedWithSelfContained" xml:space="preserve">
688+
<value>Warning: The '-r|--runtime' option should be used with '--self-contained' or '--no-self-contained'.</value>
689+
</data>
684690
</root>

src/Cli/dotnet/CommonOptions.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
using System.CommandLine;
77
using System.IO;
88
using Microsoft.DotNet.Tools.Common;
9+
using System.Collections.Generic;
10+
using Microsoft.DotNet.Cli.Utils;
11+
using System.Linq;
12+
using Microsoft.Build.Evaluation;
13+
using NuGet.Frameworks;
914

1015
namespace Microsoft.DotNet.Cli
1116
{
@@ -112,6 +117,34 @@ public static bool VerbosityIsDetailedOrDiagnostic(this VerbosityOptions verbosi
112117
verbosity.Equals(VerbosityOptions.d) ||
113118
verbosity.Equals(VerbosityOptions.detailed);
114119
}
120+
121+
public static void ValidateSelfContainedOptions(bool hasSelfContainedOption, bool hasNoSelfContainedOption, bool hasRuntimeOption, IEnumerable<string> projectArgs)
122+
{
123+
if (hasSelfContainedOption && hasNoSelfContainedOption)
124+
{
125+
throw new GracefulException(CommonLocalizableStrings.SelfContainAndNoSelfContainedConflict);
126+
}
127+
128+
if (!(hasSelfContainedOption || hasNoSelfContainedOption) && hasRuntimeOption)
129+
{
130+
projectArgs = projectArgs.Any() ? projectArgs : new string[] { Directory.GetCurrentDirectory() };
131+
foreach (var project in projectArgs)
132+
{
133+
try
134+
{
135+
var msbuildProj = MsbuildProject.FromFileOrDirectory(new ProjectCollection(), project, false);
136+
if (msbuildProj.IsTargetingNetCoreVersionOrAbove(NuGetFramework.Parse("net6.0")))
137+
{
138+
Reporter.Output.WriteLine(CommonLocalizableStrings.RuntimeOptionShouldBeUsedWithSelfContained.Yellow());
139+
}
140+
}
141+
catch
142+
{
143+
// Project file is not valid, continue and msbuild will error
144+
}
145+
}
146+
}
147+
}
115148
}
116149

117150
public enum VerbosityOptions

src/Cli/dotnet/MsbuildProject.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,19 @@ public bool IsTargetingFramework(NuGetFramework framework)
205205
return false;
206206
}
207207

208+
public bool IsTargetingNetCoreVersionOrAbove(NuGetFramework framework)
209+
{
210+
foreach (var tfm in GetTargetFrameworks())
211+
{
212+
if (tfm.Framework.Equals(".NETCoreApp") && tfm.Version >= framework.Version)
213+
{
214+
return true;
215+
}
216+
}
217+
218+
return false;
219+
}
220+
208221
private Project GetEvaluatedProject()
209222
{
210223
try

src/Cli/dotnet/commands/dotnet-build/BuildCommand.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,10 @@ public static BuildCommand FromArgs(string[] args, string msbuildPath = null)
3232

3333
parseResult.ShowHelpOrErrorIfAppropriate();
3434

35-
if (parseResult.HasOption(BuildCommandParser.SelfContainedOption) &&
36-
parseResult.HasOption(BuildCommandParser.NoSelfContainedOption))
37-
{
38-
throw new GracefulException(Publish.LocalizableStrings.SelfContainAndNoSelfContainedConflict);
39-
}
35+
CommonOptions.ValidateSelfContainedOptions(parseResult.HasOption(BuildCommandParser.SelfContainedOption),
36+
parseResult.HasOption(BuildCommandParser.NoSelfContainedOption),
37+
parseResult.HasOption(BuildCommandParser.RuntimeOption),
38+
parseResult.ValueForArgument<IEnumerable<string>>(BuildCommandParser.SlnOrProjectArgument) ?? Array.Empty<string>());
4039

4140
msbuildArgs.Add($"-consoleloggerparameters:Summary");
4241

src/Cli/dotnet/commands/dotnet-build/BuildCommandParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ internal static class BuildCommandParser
3535

3636
public static readonly Option NoSelfContainedOption = CommonOptions.NoSelfContainedOption();
3737

38+
public static readonly Option RuntimeOption = CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription);
39+
3840
public static Command GetCommand()
3941
{
4042
var command = new Command("build", LocalizableStrings.AppFullName);
@@ -43,7 +45,7 @@ public static Command GetCommand()
4345
RestoreCommandParser.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: false);
4446
command.AddOption(CommonOptions.FrameworkOption(LocalizableStrings.FrameworkOptionDescription));
4547
command.AddOption(CommonOptions.ConfigurationOption(LocalizableStrings.ConfigurationOptionDescription));
46-
command.AddOption(CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription));
48+
command.AddOption(RuntimeOption);
4749
command.AddOption(CommonOptions.VersionSuffixOption());
4850
command.AddOption(NoRestoreOption);
4951
command.AddOption(CommonOptions.InteractiveMsBuildForwardOption());

src/Cli/dotnet/commands/dotnet-publish/LocalizableStrings.resx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,4 @@ The default is to publish a framework-dependent application.</value>
151151
<data name="CmdNoLogo" xml:space="preserve">
152152
<value>Do not display the startup banner or the copyright message.</value>
153153
</data>
154-
<data name="SelfContainAndNoSelfContainedConflict" xml:space="preserve">
155-
<value>The '--self-contained' and '--no-self-contained' options conflict with each other. Specify only one of the options.</value>
156-
</data>
157154
</root>

src/Cli/dotnet/commands/dotnet-publish/Program.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,10 @@ public static PublishCommand FromArgs(string[] args, string msbuildPath = null)
3535

3636
msbuildArgs.Add("-target:Publish");
3737

38-
if (parseResult.HasOption(PublishCommandParser.SelfContainedOption) &&
39-
parseResult.HasOption(PublishCommandParser.NoSelfContainedOption))
40-
{
41-
throw new GracefulException(LocalizableStrings.SelfContainAndNoSelfContainedConflict);
42-
}
38+
CommonOptions.ValidateSelfContainedOptions(parseResult.HasOption(PublishCommandParser.SelfContainedOption),
39+
parseResult.HasOption(PublishCommandParser.NoSelfContainedOption),
40+
parseResult.HasOption(PublishCommandParser.RuntimeOption),
41+
parseResult.ValueForArgument<IEnumerable<string>>(PublishCommandParser.SlnOrProjectArgument) ?? Array.Empty<string>());
4342

4443
msbuildArgs.AddRange(parseResult.OptionValuesToBeForwarded(PublishCommandParser.GetCommand()));
4544

src/Cli/dotnet/commands/dotnet-publish/PublishCommandParser.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ internal static class PublishCommandParser
4040

4141
public static readonly Option NoSelfContainedOption = CommonOptions.NoSelfContainedOption();
4242

43+
public static readonly Option RuntimeOption = CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription);
44+
4345
public static Command GetCommand()
4446
{
4547
var command = new Command("publish", LocalizableStrings.AppDescription);
@@ -53,7 +55,7 @@ public static Command GetCommand()
5355
command.AddOption(NoSelfContainedOption);
5456
command.AddOption(NoLogoOption);
5557
command.AddOption(CommonOptions.FrameworkOption(LocalizableStrings.FrameworkOptionDescription));
56-
command.AddOption(CommonOptions.RuntimeOption(LocalizableStrings.RuntimeOptionDescription));
58+
command.AddOption(RuntimeOption);
5759
command.AddOption(CommonOptions.ConfigurationOption(LocalizableStrings.ConfigurationOptionDescription));
5860
command.AddOption(CommonOptions.VersionSuffixOption());
5961
command.AddOption(CommonOptions.InteractiveMsBuildForwardOption());

src/Cli/dotnet/commands/dotnet-publish/xlf/LocalizableStrings.cs.xlf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ Ve výchozím nastavení je publikována aplikace závislá na architektuře.</t
5959
<target state="translated">Konfigurace pro publikování. Výchozí možností pro většinu projektů je Debug.</target>
6060
<note />
6161
</trans-unit>
62-
<trans-unit id="SelfContainAndNoSelfContainedConflict">
63-
<source>The '--self-contained' and '--no-self-contained' options conflict with each other. Specify only one of the options.</source>
64-
<target state="translated">Možnosti --self-contained a --no-self-contained jsou vzájemně konfliktní. Zadejte pouze jednu z nich.</target>
65-
<note />
66-
</trans-unit>
6762
</body>
6863
</file>
6964
</xliff>

src/Cli/dotnet/commands/dotnet-publish/xlf/LocalizableStrings.de.xlf

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,6 @@ Standardmäßig wird eine Framework-abhängige Anwendung veröffentlicht.</targe
5959
<target state="translated">Die Konfiguration für die Veröffentlichung. Der Standardwert für die meisten Projekte ist "Debug".</target>
6060
<note />
6161
</trans-unit>
62-
<trans-unit id="SelfContainAndNoSelfContainedConflict">
63-
<source>The '--self-contained' and '--no-self-contained' options conflict with each other. Specify only one of the options.</source>
64-
<target state="translated">Die Optionen "--self-contained" und "--no-self-contained" stehen in Konflikt zueinander. Geben Sie nur eine der Optionen an.</target>
65-
<note />
66-
</trans-unit>
6762
</body>
6863
</file>
6964
</xliff>

0 commit comments

Comments
 (0)