Skip to content

Fix recognizing dotnet ./file.cs with special arguments #49909

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

Merged
merged 3 commits into from
Jul 24, 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: 1 addition & 2 deletions src/Cli/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,7 @@ 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.CommandResult.Command is RootCommand
&& parseResult.GetValue(Parser.DotnetSubCommand) is { } unmatchedCommandOrFile
if (parseResult.GetValue(Parser.DotnetSubCommand) is { } unmatchedCommandOrFile
&& VirtualProjectBuildingCommand.IsValidEntryPointPath(unmatchedCommandOrFile))
{
List<string> otherTokens = new(parseResult.Tokens.Count - 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void AddProjectToSolutionPostActionFindSolutionFileAtOutputPath()
Assert.Equal(solutionFileFullPath, solutionFiles[0]);
}

[Fact(DisplayName = nameof(AddProjectToSolutionPostActionFindSlnxFileAtOutputPath))]
[PlatformSpecificFact(TestPlatforms.Any & ~TestPlatforms.Linux)] // https://github.com/dotnet/sdk/issues/49923
public void AddProjectToSolutionPostActionFindSlnxFileAtOutputPath()
{
string targetBasePath = _engineEnvironmentSettings.GetTempVirtualizedPath();
Expand Down
38 changes: 34 additions & 4 deletions test/dotnet.Tests/CommandTests/Run/RunFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,34 @@ Hello from Program
{s_program}
""");

string expectedOutput = """
Hello from Program
Release config
""";

new DotnetCommand(Log, "Program.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut("""
Hello from Program
Release config
""");
.And.HaveStdOut(expectedOutput);

new DotnetCommand(Log, "./Program.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut(expectedOutput);

new DotnetCommand(Log, $".{Path.DirectorySeparatorChar}Program.cs")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut(expectedOutput);

new DotnetCommand(Log, Path.Join(testInstance.Path, "Program.cs"))
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut(expectedOutput);

new DotnetCommand(Log, "Program.cs", "-c", "Debug")
.WithWorkingDirectory(testInstance.Path)
Expand All @@ -168,6 +188,16 @@ Hello from Program
Hello from Program
Release config
""");

new DotnetCommand(Log, "Program.cs", "build")
.WithWorkingDirectory(testInstance.Path)
.Execute()
.Should().Pass()
.And.HaveStdOut("""
echo args:build
Hello from Program
Release config
""");
}

/// <summary>
Expand Down