Skip to content
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
8 changes: 4 additions & 4 deletions src/Cli/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,19 +306,19 @@ internal static int ProcessArgs(string[] args, TimeSpan startupTime)
{
// If we didn't match any built-in commands, and a C# file path is the first argument,
// parse as `dotnet run --file file.cs ..rest_of_args` instead.
if (parseResult.GetValue(Parser.DotnetSubCommand) is { } unmatchedCommandOrFile
&& VirtualProjectBuildingCommand.IsValidEntryPointPath(unmatchedCommandOrFile))
if (parseResult.GetResult(Parser.DotnetSubCommand) is { Tokens: [{ Type: TokenType.Argument, Value: { } } unmatchedCommandOrFile] }
&& VirtualProjectBuildingCommand.IsValidEntryPointPath(unmatchedCommandOrFile.Value))
{
List<string> otherTokens = new(parseResult.Tokens.Count - 1);
foreach (var token in parseResult.Tokens)
{
if (token.Type != TokenType.Argument || token.Value != unmatchedCommandOrFile)
if (token != unmatchedCommandOrFile)
Copy link
Member

@RikkiGibson RikkiGibson Dec 10, 2025

Choose a reason for hiding this comment

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

There isn't a need to make a further change, but, I was wondering if otherTokens ends up being equivalent to parseResult.Tokens[1..]? (I guess with some intermediate .Value access inserted.)

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that's guaranteed, there might be some special tokens before the subcommand argument. But I'm also not sure it makes sense to try to preserve the order until we have a motivating scenario.

{
otherTokens.Add(token.Value);
}
}

parseResult = Parser.Parse(["run", "--file", unmatchedCommandOrFile, .. otherTokens]);
parseResult = Parser.Parse(["run", "--file", unmatchedCommandOrFile.Value, .. otherTokens]);

InvokeBuiltInCommand(parseResult, out var exitCode);
return exitCode;
Expand Down
11 changes: 11 additions & 0 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ Release config
Hello from Program
Release config
""");

// https://github.com/dotnet/sdk/issues/52108
new DotnetCommand(Log, "Program.cs", "Program.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut("""
echo args:Program.cs
Hello from Program
Release config
""");
}

/// <summary>
Expand Down
Loading