Skip to content

Commit fc944af

Browse files
authored
Implements --no-launch-profile-arguments dotnet-run option (#45841)
1 parent 6319104 commit fc944af

18 files changed

+117
-17
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@
139139
<data name="CommandOptionNoLaunchProfileDescription" xml:space="preserve">
140140
<value>Do not attempt to use launchSettings.json to configure the application.</value>
141141
</data>
142+
<data name="CommandOptionNoLaunchProfileArgumentsDescription" xml:space="preserve">
143+
<value>Do not use arguments specified in launch profile to run the application.</value>
144+
</data>
142145
<data name="ConfigurationOptionDescription" xml:space="preserve">
143146
<value>The configuration to run for. The default for most projects is 'Debug'.</value>
144147
</data>

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

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static RunCommand FromParseResult(ParseResult parseResult)
5959
projectFileOrDirectory: parseResult.GetValue(RunCommandParser.ProjectOption),
6060
launchProfile: parseResult.GetValue(RunCommandParser.LaunchProfileOption),
6161
noLaunchProfile: parseResult.HasOption(RunCommandParser.NoLaunchProfileOption),
62+
noLaunchProfileArguments: parseResult.HasOption(RunCommandParser.NoLaunchProfileArgumentsOption),
6263
noRestore: parseResult.HasOption(RunCommandParser.NoRestoreOption) || parseResult.HasOption(RunCommandParser.NoBuildOption),
6364
interactive: parseResult.HasOption(RunCommandParser.InteractiveOption),
6465
verbosity: parseResult.HasOption(CommonOptions.VerbosityOption) ? parseResult.GetValue(CommonOptions.VerbosityOption) : null,

src/Cli/dotnet/commands/dotnet-run/RunCommand.cs

+19-11
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,36 @@ public partial class RunCommand
2020
{
2121
private record RunProperties(string? RunCommand, string? RunArguments, string? RunWorkingDirectory);
2222

23-
public bool NoBuild { get; private set; }
24-
public string ProjectFileFullPath { get; private set; }
23+
public bool NoBuild { get; }
24+
public string? ProjectFileOrDirectory { get; }
25+
public string ProjectFileFullPath { get; }
2526
public string[] Args { get; set; }
26-
public bool NoRestore { get; private set; }
27+
public bool NoRestore { get; }
2728
public VerbosityOptions? Verbosity { get; }
28-
public bool Interactive { get; private set; }
29-
public string[] RestoreArgs { get; private set; }
29+
public bool Interactive { get; }
30+
public string[] RestoreArgs { get; }
3031

3132
/// <summary>
3233
/// Environment variables specified on command line via -e option.
3334
/// </summary>
34-
public IReadOnlyDictionary<string, string> EnvironmentVariables { get; private set; }
35+
public IReadOnlyDictionary<string, string> EnvironmentVariables { get; }
3536

3637
private bool ShouldBuild => !NoBuild;
3738

38-
public string LaunchProfile { get; private set; }
39-
public bool NoLaunchProfile { get; private set; }
40-
private bool UseLaunchProfile => !NoLaunchProfile;
39+
public string LaunchProfile { get; }
40+
public bool NoLaunchProfile { get; }
41+
42+
/// <summary>
43+
/// True to ignore command line arguments specified by launch profile.
44+
/// </summary>
45+
public bool NoLaunchProfileArguments { get; }
4146

4247
public RunCommand(
4348
bool noBuild,
4449
string? projectFileOrDirectory,
4550
string launchProfile,
4651
bool noLaunchProfile,
52+
bool noLaunchProfileArguments,
4753
bool noRestore,
4854
bool interactive,
4955
VerbosityOptions? verbosity,
@@ -52,9 +58,11 @@ public RunCommand(
5258
IReadOnlyDictionary<string, string> environmentVariables)
5359
{
5460
NoBuild = noBuild;
61+
ProjectFileOrDirectory = projectFileOrDirectory;
5562
ProjectFileFullPath = DiscoverProjectFilePath(projectFileOrDirectory);
5663
LaunchProfile = launchProfile;
5764
NoLaunchProfile = noLaunchProfile;
65+
NoLaunchProfileArguments = noLaunchProfileArguments;
5866
Args = args;
5967
Interactive = interactive;
6068
NoRestore = noRestore;
@@ -125,7 +133,7 @@ private void ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, Project
125133
targetCommand.EnvironmentVariable(entry.Key, value);
126134
}
127135

128-
if (string.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null)
136+
if (!NoLaunchProfileArguments && string.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null)
129137
{
130138
targetCommand.SetCommandArgs(launchSettings.CommandLineArgs);
131139
}
@@ -134,7 +142,7 @@ private void ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, Project
134142
private bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel? launchSettingsModel)
135143
{
136144
launchSettingsModel = default;
137-
if (!UseLaunchProfile)
145+
if (NoLaunchProfile)
138146
{
139147
return true;
140148
}

src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ internal static class RunCommandParser
3434
Description = LocalizableStrings.CommandOptionNoLaunchProfileDescription
3535
};
3636

37+
public static readonly CliOption<bool> NoLaunchProfileArgumentsOption = new("--no-launch-profile-arguments")
38+
{
39+
Description = LocalizableStrings.CommandOptionNoLaunchProfileArgumentsDescription
40+
};
41+
3742
public static readonly CliOption<bool> NoBuildOption = new("--no-build")
3843
{
3944
Description = LocalizableStrings.CommandOptionNoBuildDescription

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

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.es.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.fr.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.it.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.ja.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.ko.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.pl.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.pt-BR.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.ru.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.tr.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.zh-Hans.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/commands/dotnet-run/xlf/LocalizableStrings.zh-Hant.xlf

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/dotnet-run.Tests/GivenDotnetRunBuildsCsProj.cs

+24-6
Original file line numberDiff line numberDiff line change
@@ -856,21 +856,39 @@ public void EnvVariablesSpecifiedInLaunchProfileOverrideImplicitlySetVariables()
856856
[Fact]
857857
public void ItIncludesCommandArgumentsSpecifiedInLaunchSettings()
858858
{
859-
var expectedValue = "TestAppCommandLineArguments";
860-
var secondExpectedValue = "SecondTestAppCommandLineArguments";
861-
var testAppName = "TestAppWithLaunchSettings";
862-
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
859+
var testInstance = _testAssetsManager.CopyTestAsset("TestAppWithLaunchSettings")
863860
.WithSource();
864861

862+
// launchSettings.json specifies commandLineArgs="TestAppCommandLineArguments SecondTestAppCommandLineArguments"
863+
865864
new DotnetCommand(Log, "run")
866865
.WithWorkingDirectory(testInstance.Path)
867866
.Execute()
868867
.Should()
869868
.Pass()
870869
.And
871-
.HaveStdOutContaining(expectedValue)
870+
.HaveStdOutContaining("TestAppCommandLineArguments")
871+
.And
872+
.HaveStdOutContaining("SecondTestAppCommandLineArguments");
873+
}
874+
875+
[Fact]
876+
public void ItIgnoresCommandArgumentsSpecifiedInLaunchSettings()
877+
{
878+
var testInstance = _testAssetsManager.CopyTestAsset("TestAppWithLaunchSettings")
879+
.WithSource();
880+
881+
// launchSettings.json specifies commandLineArgs="TestAppCommandLineArguments SecondTestAppCommandLineArguments"
882+
883+
new DotnetCommand(Log, "run", "--no-launch-profile-arguments")
884+
.WithWorkingDirectory(testInstance.Path)
885+
.Execute()
886+
.Should()
887+
.Pass()
888+
.And
889+
.NotHaveStdOutContaining("TestAppCommandLineArguments")
872890
.And
873-
.HaveStdOutContaining(secondExpectedValue);
891+
.NotHaveStdOutContaining("SecondTestAppCommandLineArguments");
874892
}
875893

876894
[Fact]

0 commit comments

Comments
 (0)