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

Implements --no-launch-profile-arguments dotnet-run option #45841

Merged
merged 1 commit into from
Jan 9, 2025
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
3 changes: 3 additions & 0 deletions src/Cli/dotnet/commands/dotnet-run/LocalizableStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@
<data name="CommandOptionNoLaunchProfileDescription" xml:space="preserve">
<value>Do not attempt to use launchSettings.json to configure the application.</value>
</data>
<data name="CommandOptionNoLaunchProfileArgumentsDescription" xml:space="preserve">
<value>Do not use arguments specified in launch profile to run the application.</value>
</data>
<data name="ConfigurationOptionDescription" xml:space="preserve">
<value>The configuration to run for. The default for most projects is 'Debug'.</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/Cli/dotnet/commands/dotnet-run/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public static RunCommand FromParseResult(ParseResult parseResult)
projectFileOrDirectory: parseResult.GetValue(RunCommandParser.ProjectOption),
launchProfile: parseResult.GetValue(RunCommandParser.LaunchProfileOption),
noLaunchProfile: parseResult.HasOption(RunCommandParser.NoLaunchProfileOption),
noLaunchProfileArguments: parseResult.HasOption(RunCommandParser.NoLaunchProfileArgumentsOption),
noRestore: parseResult.HasOption(RunCommandParser.NoRestoreOption) || parseResult.HasOption(RunCommandParser.NoBuildOption),
interactive: parseResult.HasOption(RunCommandParser.InteractiveOption),
verbosity: parseResult.HasOption(CommonOptions.VerbosityOption) ? parseResult.GetValue(CommonOptions.VerbosityOption) : null,
Expand Down
30 changes: 19 additions & 11 deletions src/Cli/dotnet/commands/dotnet-run/RunCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ public partial class RunCommand
{
private record RunProperties(string? RunCommand, string? RunArguments, string? RunWorkingDirectory);

public bool NoBuild { get; private set; }
public string ProjectFileFullPath { get; private set; }
public bool NoBuild { get; }
public string? ProjectFileOrDirectory { get; }
public string ProjectFileFullPath { get; }
public string[] Args { get; set; }
public bool NoRestore { get; private set; }
public bool NoRestore { get; }
public VerbosityOptions? Verbosity { get; }
public bool Interactive { get; private set; }
public string[] RestoreArgs { get; private set; }
public bool Interactive { get; }
public string[] RestoreArgs { get; }

/// <summary>
/// Environment variables specified on command line via -e option.
/// </summary>
public IReadOnlyDictionary<string, string> EnvironmentVariables { get; private set; }
public IReadOnlyDictionary<string, string> EnvironmentVariables { get; }

private bool ShouldBuild => !NoBuild;

public string LaunchProfile { get; private set; }
public bool NoLaunchProfile { get; private set; }
private bool UseLaunchProfile => !NoLaunchProfile;
public string LaunchProfile { get; }
public bool NoLaunchProfile { get; }

/// <summary>
/// True to ignore command line arguments specified by launch profile.
/// </summary>
public bool NoLaunchProfileArguments { get; }

public RunCommand(
bool noBuild,
string? projectFileOrDirectory,
string launchProfile,
bool noLaunchProfile,
bool noLaunchProfileArguments,
bool noRestore,
bool interactive,
VerbosityOptions? verbosity,
Expand All @@ -52,9 +58,11 @@ public RunCommand(
IReadOnlyDictionary<string, string> environmentVariables)
{
NoBuild = noBuild;
ProjectFileOrDirectory = projectFileOrDirectory;
ProjectFileFullPath = DiscoverProjectFilePath(projectFileOrDirectory);
LaunchProfile = launchProfile;
NoLaunchProfile = noLaunchProfile;
NoLaunchProfileArguments = noLaunchProfileArguments;
Args = args;
Interactive = interactive;
NoRestore = noRestore;
Expand Down Expand Up @@ -125,7 +133,7 @@ private void ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, Project
targetCommand.EnvironmentVariable(entry.Key, value);
}

if (string.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null)
if (!NoLaunchProfileArguments && string.IsNullOrEmpty(targetCommand.CommandArgs) && launchSettings.CommandLineArgs != null)
{
targetCommand.SetCommandArgs(launchSettings.CommandLineArgs);
}
Expand All @@ -134,7 +142,7 @@ private void ApplyLaunchSettingsProfileToCommand(ICommand targetCommand, Project
private bool TryGetLaunchProfileSettingsIfNeeded(out ProjectLaunchSettingsModel? launchSettingsModel)
{
launchSettingsModel = default;
if (!UseLaunchProfile)
if (NoLaunchProfile)
{
return true;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Cli/dotnet/commands/dotnet-run/RunCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ internal static class RunCommandParser
Description = LocalizableStrings.CommandOptionNoLaunchProfileDescription
};

public static readonly CliOption<bool> NoLaunchProfileArgumentsOption = new("--no-launch-profile-arguments")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add Arity = ArgumentArity.Zero to the object initializer for this option so that this is treated as a flag-only option.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will follow up.

{
Description = LocalizableStrings.CommandOptionNoLaunchProfileArgumentsDescription
};

public static readonly CliOption<bool> NoBuildOption = new("--no-build")
{
Description = LocalizableStrings.CommandOptionNoBuildDescription
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 24 additions & 6 deletions test/dotnet-run.Tests/GivenDotnetRunBuildsCsProj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -856,21 +856,39 @@ public void EnvVariablesSpecifiedInLaunchProfileOverrideImplicitlySetVariables()
[Fact]
public void ItIncludesCommandArgumentsSpecifiedInLaunchSettings()
{
var expectedValue = "TestAppCommandLineArguments";
var secondExpectedValue = "SecondTestAppCommandLineArguments";
var testAppName = "TestAppWithLaunchSettings";
var testInstance = _testAssetsManager.CopyTestAsset(testAppName)
var testInstance = _testAssetsManager.CopyTestAsset("TestAppWithLaunchSettings")
.WithSource();

// launchSettings.json specifies commandLineArgs="TestAppCommandLineArguments SecondTestAppCommandLineArguments"

new DotnetCommand(Log, "run")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should()
.Pass()
.And
.HaveStdOutContaining(expectedValue)
.HaveStdOutContaining("TestAppCommandLineArguments")
.And
.HaveStdOutContaining("SecondTestAppCommandLineArguments");
}

[Fact]
public void ItIgnoresCommandArgumentsSpecifiedInLaunchSettings()
{
var testInstance = _testAssetsManager.CopyTestAsset("TestAppWithLaunchSettings")
.WithSource();

// launchSettings.json specifies commandLineArgs="TestAppCommandLineArguments SecondTestAppCommandLineArguments"

new DotnetCommand(Log, "run", "--no-launch-profile-arguments")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should()
.Pass()
.And
.NotHaveStdOutContaining("TestAppCommandLineArguments")
.And
.HaveStdOutContaining(secondExpectedValue);
.NotHaveStdOutContaining("SecondTestAppCommandLineArguments");
}

[Fact]
Expand Down
Loading