Skip to content

Conversation

ronald-mz
Copy link

Fixes: #45761, #45634, and maybe a regression of #34949.

Context

Watch CLI

dotnet watch [<command>]
  [--list]
  [--no-hot-reload] [--non-interactive]
  [--project <PROJECT>]
  [-q|--quiet] [-v|--verbose]
  [--version]
  [--] <forwarded arguments> 

dotnet watch -?|-h|--help

Problem

dotnet-watch has -p, --project, --launch-profile, and --no-launch-profile as possible arguments. A user can not use these arguments without dotnet-watch forwarding them. These arguments only work with the run command, and will cause an error if passed to any command but run.

@ronald-mz ronald-mz requested review from tmat and a team as code owners August 30, 2025 05:01
@ronald-mz ronald-mz changed the title fix(#45761): Stop passing arguments meant for dotnet-watch to all commands fix(#45761): Stop forwarding dotnet-watch arguments to all commands Aug 30, 2025
@ronald-mz
Copy link
Author

@dotnet-policy-service agree

@ronald-mz ronald-mz changed the title fix(#45761): Stop forwarding dotnet-watch arguments to all commands fix(#45761): Stop forwarding dotnet-watch run arguments to all commands Aug 30, 2025
foreach (var child in parseResult.CommandResult.Children)
{
var optionResult = (OptionResult)child;

// skip watch options:
if (!watchOptions.Contains(optionResult.Option))
Copy link
Author

Choose a reason for hiding this comment

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

Inverting the logic allows a continue to short-circuit the loop.

}

// skip forwarding run options to other commands.
if (isRunCommand == false && forwardToRunOptions.Contains(optionResult.Option))
Copy link
Author

Choose a reason for hiding this comment

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

The meat and potatoes of this PR. Prevents options which only work with the run command from being passed to other commands.

Copy link
Member

Choose a reason for hiding this comment

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

nit: !isRunCommand

Copy link
Member

Choose a reason for hiding this comment

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

Can we pass command from the caller and generalize this to if (forwardToSubcommandOptions.Contains(optionResult.Option) && !command.Options.Contains(optionResult.Option)) ?

Copy link
Author

Choose a reason for hiding this comment

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

Of course. Give me a bit and I'll get some commits up.

Further Thoughts

This is outside the scope of this fix, but a different PR could go a step further towards generalization by:
nixing forwardToSubcommandOptions in favor of watchableSubCommandOptions.

watchableSubCommandOptions are options that can show up in subcommands that could affect watch's settings.

For example: run --project and build [<PROJECT>|<SOLUTION>] could both set watch's project path.

@@ -61,6 +61,12 @@ internal sealed class CommandLineOptions
}
});

// Options we need to know about that are passed through to the subcommand:
Copy link
Member

@tmat tmat Sep 2, 2025

Choose a reason for hiding this comment

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

add ... if the subcommand supports them

var longProjectOption = new Option<string>("--project") { Hidden = true, Arity = ArgumentArity.ZeroOrOne, AllowMultipleArgumentsPerToken = false };
var launchProfileOption = new Option<string>("--launch-profile", "-lp") { Hidden = true, Arity = ArgumentArity.ZeroOrOne, AllowMultipleArgumentsPerToken = false };
var noLaunchProfileOption = new Option<bool>("--no-launch-profile") { Hidden = true, Arity = ArgumentArity.Zero };
Option[] forwardToRunOptions =
Copy link
Member

Choose a reason for hiding this comment

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

I'd call this forwardToSubcommandOptions

Copy link
Member

@tmat tmat Sep 2, 2025

Choose a reason for hiding this comment

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

Nit: You can replace https://github.com/dotnet/sdk/pull/50558/files#diff-6a6fa2fea5af87fda145d79165f529cef772a3227d58494098b7a22511c4aecaR96-R99 with

foreach (var option in forwardToSubcommandOptions)
{
     rootCommand.Options.Add(option);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants