|
4 | 4 | #nullable disable
|
5 | 5 |
|
6 | 6 | using System.CommandLine;
|
| 7 | +using System.CommandLine.Parsing; |
7 | 8 | using System.Diagnostics;
|
8 | 9 | using Microsoft.DotNet.Cli.CommandFactory;
|
9 | 10 | using Microsoft.DotNet.Cli.Commands.Run;
|
@@ -127,10 +128,23 @@ internal static int ProcessArgs(string[] args, TimeSpan startupTime)
|
127 | 128 | ParseResult parseResult;
|
128 | 129 | using (new PerformanceMeasurement(performanceData, "Parse Time"))
|
129 | 130 | {
|
130 |
| - // If we get C# file path as the first argument, parse as `dotnet run file.cs`. |
131 |
| - parseResult = args is [{ } filePath, ..] && VirtualProjectBuildingCommand.IsValidEntryPointPath(filePath) |
132 |
| - ? Parser.Instance.Parse(["run", .. args]) |
133 |
| - : Parser.Instance.Parse(args); |
| 131 | + parseResult = Parser.Instance.Parse(args); |
| 132 | + // If we get didn't match any built-in commands, and a C# file path is the first argument, |
| 133 | + // parse as `dotnet run file.cs ..rest_of_args` instead. |
| 134 | + if (parseResult.CommandResult.Command is RootCommand |
| 135 | + && parseResult.GetValue(Parser.DotnetSubCommand) is { } unmatchedCommandOrFile |
| 136 | + && VirtualProjectBuildingCommand.IsValidEntryPointPath(unmatchedCommandOrFile)) |
| 137 | + { |
| 138 | + List<string> otherTokens = new(parseResult.Tokens.Count - 1); |
| 139 | + foreach (var token in parseResult.Tokens) |
| 140 | + { |
| 141 | + if (token.Type != TokenType.Argument || token.Value != unmatchedCommandOrFile) |
| 142 | + { |
| 143 | + otherTokens.Add(token.Value); |
| 144 | + } |
| 145 | + } |
| 146 | + parseResult = Parser.Instance.Parse(["run", unmatchedCommandOrFile, .. otherTokens]); |
| 147 | + } |
134 | 148 |
|
135 | 149 | // Avoid create temp directory with root permission and later prevent access in non sudo
|
136 | 150 | // This method need to be run very early before temp folder get created
|
|
0 commit comments