Skip to content

Commit 7058ff2

Browse files
authored
[release/10.0.1xx-preview6] Prefer CLI commands to loose files (#49487)
2 parents ad772df + 49b1ef7 commit 7058ff2

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/Cli/dotnet/Program.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System.CommandLine;
7+
using System.CommandLine.Parsing;
78
using System.Diagnostics;
89
using Microsoft.DotNet.Cli.CommandFactory;
910
using Microsoft.DotNet.Cli.Commands.Run;
@@ -127,10 +128,23 @@ internal static int ProcessArgs(string[] args, TimeSpan startupTime)
127128
ParseResult parseResult;
128129
using (new PerformanceMeasurement(performanceData, "Parse Time"))
129130
{
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+
}
134148

135149
// Avoid create temp directory with root permission and later prevent access in non sudo
136150
// This method need to be run very early before temp folder get created

0 commit comments

Comments
 (0)